[Python] How can I manage virtual environments with pyenv?

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    Scenario

    I would like to start a new project in Python. I need a different version of Python than the one on my system (Ubuntu 23.10 (Mantic Minotaur)), so I followed the steps below:


    • I downloaded pyenv and installed the desired version of the Python interpreter;


    • I moved into the project folder


    • I created a virtual environment via pyenv


    • Now I have to install my dependencies into the venv

    Before install any package, I checked the Python version to make sure I'm using the correct version of Python, but the output confused me:

    Outside the project directory using python


    cd ~/Outside_Project_Folder
    python --version


    Output:

    pyenv: python: command not found

    The `python` command exists in these Python versions:
    3.12.3
    3.12.3/envs/.venv
    .venv

    Note: See `pyenv help global` for tips on allowing both
    python2 and python3 to be found.


    And:

    which python


    Output:

    /home/usr/.pyenv/shims/python

    Outside the project directory using python3


    cd ~/Outside_Project_Folder
    python3 --version


    Output:

    Python 3.11.6


    And:

    which python


    Output:

    /home/usr/.pyenv/shims/python3

    Inside the project directory using python or python3


    cd ~/Project
    python --version


    Output:

    Python 3.12.3


    which python


    Output:

    /home/usr/.pyenv/shims/python


    Question

    I am confused about how to install dependencies within the virtual environment associated with the version of python installed with pyenv.

    I thought about running pip install from within the project folder, but I'm afraid that doing so might pollute the version of Python installed via pyenv.

    I would like to install the packages exclusively for my project, but how do I understand if I am inside the virtual environment?

    Also how do I figure out what is the current active version of Python wherever I am in the file system?

    Continue reading...

Compartilhe esta Página