[Python] Order of system-wide locations and virtual environment in `sys.path`

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I created a virtual environment using

    python -m venv venv


    Now I'm opening a Python shell without activating the virtual environment by running

    import sys
    print(sys.path, sys.prefix)


    I get

    ['', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/usr/lib/python3.12/site-packages'] /usr


    which is exactly what I'm expecting.

    While, if I activate the environment, the output is

    ['', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/home/myname/mypath/venv/lib/python3.12/site-packages'] /home/myname/Projects/pypath/venv


    What upset me a lot is that even inside the venv it seems that the interpreter is searching packages firstly in the system-wide location, and only then inside the venv directory.

    Is this true?

    Continue reading...

Compartilhe esta Página