16

According to this answer, apt-get installs packages system-wide (from canonical), and only pip can install packages into a virtualenv (from Pypi).

However, I need to install a package into a virtualenv that is only accessible via apt-get, i.e. is not hosted by Pypi.

How do I do that?

  • Nice question Pal. – rɑːdʒɑ Feb 4 '15 at 16:16
8

Another alternative is to download the file with apt-get download, extract it with dpkg -x and to move the extracted files to your virtualenv:

Example:

apt-get download python-xyz

assume this results in:

./python-xyz_1.2.3-3ubuntu5_amd64.deb

Then, use dpkg to extract the data portion of the .deb file:

dpkg -x python-xyz_1.2.3-3ubuntu5_amd64.deb ./xyz-tmp

In the newly created ./xyz-tmp directory, you now have all files that would have been installed to / (root) had you used apt-get install.

Assume they are:

./xyz-tmp/usr/lib/python2.7/dist-packages/xyz/
./xyz-tmp/usr/lib/python2.7/dist-packages/xyz-1.2.3.egg-info

You still need to map the subdirectories (e.g. dist-packages vs. site-packages), and can then move the files into your virtualenv (assuming a virtualenv /home/user/.virtualenvs/py27):

mv ./xyz-tmp/usr/lib/python2.7/dist-packages/* /home/user/.virtualenvs/py27/lib/python2.7/site-packages/

Compared to my previous answer, this approach has the advantage that you don't have to research which files got installed, because you can see them in the extraction directory. It still requires a mapping of directories between the extracted archive and the virtualenv.

| improve this answer | |
  • +1, here's another example of this approach, which works quite well to install packages that are only accessible via APT into a virtualenv - did something similar recently using Python 3.7. – RichVel Sep 20 '19 at 7:13
0

All that is needed for a virtualenv is the presence of the package files in the subtree of your virtualenv. So as long as you can determine which files got installed into the system Python via apt-get, you can copy those into your virtualenv. This includes files in the site packages directory as well as in other places within your Python subtree (e.g. scripts).

Hypothetical example:

sudo apt-get python-xyz

Assume it installs a package named "xyz" into your system Python 2.7, you may get this:

/usr/lib/python2.7/dist-packages/xyz/
/usr/lib/python2.7/dist-packages/xyz-1.2.3.egg-info

Copy the files related to the "xyz" package to your virtualenv (assuming a virtualenv /home/user/.virtualenvs/py27):

/home/user/.virtualenvs/py27/lib/python2.7/site-packages/xzy/
/home/user/.virtualenvs/py27/lib/python2.7/site-packages/xzy-1.2.3.egg-info

The difficulty is (1) in determining which files belong to a particular package, and (2) in mapping corresponding subdirectories between the system Python and the virtualenv (e.g. dist-packages vs. site-packages in the example above).

If anyone has more experience with this, please comment.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.