【问题标题】:pip-tools doesn't install dependencies to activated virtualenvpip-tools 不会将依赖项安装到激活的 virtualenv
【发布时间】:2021-10-31 13:47:22
【问题描述】:

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

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

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

鉴于我的环境(即使用 pipx 安装的 pip-tools,由 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

【问题讨论】:

    标签: python virtualenv pyenv pip-tools pipx


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这是带有 pip-tools 的 known issue

      您必须在项目的虚拟环境中安装 pip-tools(而不是使用 pipx 全局安装),或者如果使用 pipx 安装,请使用以下解决方法来“模拟”pip-sync:

      # Remove all dependencies
      pip freeze | xargs pip uninstall -y
      
      # Reinstall the dependencies
      pip install -r requirements.txt
      pip install -r requirements-dev.txt
      

      注意:当 pip-tools 与 pipx 一起安装时,pip-compile 可以正常工作。

      【讨论】:

        猜你喜欢
        • 2021-06-19
        • 2018-09-27
        • 2016-08-21
        • 2016-08-26
        • 2013-05-03
        • 2021-04-20
        • 2011-01-20
        • 2013-11-20
        • 2017-02-17
        相关资源
        最近更新 更多