[Python] Install packages into virtual environment with apt-get

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a virtual environment for my Python3 project. The problem is, some of the dependencies I'm trying to install into the virtualenv aren't through pip. For example, to get LibTorrent, I had to run: $ sudo apt-get install python3-libtorrent (LibTorrent is a C++ library with Python bindings). Outside of the environment, my project runs fine. But inside I get an import error:

    (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'


    If I run $ sudo apt-get install python3-libtorrent inside the environment, it tells me that it's already installed:

    (env) me@Comp:~/Projects/test$ sudo apt-get install python3-libtorrent
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    python3-libtorrent is already the newest version (1.1.1-1build2).
    0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.


    My understanding is this is because apt-get is a global command and has nothing to do with the environment. But if this is the case, how do I install this package into my env?

    Continue reading...

Compartilhe esta Página