Reviewing Packages and Virtual Environments
00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll be showing you what a Python package is, how it fits into your coding landscape, and what all that has to do with virtual environments.
There wasn’t much thought about shipping anything except maybe the scripts themselves, and packages really weren’t a consideration. It wasn’t until almost ten years into its existence that Python started thinking about package support, and thus,
distils was born. Three years later, the Python Cheese Shop was created. Python The language is named after Monty Python the comedy troupe, not the slithering, apple-tempting things.
00:59 Not only was it named in a funny way, but it didn’t do much either. The original PyPI was only a listing of the existing packages out there. As package management advanced and multiple versions of Python and libraries started kicking around the same machine, a problem occurred: library and version conflicts.
01:18 Thus was born virtual environments. I’ll come back to that in a bit. And like all history, it continues to be made. For those who listen to the Real Python Podcast, you’ll have heard Mr. Bailey and I discuss this several times: things in the packaging world are adapting quickly.
01:41 All right, let’s get some terminology out of the way. And like all terminology, there’s the right way, and there’s the way everybody uses it. The script is more or less agreed upon. That’s a single file.
02:05 A package is a collection of modules, but again, the real use of this differs a little bit. Typically, you’ll hear module to mean the code that is installed and being imported, while package typically gets used to mean the thing you’re downloading and installing, resulting in one or more of those modules that can be used.
02:23 And with all this kind of vocabulary stuff, if you look on the Internet, you’ll see arguments about who’s using it correctly or not. I could care less, but it would take a lot of effort. And why is this stuff so confusing?
Well, take the following example that doesn’t help. The
-m argument to the Python interpreter lets you you directly execute some code. This particular snippet is a handy little tool that pretty-prints a JSON file.
You might think Hey, -m must mean module, and you’d kind of be right, but what
-m is actually doing is looking for a file called
__main__ inside the
json.tool package. It then executes that file, so technically
-m is referencing the package.
-m, m for package. Yeah. Anyhow, don’t worry about it.
There’s a lot of stuff in here, most of it being the system library, but tacked on the end is a folder called
site-packages/. When you install a third-party package, this is where it gets put. Note that the names of the directories might be different on your machine. This is the structure used on a mac.
pip tool downloads packages from the PyPI Cheese Shop and installs them in the very same
site-packages/ directory. This is me
pip installing the
requests library. In fact, funny story:
pip itself is a Python module, and so you’ll also see it written like this.
requests library depends on other libraries. Thankfully,
pip knows this and installs the others for you. One of those dependencies is the
urllib3 library, and
requests needs a version between
Enter virtual environments. A virtual environment is a miniature copy of your Python setup inside a dedicated directory. The end result is that it gets its own
site-packages/ folder, meaning whatever is installed in one environment won’t interfere with another environment. If you need the
urllib3 library version
1.20 for one of your projects and
requests for another, you just give them separate virtual environments.
05:41 There are several tools out there for creating and managing these environments. This course doesn’t get into them in details. I mention them as they come about because of the nature of packages and their dependencies. To save some disk space, the virtual environment isn’t a complete copy of Python, but links to the important bits.
Become a Member to join the conversation.