问题描述
TL;DR:有没有办法在运行 python setup.py develop
时挂钩 setuptool 的develop"来安装一组开发要求?
TL;DR: Is there a way to hook setuptool's 'develop' to install a set of development requirements when running python setup.py develop
?
我正在使用 setuptools 构建我的第一个 python 包.我将要求指定为:
I'm building my first python package using setuptools. I'm specifying the requirements as:
requirements = [
'click',
'ansible',
'fabric',
'gitpython',
'pyyaml',
'jinja2',
'yapsy'
]
test_requirements = [
'pytest',
'pytest-pep8',
'pytest-cov',
]
setup(
...
install_requires=requirements,
tests_require=test_requirements,
...
)
在开发过程中,我一直在安装软件包(在虚拟环境中):
During development, I've been installing the package (in a virtual environment) with:
python setup.py develop
和卸载:
python setup.py develop -u
该包使用 entry_points 来安装一些命令行脚本,因此这为我设置了命令,并允许我在测试命令的同时编辑包.
The package uses entry_points to install some command line scripts, so this sets up the commands for me and allows me to edit the package while testing the command at the same time.
我还有一些用于开发的依赖项……sphinx + 扩展和其他一些东西(使用包不需要的东西).我目前只是在虚拟环境中手动安装它们.我没有看到任何关于如何使用 setuptools 连接它们的文档(也没有在谷歌上找到任何示例).
I also have some dependencies that I use for development ... sphinx + extensions and a couple other things (things that aren't needed to use the package). I'm just manually installing them in the virtual environment at the moment. I didn't see any documentation (and haven't found any examples on the googles) about how to wire them in with setuptools.
也许有一种方法可以挂钩setup.py develop"来安装一组额外的要求?我还没有读过的另一种方法?
Maybe there's a way to hook 'setup.py develop' to install an additional set of requirements? Another method I haven't read about?
推荐答案
有关使用 setup.py
与 requirements.txt
的更多信息,我找到了 这篇文章很有帮助.
For more info on using setup.py
vs requirements.txt
, I found this article helpful.
我不再使用 requirements.txt
(请参阅下面的原始答案)来安装仅用于开发的软件包.普遍的看法似乎是 requirements.txt
应该用于将部署固定到特定的版本号,通常使用 pip freeze >要求.txt
.这可确保在您的所有服务器上安装完全相同版本的项目依赖项以及项目依赖项的依赖项.
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.
我改为使用 extras_require
选项来setup
.
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'
]
}
})
现在,要安装用于开发的软件包,请运行 pip install -e .[dev]
.这将安装所有常规必需的软件包和extras_require
的 dev
部分中列出的那些.
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
.
仍然可以使用 python setup.py install
或 pip install .
(或使用 requirements.txt
文件)完成生产安装.
Production installs can still be done with python setup.py install
or pip install .
(or with a requirements.txt
file).
这是一种的方法,它似乎符合我遇到的关于setup.py
vs requirements.txt.在
setup.py
的 install_requires
参数中指定所有生产依赖项.
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
# ...
})
然后创建一个 requirements.txt
文件,该文件指示 pip 从 setup.py
安装您的生产依赖项以及您的测试依赖项.
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
现在您可以使用 pip install -r requirements.txt
安装用于开发的包.-e .
行将在开发模式下从 setup.py
安装您的包及其依赖项.要在生产环境中安装,您可以使用 python setup.py install
或 pip install .
.这只会安装 setup.py
中列出的依赖项.
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
.
这篇关于设置工具“开发"要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!