首页 > 解决方案 > 如何将多个函数打包到一个python包并上传到PyPI

问题描述

我创建了一个 python 包,theanwer. 这是PyPIGitHub 上的内容。它只有一个功能,theanswer(). 安装后:

import theanswer
theanswer.theanswer()

按预期返回42

目录结构为:

theanwer/
    theanswer/
        __init__.py
    setup.py

__init__.py包含:

def theanswer():
    return 42

if __name__ == "__main__":
    theanswer()

setup.py包含:

setup(name='theanswer',
      version='0.1',
      description='answer to the ultimate question',
      url='https://github.com/zabop/PyPIattempt',
      author='Pal Szabo',
      author_email='szabopal96@gmail.com',
      license='MIT',
      packages=['theanswer'],
      zip_safe=False)

我按照此处此处的说明执行此操作。

现在,如果我想在答案包中包含此代码:

def returnthis(this)
    return this

我想用它作为:

theanswer.returnthis(this)

归来this。我怎样才能做到这一点?文件应该是setup.py什么样子,我需要使用什么目录结构(才能将其上传到 PyPI)?


我对目录树的想法:

theanwer/
    theanswer/
        __init__.py # containing the function theanswer
    returnthis/
        __init__.py # containing the function returnthis

setup.py文件中,包应该是packages=['theanswer','returnthis'].

这是一种方法吗?

标签: pythonpython-3.xpackagepypi

解决方案


推荐阅读