How to pack and distribute python program (.py source code) so that other developers can easily install all required dependencies?
我正在开发一个python应用程序,它使用一些我安装了PIP的包构建,比如flask、requests和pil。
那么,我如何分发我的程序,以便其他人可以轻松地安装每一个所需的依赖项/包,并使其在每台计算机上工作?setup.py是不是我要找的?如果是这样的话,您能解释一下它是做什么的,并提供一个示例setup.py来完成我想要做的工作吗?
附言:我还有一个小问题:我需要在我的程序的顶层文件夹中提供一个初始化文件,还是只在子目录中提供?
在不那么老的日子里,我用这个指南来学习如何打包和分发我的python代码,然后一些优秀的人创建了flit,它允许我用三个步骤完成整个过程。
1 | $pip install flit |
创建我的元数据文件:
1 2 3 4 5 6 7 8 9 10 11 | [metadata] author=Some guy author-email=some-email@nowhere.com home-page=https://github.com/someuser/somepackage requires=requests requires-python= >=3 description-file=README.rst classifiers=Intended Audience :: Developers License :: OSI Approved :: BSD License Programming Language :: Python :: 3 Topic :: Software Development :: Libraries :: Python Modules |
发布我的包:
1 | $pip flit publish |
完成了!!!!
以下是熊猫设置文件的链接,您可以在其中查看它们如何执行依赖项检查,它们可能是平台特定的,也可能是任何第三方软件包特定的。
根据这里的文件
setup.py
There is another type of dependency specification for Python libraries
known as setup.py. Setup.py is a standard for distributing and
installing Python libraries. If you're building a Python library, such
as requests or underwear you must include setup.py so a dependency
manager can correctly install both the library as well as additional
dependencies for the library. There's still quite a bit of confusion
in the Python community over the difference between requirements.txt
and setup.py, so read this well written post for further
clarification.
同时检查:
什么是setup.py?
您可以看到这个例子,了解如何制作您的:
https://github.com/pypa/sampleproject/blob/master/setup.py
此外,这里还有一个指南:
https://pythonhosted.org/an_-example_-pypi_-project/setuptools.html
不幸的是,python打包有着复杂的历史,新的工具仍在出现。我的理解是,当前的"黄金标准"是使用
以下是我作为起点使用的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/usr/bin/env python """ boilerplate for new project setup.py""" from setuptools import setup import io import projectname def read(*filenames, **kwargs): encoding = kwargs.get('encoding', 'utf-8') sep = kwargs.get('sep', ' ') buf = [] for filename in filenames: with io.open(filename, encoding=encoding) as f: buf.append(f.read()) return sep.join(buf) long_description = read('README.md') #, 'CHANGES.txt') setup(name='projectname', version=projectname.__version__, description='short desc of projectname', long_description=long_description, author='Tylar Murray', author_email='[email protected]', url='https://github.com/7yl4r/projectname', tests_require=['nose'], install_requires=[ 'networkx' # or whatever ], #cmdclass={'test': PyTest}, packages=['projectname', 'OtherProjectProvidedPackage2'] ) |
注意:此模板要求您在顶级
关于您的P.S.问题:从技术上讲,您应该为此打开一个新的问题,但简言之,答案是您应该包括这里和这个答案中特别包括的内容。