36

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?

Jon Clements
  • 138,671
  • 33
  • 247
  • 280

5 Answers5

33

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.

MrColes
  • 2,453
  • 1
  • 28
  • 38
9

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.

Chris
  • 2,166
  • 1
  • 24
  • 37
4

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.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • 3
    Recent versions of virtualenv deprecate --no-site-packages, since it's the new default. – Colin Dunklau Jul 11 '12 at 22:02
  • 2
    Thx for your answer. But I mean thouse case when we are can't use pip install - just have a repository in .deb format - what I should to do then? – Александр Кривошеев Jul 12 '12 at 17:06
  • @АлександрКривошеев Well, if you can get the source from the package, then use `setup.py` to install it, or find the appropriate equivalent on pypi or github etc... Otherwise - there's not a lot you can do... – Jon Clements Jul 12 '12 at 17:11
  • 2
    Sometimes we are have no possibility to find setup.py and $ pip install dbus-python in this case makes [error 2] we are have no setup.py :[ – Александр Кривошеев Jul 12 '12 at 17:36
  • Just to check, you are using `pip` in the bin directory of your virtualenv, not the system wide `pip`? If you don't have `setup.py`'s, then you have to arrange directories by hand - not something you want to do if you can avoid it... – Jon Clements Jul 12 '12 at 17:46
  • No, of course it was activated virtualenv like ---(smth_v_env) $ pip install dbus-python--- and after pip was download this one I can just find in it's directory only ./config file - to compile it manually. May be I'm wrong - not environment :) – Александр Кривошеев Jul 13 '12 at 18:09
4

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
Joshua Kan
  • 353
  • 2
  • 7
3

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
SmileyChris
  • 10,578
  • 4
  • 40
  • 33