Reputation: 2200
I've tried everything over the last few days to install libtorrent in Python3. No matter what I try, I get:
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import libtorrent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'libtorrent'
I get no errors when I run:
$ sudo apt-get install python3-libtorrent
$ sudo apt-get install libtorrent-rasterbar-dev
I don't really understand the difference between libtorrent, and libtorrent-rasterbar. As far as I can tell rasterbar is a dependency for libtorrent.
The PyPi page doesn't have any binaries on it, so I can't pip install
it.
I can download the package from the GitHub page and install it that way by running:
$ python setup.py build
$ python setup.py install
Which creates:
/home/<user>/anaconda3/lib/python3.6/site-packages/python_libtorrent-1.1.5-py3.6.egg-info
But still no change. It may have something to do with my Python3 install not being in a default location, but I'm not sure how to handle that in terms of installing libtorrent.
I also found this similar SO question where the accepted answer is a definitive NO. However, there are more recent comments that say that, since then, it has become possible, except no instructions are given on how to achieve it.
I am thoroughly running out of ideas and am open to any suggestions no matter how ridiculous. Right now, I'm looking through the package I got from the GitHub page to see if I can somehow copy the code into my project directory, and then import it as a local module, but I'm not having much luck so far. If that doesn't work, I'll look at writing my own Python3 wrapper for the original C++ libtorrent library. However, I read somewhere that someone else tried and failed at this, so I'm not expecting success.
EDIT:
Okay, I've gotten a little further. I somehow managed to download a version of the repo that did not have a configure file, or any of the makefiles. Now that I have the right one, I run:
$ ./configure --enable-python-bindings
$ make
$ python setup.py build
$ python setup.py install
$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import libtorrent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.62.0: undefined symbol: PyClass_Type
I Googled this, and apparently PyClass_Type is not defined in Python3. So it seems that it's still trying to build itself for Python2, which explains the libboost_python-py27.so
in the ImportError. The suggested solution was to compile the C++ libraries with -lboost_python3
instead of -lboost_python
. Line 268 of the Makefile is BOOST_PYTHON_LIB = -lboost_python
so I changed it to BOOST_PYTHON_LIB = -lboost_python3
and re-ran:
$ make
$ python setup.py build
$ python setup.py
(I can't re-run $ ./configure --enable-python-binding
because it reverts the Makefile to use lboost-python again). Unfortunately, I still get the same error message: undefined symbol: PyClass_Type
. I know next to nothing about C++, so I'm not sure if I'm missing something really obvious about compiling the library.
Upvotes: 2
Views: 9134
Reputation: 2200
Okay, I have solved it. It turns out I had two separate installations of Python3. The default one, and the Anaconda one. Furthermore, when I installed Anaconda, I let it add itself to my $PATH
environment variable. I hadn't noticed I wasn't using the default install because I was still able to install packages through pip etc. Basically, stuff that was pure Python worked fine. However, since libtorrent is a Python wrapper on top of a C++ library, running $ ./configure --enable-python-binding
built all the Python bits to my Anaconda Python install, and all the C++ bits to my default Python install (or something like that, from what I can tell).
The solution was to uninstall Anaconda: $ sudo rm -rf ~/anaconda3
, and delete the $PATH
variable Anaconda set in my .bashrc
.For good measure I also, $ sudo apt-get remove
'd and $ sudo apt-get purge
'd every library I had installed that had anything to do with libtorrent. I can't remember all of them, but it definitely included python3-libtorrent
, python-libtorrent
, python3-libboost
, and python-libboost
. Once I'd done that, a simple $ sudo apt-get install python3-libtorrent
got it working perfectly.
@Arvid, Just as a side note, for me at least the last line of the print statement in the python example needed to be changed from:
s.num_peers, state_str[s.state])
to simply:
s.num_peers, s.state)
since s.state
held a string representing the state, not an integer that needed to be mapped to the state_str
list. That could be just a consequence of how Python3 handles states though; I haven't tested it in Python2.
Upvotes: 3