2025-10-11
Using Virtual Environments in Python
You wouldn't believe
how long it took me to make this image in the
GIMP.
Hi! This is an anti-slop blog that just presents some simple information without ads and a tremendous backstory about how my grandfather used Python back in the day or whatever. (He used FORTRAN and punch cards, for the record, but already I digress.)
And here I'm using the Zsh shell, but a generally-compatible one like Bourne or Bash will work. Check out the official instructions for other shells or tons more detail on this topic.
Let's go!
What is a Virtual Environment?
It's a place you can install Python programs and libraries that are
isolated from the rest of the system. So you can pip install
things
and not mess anything up elsewhere. A common use case is that you want
to run someone's Python program and install its related packages but
don't want to pollute your system with all these unmanaged tools and
dependencies.
On Arch Linux (BTW) and others, this is the recommended way to install Python packages when they're not part of the official package manager.
There are four steps:
- Create the virtual environment (one-off step per environment).
- Activate it before use.
- Do things in it.
- Deactivate it when done.
You only have to create each environment once. After that it's a matter of activating it when you need it and deactivating it when you're done.
And you can have as many virtual environments as you want scattered around in various directories.
Creating the Virtual Environment
The virtual environment will live in a directory. You specify the
directory when creating it. Here I create one in a directory called
fooenv
out of my current directory:
python -m venv fooenv
Or you can use a full path or whatever.
Activating It
Switch into the directory and then source the shell script
bin/activate
.
cd fooenv
. bin/activate # or "source bin/activate" if you prefer
The shell prompt will change to have a prefix with the name of the virtual environment in it; this is an in-your-face reminder that you're "inside" the virtual environment.
All the
activate
script effectively does (besides change your prompt and a couple other things) is set yourPATH
so that when you runpython
orpip
it uses the one in the environment'sbin/
directory instead of your system version. Runwhich python
and see!
Do Things In It
Now you can run pip install
and install things that are only in this
virtual environment. And when you run python
you can see them.
Have fun!
Deactivate It
When you're done having fun, you can get back to the real world by running this:
deactivate
It's a secret function that the bin/activate
script created. This gets
you back to the non-virtual environment where everything is real and
wholesome again. Run which python
and see!
(You can also just exit the shell. That effectively deactivates it.)
Deleting It
If you're done with the virtual environment and want to get rid of it, just carefully delete the directory that holds it. It will be as if it never existed [spooky ghost noises].
Bonus: Running from a Shell Script
What if you have a shell script and you want to call something in that virtual environment? Well, you just have to do the same steps.
- Create the environment somewhere that's relatively permanent. Maybe
in a
~/.local/venvs/
directory where you keep them all. - In your script, run
bin/activate
in that directory. - In your script, do fun things.
- In your script, just exit when done. Or run
deactivate
then exit, either way.
Here's an example script that prints out the path to python
in the
virtual environment:
# foo.sh
. /home/beej/fooenv/bin/activate # Change to your path
which python
deactivate
When I run that I get:
$ sh foo.sh
/home/beej/fooenv/bin/python