- Python Is Rad
- Posts
- VENV - This Ain’t Your Grandpa’s Virtual Environment
VENV - This Ain’t Your Grandpa’s Virtual Environment
Imagine you’re sitting there, doing your job, minding your own business, and then the sky opens up and all these packages come crashing down on you. Pandas, Notebooks, Pillows… Not cool right?
Well, that’s how the Global Python Environment feels when you Pip install packages directly onto it. And that’s why this issue is about virtual environments.
Now maybe you already know all about Python’s virtual environments. Well, aren’t you the smartest thing since sliced beef. That’s great, but I still think you’ll get something out of this. I use virtual environments every damn day and at the end of this article I’m gonna show you my personal shortcut for blasting out VENVs on command.
But if you’re not familiar with virtual environments. Or you’re using Anaconda. Then hold onto your hats and glasses folks cause this here’s the wildest ride in the wilderness. Oh wait. That was the Log Ride at Knott’s Berry Farm. This is much more exciting.
Virtual Environments in Python are actually very easy.
Let’s take a look at The Global Python Environment. Where the packages would go to party (and wreck your motel) if you didn’t put them into separate environments.
Let’s assume you are on a Macintosh. (Only because I am.) Let’s see where Python and Pip hang out.
In your terminal run:which python3
It should respond with something like: /usr/bin/python3
Now try: which pip3
Something like: /usr/bin/pip3
So if you were to Pip install a package now, it would go into the Global Python Environment. But that gets messy if all the packages, for different projects, go to the same place. You might need a specific version of Pillow for one Python project, and a different version for another project. Crazy right? Right?
Virtual Environments are the solution.
Let’s create a separate environment for a new project.
Make a new directory in your Documents folder for a new project.
cd
into it in your terminal.
Now let’s make a virtual environment. Traditionally they are named venv
.
python3 -m venv venv
You just created a folder called venv
with a copy of python3
in it.
Take a look in the folder.
venv folder
Inside the bin
folder there is an alias to Python. In this case python3.9
In the lib
folder you can see site-packages
. This is where the Python packages are going to party. Right now there is only pip
and setuptools
(boring). But this is a good place to check if you are having problems with installed packages. You can even go into the packages and inspect (and sometimes fix) the package code.
Great. Now you have a virtual environment. The next step is to activate it.
source venv/bin/activate
You should notice a (venv) in front of your cursor.
(venv)$
That shows that you are in a virtual environment called venv.
Now let’s install everyone’s favorite Python package: Requests
pip3 install requests
Take a look inside the site-packages folder again.
after pip installing requests
You’ll see the requests
folder. You’ll see a dist-info
folder with the Requests version number on it. You’ll also see the packages that Requests depends on, like Urllib3 and Certifi.
It’s worth poking around in these folders to understand how Python organizes packages. All the code is there to be seen.
Get Ready to Program
Ok. So you’ve got your Virtual Environment set up. You’ve activated it. You have a separated, organized place to install packages. You’re ready to program.
If you shut down your terminal, or it crashes, you can always get back into the virtual environment with
source venv/bin/activate
To quit the virtual environment you can just type:
deactivate
And the (venv) should disappear.
Creating a VENV Alias
I end up creating new Virtual Environments a couple times a week. So I’ve set up an alias, aka shortcut, in my .bashrc
file. It also works in .zshrc
.
alias v="python3 -m venv venv"
alias sv="source venv/bin/activate"
To use the new shortcut:
1) Shut down and restart your terminal. (This will reload the .bashrc
file)
2) Navigate into your project folder.
3) v
will create a new virtual environment
4) sv
will activate it.
If you’re not using bash aliases, you should consider it. They make the command line fun for the whole family.
Alright bucko, that’s how you create and use Virtual Environments. Now go forth and program.
TL;DR
1) Navigate to your project directory in the terminal
2) Create the virtual environment: python3 -m venv venv
3) Activate the virtual environment: source venv/bin/activate
4) Pip install python packages
5) Party time!
Python Is Rad,
Potter