TL;DR
Verbose explanations of each part can be found lower in the document.
# install
brew install pyenv pyenv-virtualenv openssl readline sqlite3 xz zlib
# make sure this is in your .bashrc / .zshrc
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
if which pyenv-virtualenv-init > /dev/null; then
eval "$(pyenv virtualenv-init -)"
fi
# install python version
# versions: https://www.python.org/doc/versions/
pyenv install <version>
# list all installed python versions and virtual environments
pyenv versions
# set global python version
pyenv global <version>
# create new virtual environment
pyenv virtualenv <version> <virtual_environment_name>
# tie virtual environment to repo
cd path/to/repo
pyenv local <virtual_environment_name>
# delete virtual environment
pyenv uninstall <virtual_environment_name>
FAQ
What problem is this solving?
- As a developer, constantly switching between Python versions and project dependencies can be frustrating.
- As a DevOps engineer, it is pivotal that the developer’s local environment is exactly the same as the environment that the project is deployed in.
- As part of Engineering Excellence, it is our job to cut down on the annoyances and time wasters that are slowing down our developers, while at the same time not forgoing security and all of our other best practices.
- Standardizing how we manage Python versions and project dependencies is important to our success as an organization. Hopefully this resource documents a worthy solution to our problems.
What’s the urgency?
The quicker we implement a solution, the quicker we get on top of this and cut down on developer strain.
pyenv
pyenv
is a simple Python version management tool. You can install any number of different releases of Python all at once, and have your terminal automatically switch between them.
pyenv Setup
These setup instructions were summarized and streamlined from the pyenv
repository README.md
for FIRE purposes.
- Install
pyenv
brew install pyenv
- Append this to the bottom of your shell (
.bashrc
,.zshrc
, etc) to enable shims and autocompletion.-
if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi
- Make sure this is at the end of your shell configuration file since it manipulates
PATH
during initialization.
-
- Restart your shell
- Make sure you have necessary Python build dependencies before you install a new Python version
brew install openssl readline sqlite3 xz zlib
- You’re all set! Continue on to the setup instructions for
pyenv-virtualenv
!
Helpful resources:
- Repository
- Managing Multiple Python Versions With pyenv
- How to use pyenv to run multiple versions of Python on a Mac
pyenv-virtualenv
pyenv-virtualenv
is a pyenv
plugin to manage virtual environments.
pyenv-virtualenv Setup
- Install
pyenv-virtualenv
brew install pyenv-virtualenv
- Append this to the bottom of your shell (
.bashrc
,.zshrc
, etc) to enable shims and autocompletion.-
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)" fi
- Make sure this is at the end of your shell configuration file since it manipulates
PATH
during initialization. - This code block is different from step 2 from the
pyenv
setup section.
-
- Restart your shell
- Make sure you have necessary Python build dependencies before you install a new Python version
brew install openssl readline sqlite3 xz zlib
- This is the same as step 4 from the
pyenv
setup section.
- You’re all set! Continue on to the Usage section for instructions on automatically switching between Python versions and virtual environments!
Helpful resources:
Install a new Python version
pyenv install <version>
- Replace
<version>
with a valid Python release version- If you’re not sure what Python version a project requires check the
README.md
,setup.py
,tox.ini
, or reach out to one of the project’s maintainers.
- If you’re not sure what Python version a project requires check the
- e.g.
pyenv install 3.9.18
- Replace
Set your default global Python version
pyenv global <version>
- Replace
<version>
with any of the versions you’ve installed- You can list what versions you’ve installed by running:
pyenv versions
- You can list what versions you’ve installed by running:
- Replace
- Double check that your global Python version matches the one you’ve set
python --version
- If the version is incorrect, make sure to run Step 2 from both the "pyenv Setup" and "pyenv-virtualenv Setup" sections. This will make sure that the
pyenv init
line will always run first thing when starting a fresh terminal.
Create a new virtual environment
pyenv virtualenv <version> <virtual_environment_name>
- Replace
<version>
with any of the versions you’ve installed- You can list what versions you’ve installed by running:
pyenv versions
- You can list what versions you’ve installed by running:
- Replace
<virtual_environment_name>
with a unique string name for this project’s virtual environment - e.g.
pyenv virtualenv 3.9.18 venv_my_project
- Replace
Automatically activate and switch between Python versions and virtual environments
“When you run a Python command, pyenv will look for a .python-version
file in the current directory and each parent directory. If no such file is found in the tree, pyenv will use the global Python version specified with pyenv global
.
cd
into your project’s local repository directory- List your available Python versions
pyenv versions
- This should show a list of all the Python versions you’ve installed, as well as all virtual environments you’ve created
- Set your preferred virtual environment
pyenv local <virtual_environment_name>
- Replace
<virtual_environment_name>
with the unique virtual environment name you’ve created for this project - e.g.
pyenv local venv_my_project
- Replace
- This will generate a
.python-version
file in your repository where you called it. It’s best if it lives in project root.- This file should probably be gitignored, unless you align every developer of your project to use the same exact
<virtual_environment_name>
. That could definitely work, as long as it is outlined in the project README.md or such.
- This file should probably be gitignored, unless you align every developer of your project to use the same exact
- You’re all set! Now, everytime you
cd
into your project, you will have the correct Python version and virtual environment automatically activated for you! Less time wondering why tox is bugging out, and more time completing the WI you actually came to do.
Double check PyCharm is pointing to the correct virtual environment
- Inside PyCharm, go to “PyCharm” → “Preferences” → “Project” → “Python Interpreter”
- At the top, select the dropdown titled “Python Interpreter” and choose the “Show all” option
- If you don’t see the virtual environment you’ve created for this project, click the + symbol at the bottom-left
- Click the radio button titled “Existing Environment”
- Select the dropdown titled “Interpreter” and select the correct virtual environment
- Note: if the virtual environment you created doesn’t show up, close all of these dialogs windows and redo these steps. PyCharm caches these results, so reopening these dialogs should repopulate the list properly
- Click “OK” and “Apply” until you’ve escaped all dialogs
- That’s it! Now your terminal and PyCharm are all synced up!
How to delete a virtual environment
pyenv uninstall <virtual_environment_name>
- Replace
<virtual_environment_name>
with the unique virtual environment name you want to delete
- Replace
- If you’ve set this virtual environment via
pyenv local <virtual_environment_name>
in one of your projects, make sure to remove the.python-version
and/or replace it with a new virtual environment