Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Concept: Virtual environments

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk about virtual environments one more time, and I specifically want to cover how the Windows version is different,
0:07 specifically different than doing things on Mac and Linux. They're both great. They both have the same concept in the same functionality,
0:14 but because of the way the shells work, mostly, and the way that Python gets installed, partly, you gotta issue slightly different commands.
0:21 They're very easy once you get used to them, but at first it can be a stumbling block. Okay, so let's review the Linux and macOS version.
0:29 So what we're gonna do is we want to create a virtual environment in our top level project, the top directory,
0:35 the root level directory of our projects. Sp we say "Python3 -m venv", so the "-m venv" means run the command within Python 3, and then the
0:43 orange "venv" is the directory name. So those happen to be the same by convention most of the time, they don't have to be.
0:49 Once that happens, it's created, but you don't actually have access to it yet.
0:53 You have to activate it. So you have to say "dot", meaning apply to this shell, or source, and then "venv/bin/activate".
1:01 That's going to change things about Python, it's going to change the path, it's going to change the prompt, and when you run things like "pip List",
1:09 you'll see you no longer have the entire system, whatever is installed for the Python there. You just have a fresh, empty Python,
1:16 and the only packages it has are the tools that you need in order to install more packages. pip and setuptools.
1:23 And then we can ask "which Python" and it says "It's the one here in this project". So it's header's a project directory called "weather_app",
1:30 You'd see it's the one in "venv/bin/Python" and then off you go
1:34 to install various things. One thing you might consider, didn't happen this time because Python
1:39 3.9 was just released, but often what happens is pip itself is out of date and any time you try to use pip,
1:45 it'll complain to you that it's out of date. This is like 80 - 90 percent of the time you're pip is out of date. It's not worth going into why that is,
1:53 but when you create a virtual environment, just get in the habit of always upgrading your base level tools,
1:59 pip and setuptools, so you're working with the latest one as you install other things. So here's what you do on macOS and Linux.
2:05 This two parts right there are mostly, it's basically where it varies for Linux versus Windows. Sometimes the Python 3 versus Python varies,
2:15 but that depends on how you set up your Windows machine. Speaking of windows, over here, same thing. Probably you just run "Python -m", depends.
2:23 Then, once we create our virtual environment, we don't activate it with the dot, We omit the dot and instead of the
2:29 bin folder, there's a scripts folder. So you say "venv\scripts\activate" why there's a difference there,
2:35 they haven't unified those things, I have no idea because it seems trivial to do so, but they haven't done it for years and years,
2:42 So this is how you do it in Windows. Just remember, not bin, scripts, pip list, exactly the same. Once you've activated it, your prompt changes,
2:49 it has the venv in there again. Now you can't ask which on Windows, That's not a command, but a command that does work on command prompt is "where".
2:57 So here you get the same basic answer. It's the version of Python in the virtual environment Windows style. So scripts folder, Python.exe,
3:05 you'll also see other Pythons. But when you type Python, it's going to show, the where command shows
3:09 you all of them in the path, you're going to run the first one that's found in the path, which is the blue one from the virtual environment.
3:15 Again, you want to upgrade pip and setuptools for exactly the same reason. So the big difference, these two. Remember, it's no dot, scripts not bin,
3:24 and then "where" instead of "which" Python. That's it, they might seem like a bit of a stumbling block it first,
3:29 but trust me, once you get used to them, you just do it the way that you do it on any OS and off you go.

Talk Python's Mastodon Michael Kennedy's Mastodon