Install packages into virtual environment with apt-get

Viewed 2087

I'm trying to create a virtual environment for my Python3 project. The problem is, some of the dependencies I'm trying to install into the virtualenv aren't through pip. For example, to get LibTorrent, I had to run: $ sudo apt-get install python3-libtorrent (LibTorrent is a C++ library with Python bindings). Outside of the environment, my project runs fine. But inside I get an import error:

(env) me@Comp:~/Projects/test$ python3 main.py 
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    import libtorrent as lt
ModuleNotFoundError: No module named 'libtorrent'

If I run $ sudo apt-get install python3-libtorrent inside the environment, it tells me that it's already installed:

(env) me@Comp:~/Projects/test$ sudo apt-get install python3-libtorrent
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-libtorrent is already the newest version (1.1.1-1build2).
0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.

My understanding is this is because apt-get is a global command and has nothing to do with the environment. But if this is the case, how do I install this package into my env?

1 Answers

Did you manage to solve your issue? I'm having the same issue, and I stumbled upon this: http://dreamingpotato.com/2015/11/21/how-to-install-python-libtorrent-in-virtualenv/

(reproducing commands below in case the link breaks one day)

sudo apt-get build-dep python-libtorrent
wget http://downloads.sourceforge.net/project/libtorrent/libtorrent/libtorrent-rasterbar-1.0.5.tar.gz
tar -zxvf libtorrent-rasterbar-1.0.5.tar.gz
cd libtorrent-rasterbar-1.0.5/
./configure --enable-python-binding PYTHON=`which python` --prefix=$VIRTUAL_ENV
make
make install
export LD_LIBRARY_PATH="$VIRTUAL_ENV/lib"

My guess is that the issue is the following:

  1. python3-libtorrent and python-libtorrent are just python bindings/wrappers to a C++ library, as you said.
  2. On this link, there is a command which compiles lib-torrent according to the python path you are using: ./configure --enable-python-binding PYTHON=`which python` --prefix=$VIRTUAL_ENV which changes when you create a virtual env.

So in theory, you would have to compile libtorrent for every single virtualenv you use. That's a terrible solution, but I believe will be the only one that works. Way worst than a simple pip install -r requirements.txt.

Let me know if that works out for you, and consider marking this as the correct answer.

Related