Setuptools“开发”需求

TL; DR : 在运行 python setup.py develop时,是否有办法钩住 setuptool 的“开发”以安装一组开发需求?

我正在使用 setuptools 构建我的第一个 python 包:

requirements = [
'click',
'ansible',
'fabric',
'gitpython',
'pyyaml',
'jinja2',
'yapsy'
]


test_requirements = [
'pytest',
'pytest-pep8',
'pytest-cov',
]


setup(
...
install_requires=requirements,
tests_require=test_requirements,
...
)

在开发过程中,我一直使用以下方法安装包(在虚拟环境中) :

python setup.py develop

和卸载:

python setup.py develop -u

该软件包使用 entry _ points 来安装一些命令行脚本,因此这为我设置了命令,并允许我在测试命令的同时编辑该软件包。

我还有一些用于开发的依赖项... sphinx + 扩展和其他一些东西(使用包不需要的东西)。我现在只是在虚拟环境中手动安装它们。我没有看到任何关于如何将它们与 setuptools 连接起来的文档(在 Google 上也没有找到任何示例)。

也许有一种方法可以钩住“ setup.py development”来安装一组额外的需求?又一个我没读过的方法?

28905 次浏览

For more info on using setup.py vs requirements.txt, I found this article helpful.

Update: September 2016

I no longer use requirements.txt (see original answer below) for installing development only packages. The prevailing wisdom seems to be that requirements.txt should be used to pin deployments to specific version numbers, typically using pip freeze > requirements.txt. This ensures that the exact same versions of your project's dependencies and also your project's dependencies' dependencies are installed on all of your servers.

I instead use the extras_require option to setup.

requirements = [
'click',
'ansible',
'fabric',
'gitpython',
'pyyaml',
'jinja2',
'yapsy'
]


setup({
install_requires=requirements,
extras_require={
'dev': [
'pytest',
'pytest-pep8',
'pytest-cov'
]
}
})

Now, to install your package for development, you run pip install -e .[dev]. This installs all the regular required packages and those listed in the dev section of extras_require.

Production installs can still be done with python setup.py install or pip install . (or with a requirements.txt file).

Original Answer

Here is a way to do it that seems to be in keeping with the recommendations I've run into regarding setup.py vs requirements.txt. Specify all your production dependencies in the install_requires parameter of setup.py.

requirements = [
'click',
'ansible',
'fabric',
'gitpython',
'pyyaml',
'jinja2',
'yapsy'
]


setup({
# ...
install_requires=requirements
# ...
})

Then create a requirements.txt file that instructs pip to install your production dependencies from setup.py as well as your testing dependencies.

-e .


pytest
pytest-pep8
pytest-cov

Now you can install your package for development with pip install -r requirements.txt. The -e . line will install your package and its dependencies from setup.py in development mode. To install on production, you could use python setup.py install or pip install .. This will only install the dependencies listed in setup.py.