Working With a Virtual Environment
00:00 To avoid installing packages directly into your system Python installation, you can use a virtual environment. A virtual environment provides an isolated Python interpreter for your project.
00:13 Any external packages that you’re using inside this virtual environment will be independent of your system interpreter. This means that you can keep your project’s dependencies separate from other projects and the system at large.
00:27
Using pip
inside a virtual environment has three main advantages: you can be sure that you’re using the right Python version for the project at hand, You can also be confident that you are referring to the correct pip
instance when running pip
, and you use a specific package version for your project without affecting other projects.
00:48
And as so often, the good news is that Python has the venv
module that helps you create virtual environments right out of the box.
00:58
When you are in the terminal, you run commands from the perspective of your current working directory. To check what your working directory is, you can type pwd
and press Enter. So in my case, I’m currently in the realpython/
user folder of my operating system.
01:17
To show you how to work with virtual environments, let’s create a new folder first. For this, you use the command mkdir
. Let’s stay generic and name the folder project/
.
01:29
So the command is mkdir project
.
01:34
By typing mkdir project
and pressing Enter, I just created a new folder in my user directory. With the command cd
, I can change into this directory.
01:47
When you are in your project directory, it’s a good idea to create a virtual environment before doing anything else. And you do this by typing python3 -m venv venv
02:02
The first venv
is the name of the module, and the second venv
is the name of your virtual environment. You can name the virtual environment any way you want, but naming it venv
is common practice. Agreed, it makes the command look a bit funny, but also more memorable.
02:21
The important thing is that you remember that the first venv
is the venv
module name, and the second is the name of the virtual environment you create.
02:29
With the ls
command, you can list the items in a directory. So as you can see, your project directory now contains a venv
folder.
02:42
Here is a quick recap. You create a virtual environment with python -m venv venv
. This command uses your standard Python version to create a virtual environment.
02:55
But one of the advantages of using pip
with a virtual environment is that you can be sure you’re using the right Python version for the project at hand.
03:03
So if you have different Python versions on your system, you can use the Python version you want to use for this project with the venv
command.
03:12
That means you could also type python3.9 -m venv venv
on the mac or reference the full path on Windows to the Python executable of Python 3.9 on your system.
03:24 This command creates a virtual environment with Python 3.9 linked to it, no matter what your system Python version is at this point. That’s super handy if you want to develop a Python program, and you want to make sure that it works in different Python versions.
03:39 So you can create different virtual environments with different Python versions in them. However, so far, you only have created a virtual environment, and before using your virtual environment, you should activate it.
03:56
The commands to activate a virtual environment are a bit different between Windows and macOS and Linux. On Windows, you execute venv\Scripts\activate
.
04:08
And on mac and Linux, you run source venv/bin/activate
. The first part, venv
, is the folder name of your virtual environment. If you name your virtual environment different, then the command would be different too. Oh, and before you run this command, you need to make sure that you’re in the folder that contains the virtual environment folder you just created. Because with this command, you execute a script inside your venv
folder.
04:38
To make sure I’m in my project folder, I type pwd
, which stands for print working directory. I think I didn’t mention this before.
04:47
And to check if my venv
folder is present, I type ls
. To activate the virtual environment, I run source venv/bin/activate
.
05:00 Once you can see the name of your virtual environment within parentheses in your command prompt, then you know that your virtual environment is active.
05:09
You can type deactivate
to deactivate it,
05:15
but since I want to work with the virtual environment, I can activate it again by using the ↑
and find the activation command, press Enter, and there you see venv
in parentheses again.
05:28
And with this activated virtual environment, you are ready to install external packages with pip
in the next lesson.
michaelboatright on June 28, 2023
nvm I figured it out.
arthur55 on Dec. 11, 2023
Hello, When I try to activate the virtual environment I get the error message: “[Absolute file path] cannot be loaded because running scripts is disabled on this system.” Do you know what I need to do to my pc to fix this?
Philipp Acsany RP Team on Dec. 11, 2023
Hi arthur55! Oh, that’s annoying. It seems that you have a problem with your system’s execution policy.
I assume you’re on Windows? If so, then I think it might be a good idea to check out our guide on Setting up a Coding Environment on Windows and see if you can find a solution there :)
Become a Member to join the conversation.
michaelboatright on June 28, 2023
Hello, PS> venv\Scripts\activate doesn’t work for me.