Why installed libtorrent shows Import Error?

882 views Asked by At

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

2

There are 2 answers

0
S.A.H On BEST ANSWER

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. :)

3
Luuk On

What did I do ?

Basically following: https://github.com/arvidn/libtorrent/blob/master/docs/building.rst#downloading-and-building

Unzipping boost_1_78_0.zip into D:\boost_1_78_0, and running:

set BOOST_ROOT=D:\boost_1_78_0
set BOOST_BUILD_PATH=%BOOST_ROOT%\tools\build
(cd %BOOST_ROOT% && .\bootstrap.bat)
echo using msvc ; >>%HOMEDRIVE%%HOMEPATH%\user-config.jam
%BOOST_ROOT%\b2.exe --hash release

After this i got:

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

D:\boost_1_78_0

The following directory should be added to linker library paths:

D:\boost_1_78_0\stage\lib

I think this is where i started to fail, I did not read this the first time, and now I am asking meself where/how should the "compiler include paths" and "linker library paths" be set .?

When trying to compile libtorrent, using the command-line you provided (b2 msvc-14.2 variant=release link=static runtime-link=static debug-symbols=on), i got:

CXXFLAGS =
LDFLAGS =
OS = NT
building boost from source directory:  D:/boost_1_78_0
Performing configuration checks

    - default address-model    : 64-bit (cached) [1]
    - default architecture     : x86 (cached) [1]

[1] msvc-14.2
...patience...
...patience...
...patience...
...found 3888 targets...
...updating 78 targets...
compile-c-c++ bin\msvc-14.2\release\cxxstd-14-iso\debug-symbols-on\link-static\runtime-link-static\threading-multi\src\hasher.obj
hasher.cpp
D:\TEMP\libtorrent\libtorrent\include\libtorrent/hasher.hpp(66): fatal error C1083: Cannot open include file: 'openssl/sha.h': No such file or directory

    call "bin\standalone\msvc\msvc-14.2\msvc-setup.bat"  >nul
 cl /Zm800 -nologo "src\hasher.cpp" -c -Fo"bin\msvc-14.2\release\cxxstd-14-iso\debug-symbols-on\link-static\runtime-link-static\threading-multi\src\hasher.obj"     -TP /bigobj /wd4251 /wd4268 /wd4275 /wd4373 /wd4503 /wd4675 /EHs /std:c++14 /GR /Zc:throwingNew /O2 /Z7 /Ob2 /W4 /MT /Zc:forScope /Zc:wchar_t /Zc:inline /Gw /favor:blend -DBOOST_ALL_NO_LIB -DBOOST_ASIO_ENABLE_CANCELIO -DBOOST_ASIO_HAS_STD_CHRONO -DBOOST_ASIO_NO_DEPRECATED -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_NO_DEPRECATED -DBOOST_SYSTEM_NO_DEPRECATED -DBOOST_SYSTEM_STATIC_LINK=1 -DNDEBUG -DOPENSSL_NO_SSL2 -DTORRENT_BUILDING_LIBRARY -DTORRENT_SSL_PEERS -DTORRENT_USE_I2P=1 -DTORRENT_USE_LIBCRYPTO -DTORRENT_USE_OPENSSL -DWIN32 -DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_DEPRECATE -D_FILE_OFFSET_BITS=64 -D_SCL_SECURE_NO_DEPRECATE -D_WIN32 -D_WIN32_WINNT=0x0600 -D__USE_W32_SOCKETS "-ID:\boost_1_78_0" "-Ideps\try_signal" "-Iinclude" "-Iinclude\libtorrent" 

...failed compile-c-c++ bin\msvc-14.2\release\cxxstd-14-iso\debug-symbols-on\link-static\runtime-link-static\threading-multi\src\hasher.obj...
compile-c-c++ bin\msvc-14.2\release\cxxstd-14-iso\debug-symbols-on\link-static\runtime-link-static\threading-multi\src\merkle.obj
merkle.cpp
D:\TEMP\libtorrent\libtorrent\include\libtorrent/hasher.hpp(66): fatal error C1083: Cannot open include file: 'openssl/sha.h': No such file or directory
..... (rest of logging removed)