c++python-3.xdependency-managementlibtorrentlibtorrent-rasterbar

Why installed libtorrent shows Import Error?


I have built libtorrent with boost with this commands in the boost root folder :

bootstrap.bat
b2 --hash cxxstd=14 release

and after I have added BOOST_ROOT and BOOST_BUILD_PATH to PATH variable.

I also have downloaded OpenSSL and build it then have copied to Visual studio 15 2017 compiler include and libs folder repectively.

Next in the libtorrent root folder I have run this commands:

b2 variant=release link=shared
b2 install --prefix=build

The build was successful and libtorrent c++ library has created.

and after that I have run these commands :

py setup.py build
py setup.py install

They executed with no errors and libtorrent installed in my python libs/site-packages folder. But when I import it this error shows:

Python Import Error [enter image description here]

What build steps might I have done wrong?

Os : Windows 10 x64

Python : 3.9.5 x64

Libtorrent : 2.0.5

Boost : 1.78.0

I have followed from the libtorrent docs : https://libtorrent.org/building.html and https://www.libtorrent.org/python_binding.html


Solution

  • I found the answer.

    While building libtorrent python binding 2 factors are important:

    1- openSSL version 2- linking type

    1. python comes with openssl v.1.1 (or similar based on python version) , if building python binding with openssl v.1.1 (which is the latest version while I am writing) one dependency solved otherwise, if using openssl v.3 for building 2 dependency must add to python which they are:
    // 32 or 64 bits library based on openssl build
    
    libssl-3-x64.dll
    libcrypto-3-x64.dll
    

    2 ) in the time of building python binding 2 commands can be use:

    a ) simple with default parameters :

    py setup.py build
    py setup.py install
    

    In this case in default libtorrent and boost-python linking static.

    b ) complex one with more control (I think) :

    py setup.py build_ext --b2-args="VARS" install
    

    In the VARS place we can write boost build options but these are the one we want:

    libtorrent-link=TYPE  boost-link= TYPE
    

    TYPE can be static or shared but anyone that sets shared , it becomes dependency. two files which is in need are :

    // 32 and 64 bits file may have different name
    // files can have different names but they are similar to below
    
    torrent-rastarbar.dll
    boost_python(PYTHON-VERSION)(SOME-INFO).dll
    

    boost python can be find in the boost root directory in the stage/lib .

    pleae note that you must build boost and libtorrent SHARED for this solution.

    Conclusion :

    as mentioned above these dependency must add to based on the build setting you did:

    1 - OpenSSL libraries 2 - Boost python 3 - libtorrent libraries

    There is an optional file that mentioned in some forums and discussion msvcr90.dll which does not effect on my project but good to point.

    Put those files to a directory which can be find by python interpreter or put in project your folder and add this piece of code before imporing libtorrent :

    import os
    
    current_path = os.path.abspath(".")
    
    # do not pass relative path like ".", pass full path
    
    os.add_dll_directory(current_path)
    
    

    Sorry for any poor english. :)