How I can make apt-get install to my virtualenv?

Question:

It it’s possible, of course.

For example – I can download python-dbus like this:
$ sudo apt-get download python-dbus

But what I should to do next, with this .deb package in my current virtualenv?

Answers:

Why would you want to do this? The whole point is to avoid doing stuff like that…

virtualenv whatever
cd whatever
bin/pip install dbus-python

You may also choose to specify --no-site-packages to virtualenv to keep it extra isolated.

Answered By: Jon Clements

If you really need to do it this way, you can just copy the files that get installed globally directly into your virtualenv. For example I couldn’t get pycurl working since the required libraries weren’t installing, but apt-get install python-pycurl did. So I did the following:

sudo apt-get install python-pycurl
cp /usr/lib/python2.7/dist-packages/pycurl* ~/.virtualenvs/myenv/lib/python2.7/site-packages/

The install said it was adding it to /usr/lib/python2.7. So I looked in that directory for a site-packages or dist-packages with pycurl, after looking at the files I copied them into my virtualenv. You’d have to also copy any executables from bin into your virtualenv’s bin directory.

Also, running a pip install -r requirements.txt successfully found pycurl in there and just skipped over it as if I had installed it via pip.

Answered By: MrColes

First install the dbus development libraries (you may need some other dev libraries, but this is all I needed)

sudo apt-get install libdbus-1-dev libdbus-glib-1-dev

Next, with your virtualenv activated, run the following. It’ll fail but that’s ok.

pip install dbus-python

Finally, go into your virtualenv’s build directory and install it the non-pythonic way.

cd $VIRTUAL_ENV/build/dbus-python
chmod +x configure
./configure --prefix=$VIRTUAL_ENV
make
make install
Answered By: SmileyChris

An alternative solution is to install globally, then followed by allowing the virtualenv to be able to see it.
As an example, let’s say we want to install matplotlib for Python 3:

  1. sudo apt update # Update first
  2. sudo apt install python3-matplotlib # Install globally
  3. sudo pip3 install -U virtualenv # Install virtualenv for Python 3 using pip3
  4. virtualenv --system-site-packages -p python3 ./venv #the system-site-packages option allows venv to see all global packages including matplotlib
  5. source ./venv/bin/activate #activate the venv to use matplotlib within the virtualenv
  6. deactivate # don’t exit until you’re done using the virtualenv
Answered By: Joshua Kan

To include system site packages in your existing virtual environment open the config file:
<PATH_TO_YOUR_VENV_FOLDER>/pyvenv.cfg

and change false to true for include-system-site-packages

include-system-site-packages = true

Save and reload your virtual environment.

(tested with virtualenv 20.2.2 on Raspbian GNU/Linux 10 (buster) to pull in python3-pyqt5 installed with apt into my virtual environment)

If it is for a new environment @Joshua Kan’s answer using the --system-site-packages flag with the venv command is probably what you want.

Answered By: Chris
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.