如何打包分发python程序(.py源码),让其他开发者可以轻松安装所有需要的依赖?
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。
那么我如何分发我的程序,以便其他人可以轻松地安装每个需要的程序 dependency/package 并使其在每台计算机上都能正常工作? setup.py 是我要找的还是根本不是?如果是这样,您能否解释一下它的作用并提供一个示例 setup.py 来完成我正在尝试做的事情?
PS:我也有这个小问题:我需要在程序的顶层文件夹中还是仅在子目录中提供 __init__.py?
根据文档here
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.
同时检查这个:
What is setup.py?
您可以查看此示例以了解如何制作您的示例:
https://github.com/pypa/sampleproject/blob/master/setup.py
此外,这里有一个指南:
https://pythonhosted.org/an_example_pypi_project/setuptools.html
here 是 pandas 的设置文件的 link,您可以在其中查看它们如何执行依赖项检查,它们可能是特定于平台的或任何第三方包具体
在不久以前,我使用 this guide to learn how to package and distribute my python code, then some good people created flit,这让我可以分三步完成整个过程。
$pip install flit
创建我的元数据文件:
[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
发布我的包:
$pip flit publish
大功告成!!!
不幸的是,python 包装有 a complicated history, and new tools are still emerging. My understanding is that the current "gold standard" is to use setup_tools
to define a setup.py
file. This file will allow others to install your project's source code using pip. If you want to be able to install with pip without explicitly downloading the source first, you will need to publish your project to pypi 使用 python setup.py upload
。
下面是我用作起点的 setup.py
样板文件:
#!/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', '\n')
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='code+projectname@tylar.info',
url='https://github.com/7yl4r/projectname',
tests_require=['nose'],
install_requires=[
'networkx' # or whatever
],
#cmdclass={'test': PyTest},
packages=['projectname', 'OtherProjectProvidedPackage2']
)
注意:此模板要求您在顶级 __init__.py
中有一个 README.md
和类似 __version__="0.1.23"
的内容
关于你的P.S。问题:从技术上讲,您应该为此打开一个新问题,但简而言之,答案是您应该将两者都包括在内 and in this answer particularly。
我正在开发一个 Python 应用程序,该应用程序使用我用 pip 安装的一些软件包构建,例如 Flask、requests、PIL。
那么我如何分发我的程序,以便其他人可以轻松地安装每个需要的程序 dependency/package 并使其在每台计算机上都能正常工作? setup.py 是我要找的还是根本不是?如果是这样,您能否解释一下它的作用并提供一个示例 setup.py 来完成我正在尝试做的事情?
PS:我也有这个小问题:我需要在程序的顶层文件夹中还是仅在子目录中提供 __init__.py?
根据文档here
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.
同时检查这个:
What is setup.py?
您可以查看此示例以了解如何制作您的示例:
https://github.com/pypa/sampleproject/blob/master/setup.py
此外,这里有一个指南:
https://pythonhosted.org/an_example_pypi_project/setuptools.html
here 是 pandas 的设置文件的 link,您可以在其中查看它们如何执行依赖项检查,它们可能是特定于平台的或任何第三方包具体
在不久以前,我使用 this guide to learn how to package and distribute my python code, then some good people created flit,这让我可以分三步完成整个过程。
$pip install flit
创建我的元数据文件:
[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
发布我的包:
$pip flit publish
大功告成!!!
不幸的是,python 包装有 a complicated history, and new tools are still emerging. My understanding is that the current "gold standard" is to use setup_tools
to define a setup.py
file. This file will allow others to install your project's source code using pip. If you want to be able to install with pip without explicitly downloading the source first, you will need to publish your project to pypi 使用 python setup.py upload
。
下面是我用作起点的 setup.py
样板文件:
#!/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', '\n')
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='code+projectname@tylar.info',
url='https://github.com/7yl4r/projectname',
tests_require=['nose'],
install_requires=[
'networkx' # or whatever
],
#cmdclass={'test': PyTest},
packages=['projectname', 'OtherProjectProvidedPackage2']
)
注意:此模板要求您在顶级 __init__.py
README.md
和类似 __version__="0.1.23"
的内容
关于你的P.S。问题:从技术上讲,您应该为此打开一个新问题,但简而言之,答案是您应该将两者都包括在内