【问题标题】:Does libtorrent work in Python 3?libtorrent 在 Python 3 中工作吗?
【发布时间】:2017-11-19 00:46:45
【问题描述】:

在过去的几天里,我尝试了所有方法来在 Python3 中安装 libtorrent。无论我尝试什么,我都会得到:

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'

我运行时没有错误:

$ sudo apt-get install python3-libtorrent
$ sudo apt-get install libtorrent-rasterbar-dev

我不太了解 libtorrent 和 libtorrent-rasterbar 之间的区别。据我所知,rasterbar 是 libtorrent 的依赖项。

PyPi page 上没有任何二进制文件,所以我不能pip install 它。

我可以从the GitHub page 下载软件包并通过运行以这种方式安装:

$ python setup.py build
$ python setup.py install

创建:

/home/<user>/anaconda3/lib/python3.6/site-packages/python_libtorrent-1.1.5-py3.6.egg-info

但仍然没有变化。这可能与我的 Python3 安装不在默认位置有关,但我不确定在安装 libtorrent 方面如何处理。

我还发现了this 类似的 SO 问题,其中接受的答案是明确的否。然而,最近有更多的 cmets 表示,从那时起,它已经成为可能,只是没有给出如何实现它的说明。

我已经完全没有想法了,无论多么荒谬,我都愿意接受任何建议。现在,我正在查看从 GitHub 页面获得的包,看看是否可以以某种方式将代码复制到我的项目目录中,然后将其作为本地模块导入,但到目前为止我运气不佳。如果这不起作用,我将考虑为原始 C++ libtorrent 库编写我自己的 Python3 包装器。但是,我在某处读到其他人在这方面尝试过但失败了,所以我并不期待成功。

编辑:

好的,我已经走得更远了。我以某种方式设法下载了一个没有配置文件或任何 makefile 的 repo 版本。现在我选对了,我运行:

$ ./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

我用谷歌搜索了这个,显然 PyClass_Type 没有在 Python3 中定义。所以它似乎仍在尝试为 Python2 构建自己,这解释了 ImportError 中的libboost_python-py27.so。建议的解决方案是使用-lboost_python3 而不是-lboost_python 编译C++ 库。 Makefile的第268行是BOOST_PYTHON_LIB = -lboost_python所以我把它改成了BOOST_PYTHON_LIB = -lboost_python3并重新运行:

$ make
$ python setup.py build
$ python setup.py 

(我无法重新运行 $ ./configure --enable-python-binding,因为它会将 Makefile 恢复为再次使用 lboost-python)。不幸的是,我仍然收到相同的错误消息:undefined symbol: PyClass_Type。我对 C++ 几乎一无所知,所以我不确定我是否遗漏了关于编译库的一些非常明显的东西。

【问题讨论】:

  • libboost_python-py27.so.1.62.0 中的“27”指的是python 2.7版本。您还需要为 python 3.x 构建 boost-python
  • 感谢@Arvid 的回复。我从boost.org 下载了 boost 并根据此处的说明构建它:eb2.co/blog/2012/03/building-boost.python-for-python-3.2 project-config.jam 将我的 python 路径列为using python : 3.6 : /home/&lt;user&gt;/anaconda3 ; 所以我认为我不需要更改它。然后我重新运行命令来构建 libtorrent。不幸的是,我仍然遇到同样的错误。
  • 顺便说一句,你是 libtorrent 开发者,对吧?在我进行故障排除时,我看到了很多关于 libtorrent 的 SO 问题的帖子。非常感谢您为 torrent 相关内容构建了如此完整的库。很高兴有一个开发人员在他们建成后继续帮助他们。更不用说如果我能让它工作,不管用多少周来破解一个更糟糕的版本,你都会拯救我
  • @Arvid 我正在尝试使用dreamingpotato.com/2015/11/21/… 提供的步骤在 conda env 中安装 libtorrent。但是我在原始问题中遇到了同样的问题。你们能提供在 conda virtualenv 中在 python 3.3 上设置 libboost 的步骤吗?我按照这篇文章gist.github.com/melvincabatuan/a5a4a10b15ef31a5a481 中的步骤操作并添加了 conda virtualenv python 3.3 路径。但仍然遇到同样的错误。
  • @VaibhavBhavsar 据我所知,无法在 conda 中设置 libtorrent。我试过没有成功。最好的办法是使用默认的 python 安装,但你也可能会遇到 virtualenv 的问题。

标签: linux python-3.x ubuntu libtorrent


【解决方案1】:

好的,我已经解决了。事实证明我有两个单独的 Python3 安装。默认一个,Anaconda 一个。此外,当我安装 Anaconda 时,我让它自己添加到我的 $PATH 环境变量中。我没有注意到我没有使用默认安装,因为我仍然能够通过 pip 等安装软件包。基本上,纯 Python 的东西运行良好。但是,由于 libtorrent 是 C++ 库之上的 Python 包装器,因此运行 $ ./configure --enable-python-binding 会将所有 Python 位构建到我的 Anaconda Python 安装中,并将所有 C++ 位构建到我的默认 Python 安装中(或类似的东西,尽我所能告诉)。

解决方案是卸载 Anaconda:$ sudo rm -rf ~/anaconda3,并删除在我的 .bashrc 中设置的 $PATH 变量 Anaconda。为了更好地衡量,我还使用了 $ sudo apt-get remove'd 和 $ sudo apt-get purge'd 我拥有的每个库安装与 libtorrent 有任何关系。我不记得所有这些,但肯定包括python3-libtorrentpython-libtorrentpython3-libboostpython-libboost。一旦我这样做了,一个简单的$ sudo apt-get install python3-libtorrent 就让它完美地工作了。

@Arvid,顺便说一句,对我来说,至少 python example 中打印语句的最后一行需要更改为:

s.num_peers, state_str[s.state])

简单地说:

s.num_peers, s.state)

因为s.state 持有一个表示状态的字符串,而不是需要映射到state_str 列表的整数。不过,这可能只是 Python3 处理状态的结果;我没有在 Python2 中测试过。

【讨论】:

  • 我遇到了类似的问题:在 python3.6 或 python 3.8 上不起作用,但在 python3 上起作用:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
相关资源
最近更新 更多