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.
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.
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.
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.
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
So the command is
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.
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
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.
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.
ls command, you can list the items in a directory. So as you can see, your project directory now contains a
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.
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.
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
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.
The commands to activate a virtual environment are a bit different between Windows and macOS and Linux. On Windows, you execute
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
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.
And to check if my
venv folder is present, I type
ls. To activate the virtual environment, I run
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.
You can type
deactivate to deactivate it,
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.
And with this activated virtual environment, you are ready to install external packages with
pip in the next lesson.
Become a Member to join the conversation.