Categories
Technology

Specifying Python Version in venv

I’m leaving this blog post as a note to myself or to anyone else who stumbles upon this issue in the future. I was recently working on a project and was having issues with my virtual environment not installing dependencies correctly. The root cause was I was using Python 3.12, and should have been on Python 3.11.4. I didn’t think this would be a big deal to fix, but it turned out to be surprisingly frustrating.

I installed version 3.11.4 of Python with pyenv, set it as the default version on my machine, and recreated the virtual environment. Annoyingly, it was still using Python 3.12, even though typing python --versionโ€‚and python3 --version resulted in 3.11.4.

Turns out, when you are creating the virtual environment, you need to specify the Python executable that corresponds to the version you want to use, or else it uses the most recent version. I had (incorrectly) assumed that running python3 would use the global version, but I assumed incorrectly. In this case, I needed to run python3.11 -m venv .venv and it created my virtual environment with Python 3.11.4. ๐ŸŽ‰

This could have been specific to my local environment, but I had a difficult time tracking down documentation on how to handle this, so I thought I’d document it for future use.