Managing Python Dependencies Transcripts
Chapter: Isolating Dependencies With Virtual Environments
Lecture: Creating and Activating a Virtual Environment

Login or purchase this course to watch this video and the rest of the course contents.
0:01 I am going to jump into a terminal session now, to show you how you can create and activate these virtual environments.
0:07 Alright, I'm in my terminal here and now I am going to show you how to create your first Python virtual environment.
0:14 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.
0:22 You can see here it's inside user local bin pip3, which is the global shared environment.
0:29 Now if I would go ahead and run pip install and install some library, it would end up, well, not exactly here in this folder, in the bin folder,
0:37 but it would end up in the global environment. So, the way around that is by creating a virtual Python environment.
0:45 Now let's assume we wanted to start a new Python project, so probably create its own folder for that,
0:52 so I would create a directory let's call that test project, switch into that test project, and you can see here that right now,
1:04 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,
1:12 so you want to go Python3 -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,
1:20 and then as the last argument here, you can pass the name of the folder where you want to store that virtual environment,
1:32 or where you want that virtual environment to be created. Now, personally, I use a very simple naming scheme,
1:37 I just create my virtual environments right inside the project folder, and I usually call them venv.
1:44 So, personally, I would do something like this, but of course, you could also have a shared folder, like a Python environments folder,
1:52 where all of your Python environments live and then you are going to be able to reuse them across different projects.
1:58 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,
2:07 and now, when I check what is inside this folder, we can see here that now we have this venv folder. And when I check what is inside the venv folder,
2:15 you can see here that there is a bunch of files that are part of this new Python environment. Now why don't we take a closer look at this venv folder?
2:25 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.
2:33 Now this is not going to be very interesting, because, it's just the Python internals here, but this should give you an idea
2:40 that a Python virtual environment is actually a completely separate Python environment, and that is exactly what we want.
2:46 Alright, so we created a virtual environment, and if I were to run this pip3 command,
2:53 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.
2:59 And that is we need to execute a script inside the virtual environment. And, it's this one here, so inside the virtual environment,
3:06 you want to go into the bin folder and look for the activate script. And so when I run this, this activates the virtual environment,
3:14 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,
3:21 or that I have activated this virtual environment called venv. So that is just a folder name that I used earlier.
3:29 Now, when I use this which pip command again, you can see that now this is actually pointing to a different location,
3:36 so now this points to the separated and isolated environment that I just created. And the same thing is true for the Python interpreter,
3:44 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,
3:52 which is exactly what we want. So this is how you create and activate the Python virtual environment,
3:58 so here is a quick recap on what I just showed you, so on Python 3.3 and above,
4:04 it's really easy to manage your virtual environments because the venv command,
4:10 or the venv module that manages them is actually part of the Python distribution, so you can just use that Python -m venv
4:18 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,
4:25 so for those versions of Python, you typically need to install the virtual env package manually, and then you would use the virtualenv command
4:33 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.
4:42 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.
4:49 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,
4:57 to load the activate script, but instead, we're just running the activate command or activate script from the scripts folder,
5:06 so that is a small difference, but in all other aspects, it's very similar to how it works on Linux and macOS.


Talk Python's Mastodon Michael Kennedy's Mastodon