After learning that Python has several versions, and after learning how to install any of them using pyenv, today’s tip teaches you the next step: how to use venv to create virtual environments with Python 3.
At the launch of Python 3.3 venv was announced as a new language module, and in version 3.6, venv became the official choice for creating Python virtual environments with the deprecation of the pyenv script. History of how venv came to life aside, let’s go to what matters. You can use any version equal to or higher than Python’s 3.3 that the commands below should work ¯\_(ツ)_/¯.
To create a virtual environment:
|
To activate the environment:
|
Now pay attention to one thing: this way of creating virtual environments doesn’t allow you to create environments to other Python versions than the one you are already using.
|
|
Here I was using Python 3.3.0, so my virtual env has that version. That’s when having pyenv installed is a good thing! You only have to use pyenv and switch to the version you want then create the environment. Take a look:
|
|
|
|
|
|
|
One of the advantages of having one module of this kind built-in the language is that you don’t need to install anything else to create your virtual environments. To the people that avoid installing things on the computer like myself, this is fantastic!
Virtual envs in Python and AI
When you ask an AI coding agent to install a package, it runs pip install with whichever Python it finds first. If no virtual environment is active, that is your system Python. On a Linux machine with a recent system install of Python, a package installation can get refused:
|
|
|
|
|
|
|
|
On other setups the install goes through, and the package lands in the Python that every project on your machine shares. Neither result is what you wanted, and on the second case it can even be an issue if two projects require different packages versions.
Check where you are
Two quick checks, for you or for the agent, these can be run in the terminal and will check the Python installation path and if you are in a virtual environment:
|
|
|
Inside an active virtual environment, the first one prints a path ending in something like: .venv/bin/python and the second will print True, the sys.prefix will say the directory of the the current Python installation files while sys.base_prefix is the directory of the system-wide Python installation (even when a virtual environment is activated). Following the same logic, outside of the virtual environment, you get the system-with Python path and False for the environment check.
What the agent should do
If you are working with a harness or an agent, your coding assistant should still follow best practices where virtual environments are concerned. It should always follow steps like the ones below:
- Look for a
.venvfolder in the project before creating anything. - If there is one, use it. If there isn’t, create it with
python -m venv .venv. - Don’t create a second environment next to the first one. Unless permission is given or explicitly asked for it.
- Remember to activate and use the available virtual environments. Some agent tools run every command in a fresh shell, so an
activatefrom one command may not carry over to the next. - Never install outside a virtual environment, and never add
--break-system-packagesto get past that error above. - Keep
.venvout of git by adding it to.gitignore.
Write those down once, in your AGENTS.md:
|
|
|
|
|
|
|
And some prompts to try when you are asking in the moment:
- “Create a virtual environment for this project in .venv with Python’s venv module, then install the dependencies.”
- “Before you install anything, tell me which Python you are about to use.”
- “Use the existing .venv. If you think we need a new one, ask me first.”
Now that you have your virtual environment created you can go ahead, pip install all things and start coding. ☺️