Creating and Activating a Virtual Environment
00:00 I am going to jump into a terminal session now, to show you how you can create and activate these virtual environments. Alright, I’m in my terminal here and now I am going to show you how to create your first Python virtual environment.
So, the first thing I want to demonstrate to you is when I use the
which command to look up where the pip executable is right now. You can see here it’s inside
/usr/local/bin/pip3, which is the global shared environment.
So, the way around that is by creating a virtual Python environment. Now let’s assume we wanted to start a new Python project, so probably create its own folder for that, so I would create a directory let’s call that
Switch into that test project, and you can see here that right now, this is completely empty. So what I am going to do now is I am going to create a virtual environment, with this command here, so you want to go
python 3 -m venv if you are on Python 3, on Python 2 it’s a little bit different but I am going to walk you through that later.
01:43 So, personally, I would do something like this, but of course, you could also have a shared folder, like a Python environments folder, where all of your Python environments live and then you are going to be able to reuse them across different projects.
Now, personally, I don’t recommend that, so this is what I like to do. Okay, so this just took a second here to set up the virtual environment, and now, when I check what is inside this folder, we can see here that now we have this
Now why don’t we take a closer look at this
venv folder? So you can see here that there is a lot of stuff inside that folder, because this is actually a completely isolated and separate Python environment.
02:32 Now this is not going to be very interesting, because, it’s just the Python internals here, but this should give you an idea that a Python virtual environment is actually a completely separate Python environment, and that is exactly what we want.
Alright, so we created a virtual environment, and if I were to run this
pip3 command, or the
pip command now, it would actually still point to the global environment, so there is one more step we need to take here.
And that is we need to execute a script inside the virtual environment. And, it’s this one here, so inside the virtual environment, you want to go into the
bin folder and look for the
And so when I run this, this activates the virtual environment, and you can see that here that running the script out of this little marker here to my shell prompt, now it tells me that I am inside, or that I have activated this virtual environment called
So that is just a folder name that I used earlier. Now, when I use this
which pip command again, you can see that now this is actually pointing to a different location, so now this points to the separated and isolated environment that I just created.
03:41 And the same thing is true for the Python interpreter, so now if I were to run the Python interpreter, it would actually load it from inside the virtual environment and not from my global environment, which is exactly what we want.
So this is how you create and activate the Python virtual environment, so here is a quick recap on what I just showed you, so on Python 3.3 and above, it’s really easy to manage your virtual environments because the
venv command, or the
venv module that manages them is actually part of the Python distribution, so you can just use that
python -m venv and then the name of the folder where you want to create that virtual environment.
But on older Python versions, it’s a little bit different, so for those versions of Python, you typically need to install the
virtualenv package manually, and then you would use the
virtualenv command and it would kind of follow the same syntax to actually create a virtual environment, and you would activate it in exactly the same way.
So that is a little difference you need to be aware of, maybe one extra step you need to take before you can start creating your virtual environments. On Windows, the step you need to take to actually activate a virtual environment is slightly different, so we’re not using the
source command there, to load the activate script, but instead, we’re just running the
activate command or
activate script from the
scripts folder, so that is a small difference, but in all other aspects, it’s very similar to how it works on Linux and macOS.
Become a Member to join the conversation.