user3812335
user3812335

Reputation: 125

Installing libtorrent for Python 3.6 on Windows 7

Windows 7 x64 - Python 3.6

I am trying to install the libtorrent Python library in windows using the instructions here.

After navigating to the setup.py file, I used the following commands

python setup.py build
python setup.py install

In the cmd window, I get the following messages:

C:\Users\thomas\Desktop\libtorrent-master>python setup.py build
running build

C:\Users\thomas\Desktop\libtorrent-master>python setup.py install
running install
running build
running install_egg_info
Removing C:\Users\thomas\AppData\Local\Programs\Python\Python36-32\Lib\site-pac
kages\python_libtorrent-1.2.0-py3.6.egg-info
Writing C:\Users\thomas\AppData\Local\Programs\Python\Python36-32\Lib\site-pack
ages\python_libtorrent-1.2.0-py3.6.egg-info

What else do I need to do? Because trying to import the libtorrent library, the interpreter comes up with this message:

>>> import libtorrent
Traceback (most recent call last):
File , line 1, in
ModuleNotFoundError: No module named 'libtorrent'

No matter what, the proper DLL is not available in the Python folder and thus I can't import the library.

Using the MSI installer from the Sourceforge link doesn't help either since it's severely outdated.

Upvotes: 1

Views: 8178

Answers (1)

Stam Kaly
Stam Kaly

Reputation: 668

If you took a quick look at the setup.py file you are trying to install, you would see that it assumes you have installed the boost C++ libraries in order to generate the libtorrent.pyd required for Python. You would expect to get an error, but that's not how things are right now.


Installing libtorrent for Python without building it

For your own convenience, I have built Python Wheels of libtorrent which can be installed with pip install. Please take into consideration, that if it does not work, it means you will have to build your own .pyd for your machine.


Building and installing libtorrent for Python on Windows 7

In order to get boost working, you will first have to download and install:

  1. Windows 7 SDK and .NET Framework 4

  2. Microsoft Visual C++ 2015 Build Tools

After you are done installing those, you will have to add their directories to your PATH:

  1. Right-Click Computer and go to Properties:

    Right-Click <code>Computer</code> and go to <code>Properties</code>

  2. Click Advanced System Settings at the left:

    Advanced System Settings

  3. Click Environment Variables at the bottom-right:

    Environment Variables

  4. Choose PATH from the top list and click Edit...:

    PATH

  5. Inside the box that popped up add these if you want to be building for 32-bit Python:

    ;C:\Program Files\Microsoft SDKs\Windows\v7.1\Include;C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\;C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\
    

    Or these for 64-bit:

    ;C:\Program Files\Microsoft SDKs\Windows\v7.1\Include;C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\x64;C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\x64;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\
    
  6. Click OK both from the pop up and from the Environment Variables window, and leave the other one opened, we will need it later on..

Now everything is set up and you are ready to install the boost C++ libraries. Because libtorrent's Python bindings have some issues with boost versions higher than 1.63 (in August 2017), make sure to download this one. After you have downloaded it:

  1. Extract it

  2. Open a Command Prompt and cd into the extracted directory

  3. Run bootstrap.bat to install the libraries

After that is done, go to the System Properties window you left open from earlier, and click Environment Variables again. Click on New... and add these:

Variable name: BOOST_ROOT
Variable value: "<full path to extracted directory of boost>"

and click OK to both windows again.

There is one last step before you can actually start building libtorrent, and that is specifying your Python version into a configuration file.

  1. Open a new Command Prompt

  2. Execute echo using python : <Python Version> : "<Python Path>" : "<Python Path>\Include" : "<Python Path>\libs" ; >> user-config.jam

    For example: echo using python : 3.5 : "C:\Program Files\Python35" : "C:\Program Files\Python35\Include" : "C:\Program Files\Python35\libs" ; >> user-config.jam

Now to build libtorrent:

  1. Download and extract the repository

  2. Open <libtorrent extracted directory>\include\libtorrent\session.hpp in a notepad, find the line that starts with std::snprintf, remove std:: and save.

  3. In a Command Prompt, cd into <libtorrent extracted directory>\bindings\python

  4. Now

    if you are building for 32-bit Python execute:

    bjam libtorrent-link=static boost-link=static stage_module

    or

    bjam libtorrent-link=static boost-link=static address-model=64 stage_module for 64-bit

  5. Just be patient, when it finishes you will have a libtorrent.pyd in <libtorrent extracted directory>\bindings\python which you can import inside Python!

Upvotes: 3

Related Questions