Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: The problem with pip

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You often hear that Python comes with batteries included. This means that it comes with plenty of packages already installed.
0:08 This is called the Standard Python Library. You can Goto Python Modules Index website,
0:13 and here you can find the list of all the packages that comes with Python. So, without installing anything, you can just import them in your code.
0:21 But what makes Python truly amazing is the Python Package Index. This list of over 260,000 packages created by other developers.
0:33 If you find the module that you want to use, all you need to do is to run pip,
0:37 install and the modules name, and this will download and install that package for you
0:42 Now you could import this module in your code and start using any function that this module offers. With so many Python packages,
0:51 you can quickly built a pretty advanced projects simply by combining different packages together. However, pip has one big problem.
0:59 You can only have one version of a given package installed on your computer. Whenever you ask it to install a specific version of a Python package,
1:08 it will uninstall the previous versions from your computer and installed the one that you asked for. Let me show you an example of what I mean.
1:17 Let's say you are a Web developer and you want to build a Django website,
1:21 so, you create a folder for the application and then you start by installing the latest version of Django with pip install Django Command.
1:32 As you can see it, this installs Django 3. Everything works great. You build an awesome website and it's actually so good that soon
1:41 you get a customer coming to you and asking to fix their Django website.
1:46 Their Django website is still running on Django to so you go out from this folder
1:51 you create a new one and then you installed Django 2 by specifying pip install django==2.2 Let's say, pip does what you ask for,
2:03 and after a few seconds you start working on your client's website using Django 2.2. So far, so good. But later that day,
2:11 you discover a bug on your personal website, the one that you created with Django 3.
2:16 You quickly fix the code, but when you want to test that is working correctly you get error message saying that Django 2 is not installed.
2:23 What? Wait. But we just installed a few days ago, where did it go? Well, when we told pip to install Django 2.2 pip first checked if we already
2:33 have Django install and we did. But it was not the 2.2 version, so pip uninstalled that version and installed the correct one.
2:41 As you can see here, it says uninstalling Django 3 and successfully installed Django 2. And if we check the list of packages installed on our computer,
2:51 you will see that we no longer have Django 3. So, we just run into a problem with dependencies management.
2:58 All the Python packages that you install with pip are called dependencies because the projects you
3:03 are building depends on them. pip installs those dependencies in the site packages folder and
3:09 puts each package in a separate folder named after the package. So, when we installed Django 3, it was placed in site packages/django folder.
3:19 But when we tried to install Django 2, it was also placed in the same folder,
3:25 so, pip has the first remove what's inside the Django folder and then install different version
3:30 of Django. If you're only working with one Python project on your computer, then you are probably not affected by this problem.
3:39 But sooner or later you will need to install a different version of a package,
3:43 and you're going to run into issues with pip uninstalling some previous dependencies. Dependencies management problems are not specific.
3:52 to Python. Basically, any programming language that allows installing external modules have to face
3:58 this issue, and different programming languages solve it in a different way.
4:03 For example, developers who used Ruby on rails install their packages with a tool called bundler. And when Bundler installs a new dependency,
4:11 it puts it in a folder called Package Name-Version. So it's possible to have two different versions of a package.
4:18 For example, here you can see we have active support 5.2.3 and 6.0 and so on. JavaScript or, more specifically, node.
4:33 So, the server side version of JavaScript solves this problem by installing all the dependencies inside
4:39 the folder called node modules inside of your project.
4:43 So, each different project on your computer has a different folder where the dependencies are stored
4:50 right now, in Python pip doesn't have a good solution for this problem.
4:54 However, there is a proposal to use a local packages directory in the same manner as JavaScript developers do. If it gets implemented,
5:02 pip would be installing all the dependencies in the folder inside of your project, not globally. This proposal is still draft,
5:09 but maybe in a future version of Python problem with dependencies will be solved.
5:14 But for now, most Python developers solved this problem using virtual environments.

Talk Python's Mastodon Michael Kennedy's Mastodon