Working With a Virtual Environment
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.
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.
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.
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.
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
Become a Member to join the conversation.