首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pip-tools不向激活的virtualenv安装依赖项

pip-tools不向激活的virtualenv安装依赖项
EN

Stack Overflow用户
提问于 2021-09-02 04:38:27
回答 2查看 1.1K关注 0票数 1

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

我已经安装了pip-工具使用pipx,因为我希望它是全球性的,但孤立的。但是,这样做会使我的依赖项由pip本身的虚拟环境中的pip安装,而不是激活的虚拟环境。

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

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

下面是我的工作流程:

代码语言:javascript
复制
# 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

生产依赖文件

代码语言:javascript
复制
# requirements.in
flask

开发依赖文件

代码语言:javascript
复制
# requirements-dev.in
-c requirements.txt
pytest
EN

回答 2

Stack Overflow用户

发布于 2021-09-03 02:14:54

这是一个带有pip工具的已知问题

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

代码语言:javascript
复制
# Remove all dependencies
pip freeze | xargs pip uninstall -y

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

注:pip-编译工作时,没有问题,当pip-工具安装与pipx。

票数 0
EN

Stack Overflow用户

发布于 2021-09-22 20:04:48

您可以使用--python-executable选项(6.2.0中的介绍)在任何环境中安装包:

代码语言:javascript
复制
pip-sync --python-executable venv/bin/python requirements.txt requirements-dev.txt
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69024242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档