Skip to content

Instantly share code, notes, and snippets.

@hartleybrody
Last active February 13, 2024 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartleybrody/4a94859e2eaf3d3b7a105c3694453d9a to your computer and use it in GitHub Desktop.
Save hartleybrody/4a94859e2eaf3d3b7a105c3694453d9a to your computer and use it in GitHub Desktop.
modern python versions + virtualenvs

I do this infrequently enought that I always forget how to do it. Note that I've finally decided to move away from virtualenvwrapper (workon, mkvirtualenv, etc) since it didn't play nicely with the python versions set by pyenv.

Install the tools (pyenv & pyenv-virtualenv)

Only necessary if this is a new laptop

brew install pyenv
brew install pyenv-virtualenv

Install python version

If not specified by the project, use a python version released in the last year or two.

pyenv install 3.11

Note that pyenv may say that there is no matching versions, and you'll need to append the latest minor release version to the version number as well.

Specify this as the local python version

Create a .python-version file to ensure pyenv always uses the correct python version when using a terminal window inside this project's directory.

pyenv local 3.11

Verify this is getting picked up correctly by running

python --version

as well as

pyenv version

Create a virtualenv using this python version

Now that python is setup correctly in the project's directory, create a new virtualenv to isolate dependencies.

pyenv virtualenv 3.11 name-of-virtualenv

help via stackoverflow

View the virtualenvs you've created with

pyenv virtualenvs   <-- note the "s" on the end of the command

Verify you have setup a clean virtualenv

pip freeze

Now you can install your project's dependencies

pip install -r requirements.txt

To activate the virtualenv in a new shell (say, whenever you cd into the directory with autoenv)

pyenv activate name-of-virtualenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment