4

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?

Sam
  • 2,172
  • 3
  • 24
  • 43
  • 1
    `apt-get` installs globally, it has nothing to do with the virutal environment. It doesn't matter if you run that command in activated virtualenv or outside. – cezar Nov 27 '17 at 08:43
  • @cezar Then why am I getting an import error only when I run the program inside the virtual environment? – Sam Nov 27 '17 at 08:48
  • It's hard to say what the reason is, but I'd check the `python` version. Which version of python do you have in the virtual environment? If it is already python 3, you should run: `python main.py` from the virtual environment. Use the command `which` to find out which python is executed. – cezar Nov 27 '17 at 13:36
  • What do you get executing `pip freeze`? – Gonzalo Matheu Jan 19 '18 at 02:48

1 Answers1

0

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.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90