piptools不会将依赖项安装到激活的virtualenv

2024-04-28 19:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用pip工具将我的需求文件分为开发(requirements-dev.txt)和生产(requirements.txt)

我已经使用pipx安装了pip工具,因为我希望它可以在全球范围内使用,但需要隔离。但是,这样做会导致我的依赖项由pip工具安装在pip工具本身的虚拟环境中,而不是激活的虚拟环境中

我不知道这是否是一个因素,但我也在使用pyenv来管理我的python版本,但在全局范围内只安装了一个(非系统)版本

考虑到我的环境(即随pipx一起安装的pip工具,由pyenv管理的python),我如何让pip-sync激活的虚拟环境中安装依赖项

以下是我的工作流程,以重现此内容:

# Install pip-tools globally
pipx install pip-tools

# Create a virtual environment and activate it
python -m venv venv
source venv/bin/activate

# Create prod/dev requirement input files (see below for content)

# Autogenerate requirement files
pip-compile requirements.in
pip-compile requirements-dev.in

# Install all dependencies
pip-sync requirements.txt requirements-dev.txt

# Check what is installed (outputs nothing)
pip freeze

# Check what is installed in pip-tools virtual env
~/.local/pipx/venvs/pip-tools/bin/python -m pip freeze

# output shows flask, pytest, and their dependencies

生产依赖文件

# requirements.in
flask

开发依赖文件

# requirements-dev.in
-c requirements.txt
pytest

Tags: installpip文件工具indev版本txt
2条回答

您可以使用 python-executable选项(introduced在6.2.0中)在任何环境中安装软件包:

pip-sync  python-executable venv/bin/python requirements.txt requirements-dev.txt

这是一个带有pip工具的known issue

您必须在项目的虚拟环境中安装pip工具(而不是使用pipx全局安装),或者如果使用pipx安装,请使用以下变通方法“模拟”pip同步:

# Remove all dependencies
pip freeze | xargs pip uninstall -y

# Reinstall the dependencies
pip install -r requirements.txt
pip install -r requirements-dev.txt

注意:当pip工具与pipx一起安装时,pip编译不会出现问题

相关问题 更多 >