使用aptg将包安装到虚拟环境中

2024-06-07 18:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的Python3项目创建一个虚拟环境。问题是,我试图安装到virtualenv中的一些依赖项不是通过pip实现的。例如,要获得LibTorrent,我必须运行:^ {< CD1> }(LyTrOrthon是一个带有Python绑定的C++库)。在环境之外,我的项目运行良好。但在里面我得到了一个导入错误:

(env) me@Comp:~/Projects/test$ python3 main.py 
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    import libtorrent as lt
ModuleNotFoundError: No module named 'libtorrent'

如果我在环境中运行$ sudo apt-get install python3-libtorrent,它会告诉我它已经安装:

^{pr2}$

我的理解是,这是因为apt get是一个全局命令,与环境无关。但如果是这样的话,如何将这个包安装到env中呢?在


Tags: pip项目pyenvget环境virtualenvmain
1条回答
网友
1楼 · 发布于 2024-06-07 18:30:55

你设法解决了你的问题吗?我也有同样的问题,我偶然发现: http://dreamingpotato.com/2015/11/21/how-to-install-python-libtorrent-in-virtualenv/

(复制下面的命令,以防有一天链接断开)

sudo apt-get build-dep python-libtorrent
wget http://downloads.sourceforge.net/project/libtorrent/libtorrent/libtorrent-rasterbar-1.0.5.tar.gz
tar -zxvf libtorrent-rasterbar-1.0.5.tar.gz
cd libtorrent-rasterbar-1.0.5/
./configure  enable-python-binding PYTHON=`which python`  prefix=$VIRTUAL_ENV
make
make install
export LD_LIBRARY_PATH="$VIRTUAL_ENV/lib"

我想问题是:

    {{CD1}}和^ {< CD2}}是Python绑定/包装到C++库,正如您所说的。在
  1. 在这个链接中,有一个命令根据您使用的python路径编译lib torrent:./configure enable-python-binding PYTHON=`which python` prefix=$VIRTUAL_ENV,当您创建一个虚拟环境时,这个命令会发生变化。在

所以理论上,你必须为你使用的每一个virtualenv编译libtorrent。这是一个可怕的解决办法,但我相信这是唯一有效的办法。比简单的pip install -r requirements.txt更糟糕。在

如果你觉得合适,请告诉我,并考虑将此标记为正确答案。在

相关问题 更多 >

    热门问题