Visual Studio Code for Python Developers Transcripts
Chapter: Managing Environments
Lecture: Virtual Environments

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we've learned how to switch between different versions of the Python interpreter using pyenv,
0:06 let's move on to talk about virtual environments. Now, according to the official documentation,
0:11 virtual environments are a cooperatively isolated runtime environment
0:15 that allows Python users and applications to install and upgrade Python distribution packages
0:21 without interfering with the behavior of other Python applications running on the same system.
0:25 Now, that sounds like a superficial way to think about it, but the way me and Brian look at it is,
0:31 virtual environments are just a safe place to work on our Python projects on our dev machines
0:35 that doesn't interfere with everything else that's going on on the operating system. So that means that we don't have to worry about conflicts
0:42 whenever we install or upgrade a dependency that our project needs. Now, it turns out that Visual Studio Code and the Python extension
0:49 have really great support for working with and creating virtual environments, so why don't we head over there and take a look at it.
0:55 With the podcast project opened up in Visual Studio Code,
0:59 you might notice that I have some error messages due to some unresolved dependencies inside of my project.
1:04 Well, why don't we go ahead and create a virtual environment on the command line and then try to resolve these issues.
1:09 Now, I'm going to use the VENV module built into Python to create a virtual environment inside of my folder.
1:15 One thing you might notice on the bottom right side, Visual Studio Code has now noticed that I just created a virtual environment within my workspace.
1:24 So now it should ask me if I want to associate it with my project. I'm going to go ahead and hit yes.
1:30 One of the things you might notice also on the bottom right side, if I click on this little selector here,
1:36 it has set that virtual environment as the default interpreter that I'm going to use.
1:42 So now whenever I install packages or any additional dependencies, it's going to be scoped solely to this folder.
1:48 Now, another thing we usually have to do is activate our virtual environment. But what you might notice is, well,
1:54 the Python that's inside of our terminal right now is still not set up to work. So now we're going to set it to the one that's within our project.
2:02 Now, one of the things that we could do, if we wanted to, well, it was we could run this activated command,
2:06 and that's included inside of that virtual environment. But we're not going to do that. Instead, if I create a new terminal window,
2:14 what you'll notice is that Visual Studio Code has gone ahead and activated that virtual environment for me.
2:22 So anytime we create a new terminal for our Python project, that's just one less thing for us to worry about.
2:26 Now, if I check one of the things that I want to do, which version of Python this one's running,
2:30 it's using the one that's inside of the virtual environment we just created. Now, that's just one convenient way to create virtual environments.
2:37 But let's say you're not a command line person. Well, one of the things you can do is open up the command palette inside of Visual Studio Code,
2:45 and then you can type Python create environment. And then notice it'll offer you to create a virtual environment using VENV or Conda.
2:54 I don't have Conda installed, so I'm going to pick the first option. And now I'm going to pick the base interpreter that I'm going to use.
3:01 And if you remember, I have multiple versions of Python installed on my machine. So I'm going to select the one that I want it to be the base for.
3:08 And I'm going to choose 3.11. Now the last step is notice that I have a requirements.txt file also inside of my folder.
3:16 So it's giving me the option to go ahead and install those while it's creating the environment.
3:22 So again, very convenient and just less steps for me to do. Well, that's just a really convenient option if I didn't want to use the command line.
3:30 Now if I run my project, looks like everything's up and running. So that's perfect. I'm going to close this.
3:39 And I'm going to go ahead and delete this virtual environment that we just created.
3:43 And I want to show you one more way that we could do this inside of Visual Studio Code.
3:48 So you saw how to do it via the command line and you saw how to do it via the command palette. Well, what if I open my project?
3:55 If I am supposed to head over to the requirements.txt file, notice on the bottom right side, it says create environment.
4:02 So that means that I can initiate that create environment action straight from the requirements.txt file.
4:09 And I'll go through the same workflow that I did before where I select the interpreter I'm going to use.
4:14 I'm going to select whether I'm going to use conda or VNV module. And then also it'll install my requirements listed in my requirements.txt file.
4:24 So there you have it. You've seen three different ways that you can create virtual environments using a Python extension inside of Visual Studio Code.
4:31 Now let's take a second to talk about how the Python extension discovers these different environments inside of your system.
4:37 When the VS Code extension for Python is looking for different interpreters on your system,
4:42 different virtual environments for you to use inside your project, there are a few different lookup locations it checks out by default.
4:48 As an example, it takes a look at common installation paths based on your system,
4:53 whether it's a Windows path or a Mac path or a Linux path, for instance.
4:58 Also, it's going to look for virtual environments that you might have created inside of your workspace folder, kind of like what we did in this video.
5:06 It's going to look for the virtual env folder off the root home folder of that user.
5:13 And it'll also look for different environments that might be created by pyenv, pipenv, and even portrait. Also, if you're using conda environments,
5:22 anything that's returned from the conda env list command is also for your game too. Now this is not an exhaustive list.
5:29 There's definitely more places that it looks at when it's trying to determine what available environments,
5:34 whether it's virtual environments or interpreters that are available for your system to use.
5:38 So I definitely recommend that you check out the documentation to see all the different options that you have. that you have.


Talk Python's Mastodon Michael Kennedy's Mastodon