John Kealy
John Kealy

Reputation: 1883

How to create a Python virtualenv within my project directory with a specific (but uninstalled) python version

I'm a fan of virtual environments, and I also like to keep my virtualenvs in the root project directory. This helps me keep track of where they are and allows me to call them generic names, like .venv.

But I need a way to install specific versions of python for use with my virtualenvs. All roads seem to point to pyenv.

Unless I'm misunderstanding how pyenv and pyenv-virtualenv work, this libary seems to insist on burying my actual virtualenv somewhere in the ~/.pyenv folder, and leaving me with a .python-version file, which I'd rather not have.

Isn't there a way to install specific versions of python with the ease of use of pyenv, but then create my virtual environments in the traditional way, inside my project directory, thus removing my projects's dependency on pyenv after the virtual environment has been created?

Upvotes: 6

Views: 4054

Answers (1)

HALF9000
HALF9000

Reputation: 628

# you need install the target version first
pyenv install 3.9.5
# then sets a shell-specific Python version
pyenv shell 3.9.5
# use this python version to create virtualenv
python -m venv .venv
# or use virtualenv
virtualenv .venv -p $(pyenv which python)

Upvotes: 15

Related Questions