pip-tools doesn't install dependencies to activated virtualenv

2.5k views Asked by At

I wish to separate my requirement files into development (requirements-dev.txt) and production (requirements.txt) using pip-tools.

I have installed pip-tools using pipx as I want it to be globally available but isolated. However, doing so causes my dependencies to be installed by pip-tools within the virtual environment of pip-tools itself, rather than the activated virtual environment.

I don't know if it is a factor, but I am also using pyenv to manage my python versions, but only have one (non-system) version installed globally.

Given my environment (i.e. pip-tools installed with pipx, python managed by pyenv), how do I get pip-sync to install the dependencies within the activated virtual environment?

Here is my workflow to reproduce this:

# 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

Production dependency file

# requirements.in
flask

Development dependency file

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

There are 2 answers

0
Albert Tugushev On

You can install packages in any environment using --python-executable option (introduced in 6.2.0):

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

This is a known issue with pip-tools.

You either have to install pip-tools within your project's virtual environment (rather than globally with pipx), or if installed using pipx, use the following workaround to "simulate" 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

Note: pip-compile works without issue when pip-tools is installed with pipx.