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