This guide shows you a way to install multiple python versions on Mac using pyenv and to control virtual environments with pipenv.
Installing multiple versions of python
To control python versions I use pyenv. I installed it using:
brew install pyenv
Afterward, I added the environment variables as described in pyenv GitHub
You can set versions of python with pyenv with:
pyenv install 3.11.4 # Use this option for Mac Intel
arch -x86_64 pyenv install 3.11.4 # Use this option for Mac M1
You can set the python for a project with:
pyenv local 3.11.4
which will create a file in your project called .python-version
Or globally with:
pyenv global 3.11.4
You can check versions with:
pyenv versions
You can also check python version with:
python --version
Binaries and python versions are usually stored in ~/.pyenv
Creating virtual environments
To manage python dependencies and virtual environments I make use of pipenv
Install pipenv:
pip install pipenv
Inside your project folder you can create a virtual environment and install your packages with:
pipenv install <a_package>
When you run this command it will install your libraries based on the python version you set in your pyenv.
Since you may have several python versions on your machine, I advise you to create a virtual environment that always specifies the python version with:
pipenv --python 3.11
Now with your environment created, no matter what python version is set to be your global, you can do the below command and it will automatically launch the latest created environment
pipenv shell
Once you run the command above you will be inside a virtual environment in your terminal. To install a package you can do
pipenv install yamllint
This will create a Pipfile and a Pipfile.lock. The first will show the packages that you are installing and the lock file shows the packages' version hashes and their dependencies as well.
If you would like to know more about pipenv and dependency resolution I advise you to read this complete guide about it.
Integrating with VSCode
Now that you have all installed via terminal you might want to integrate all that with VSCode.
Install Python VSCode and it should work, right? Well, not everything.
Even though you loaded your virtual environment in the terminal, VSCode might not detect it and you will not be able to use the VSCode debugger
To fix it click on item 1 from the image below and choose the virtual environment you created before. Once you click you should see the virtual environment reference in your VS Code bar.
If your virtual environment is not showing up, make sure you created one by going to your project terminal and running pipenv shell. If you still cannot select it afterward, you need to tell VSCode where your environments are by going to VSCode Settings > Looking for venv > Venv Folders and add there the path of your environments. In my case that is ~/.local/share/virtualenvs/
Adding environment and python version to your terminal
You can also have information about your virtual environment displayed in your terminal together with your python version like item 3 from the image above
For that, you will need to install https://ohmyz.sh and add steroids to it with https://github.com/romkatv/powerlevel10k
PowerLevel10k might seem intimidating at first but there's a wizard where you can choose exactly how your terminal should be.
Top comments (2)
You might want to try github.com/jdxcode/rtx/blob/main/d..., a similar tool inspired by pyenv but that does not use shims, hence removing some of the typical pitfalls. I've been using it for some time and I love it.
Interesting ^^ Will take a look.