Hello! Welcome to your sixth and final lesson in the "Python Fundamentals for Web Development" module.
In our last lesson, we learned how to build more robust programs by handling errors with try-except blocks. This was a crucial step in writing code that can gracefully handle unexpected situations, a key feature of any real-world application.
With this lesson, you've completed all the core Python language concepts for now. You can write scripts, work with data, create functions, and manage errors. But Python's true power comes from the vast community of developers who have created pre-built code packages that we can easily add to our own projects.
Today, we'll learn how to tap into this ecosystem. The learning outcome for this lesson is to install and manage external Python packages using pip. This is the tool that will allow us to install Flask (our web framework), the Google Gemini library for our AI features, and any other tools we'll need to build your web application.
What is pip?
Python comes with a "standard library" full of useful modules. However, there are millions of other packages for specialized tasks—from web development to data science—that don't come with Python by default. These are stored in a central online repository called the Python Package Index (PyPI).
pip (which stands for "Pip Installs Packages") is the command-line tool we use to download and manage these packages from PyPI.
Since pip is a command-line tool, you'll need to open your system's terminal (on macOS/Linux) or Command Prompt (on Windows).
- Windows: Press the Windows key, type
cmd, and press Enter. - macOS: Open the Spotlight search (Cmd+Space), type
Terminal, and press Enter.
Getting Started with pip
For modern versions of Python, pip is included by default. To start, let's learn what pip is and check if it's ready to go on your system.
This short video from Bro Code gives a quick overview of what pip is and shows you how to check if it's installed.
Watch from the beginning to 0:57. Pay attention to the commands used in the terminal to interact with pip.
As the video shows, you can check your pip version by typing this command in your terminal and pressing Enter:
pip --version
If that gives you an error, try pip3 --version.
A Best Practice: Using python -m pip
Sometimes, you might have multiple versions of Python on your system, which can cause confusion about which version of pip is being used. A great way to avoid this is to run pip as a Python module.
Using Python's pip to Manage Your Projects' Dependencies
This article from Real Python explains a more robust way to use pip that guarantees you're using the installer associated with your main Python version.
Read the subsections 'Getting Started With pip' and 'Running pip as a Module'. This will reinforce what pip is and introduce the python -m pip command.
From now on, we'll use the python -m pip syntax. To check your version this way, you would run:
python -m pip --version
This command tells your system to find your Python interpreter and then use its specific pip module. It's a bit longer to type but much safer.
The Essential pip Commands
Now that we know pip is ready, let's learn the core commands you'll use constantly. We'll cover how to find, install, and remove packages.
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer's tutorial provides an excellent, clear demonstration of the main pip commands. It's a great walkthrough of the day-to-day workflow.
Watch from 01:06 to 03:30. This section covers installing a package (pip install), listing all installed packages (pip list), and removing a package (pip uninstall). You'll also see how to check for outdated packages.
Let's recap the commands from the video:
- Install a package:
python -m pip install <package_name>- This downloads the package from PyPI and installs it.
- List installed packages:
python -m pip list- This shows you all the packages currently installed in your environment, along with their versions.
- Uninstall a package:
python -m pip uninstall <package_name>- This removes a package.
pipwill ask you to confirm before deleting.
- This removes a package.
Managing Dependencies for Your Project
When you start building your web application, you'll install several packages (like Flask). If you want to share your project with someone else, or deploy it to the internet, you need a way to tell them exactly which packages and versions your project needs to run.
This is where requirements files come in. The standard convention is to create a file named requirements.txt.
Creating a requirements.txt file
You can automatically generate this file based on the packages you've already installed using the freeze command.
- First, let's install a package. The
requestslibrary is a very popular package for making HTTP requests (which we'll do later!). Let's install it:python -m pip install requests - Now, run the
freezecommand. The>symbol redirects the output of the command into a file.python -m pip freeze > requirements.txt
This command will create a requirements.txt file in your current directory. If you open it, you'll see something like this (your versions may differ):
certifi==2023.7.22
charset-normalizer==3.3.0
idna==3.4
requests==2.31.0
urllib3==2.0.7
Notice that pip also included the dependencies of requests—other packages that requests needs to function. This file is a complete "snapshot" of your project's environment.
Installing from a requirements.txt file
Now, the magic happens. If you give this requirements.txt file to another developer, they can install all the exact same package versions with a single command:
python -m pip install -r requirements.txt
The -r flag tells pip to read the requirements from the specified file. This makes your project reproducible and easy to share.
To see this whole workflow in action, let's return to the Corey Schafer video.
Python Tutorial: pip - An in-depth look at the package management system
This part of the video explains the purpose of a requirements.txt file and demonstrates the freeze and install -r commands.
Watch from 03:30 to 05:39. Focus on understanding how pip freeze captures the environment and how pip install -r recreates it.
Test your understanding!
You are starting a new project and have installed two packages, flask and numpy. You need to prepare your project so that your teammate can set up the exact same environment on their computer.
What single command would you run to create the necessary file for them?
Show answer
You would run the following command in your terminal:
python -m pip freeze > requirements.txt
This creates the requirements.txt file, which you would then share with your teammate. They could then run python -m pip install -r requirements.txt to install flask, numpy, and all their dependencies.
A Quick Note on Virtual Environments
As you work on more projects, you'll find that Project A might need version 1.0 of a package, while Project B needs version 2.0. If you install everything globally (as we've been doing), you'll run into conflicts.
The solution is virtual environments. A virtual environment is an isolated Python environment that allows each project to have its own set of installed packages. It's like having a separate, clean toolbox for every project you work on.
We won't go deep into creating them today, but it's a critical concept. When we start building our Flask application in the next module, our very first step will be to create a virtual environment for our project. If you're curious, the Real Python article we looked at earlier has an excellent section on this. You can read the subsection titled "Using pip in a Python Virtual Environment" in the article "Using Python's pip to Manage Your Projects' Dependencies" for a preview.
Key Takeaways
You've just learned one of the most essential skills for any Python developer. Mastering pip is the key to unlocking the vast potential of the Python ecosystem.
pipis the package manager for Python, used to install and manage third-party libraries from the Python Package Index (PyPI).- It's a best practice to run
pipusingpython -m pip ...to avoid version conflicts. - The essential commands are
install,uninstall, andlist. - The
pip freeze > requirements.txtcommand saves a snapshot of your project's dependencies. - The
pip install -r requirements.txtcommand installs all dependencies from a requirements file, making your project portable and reproducible.
Next Steps
This lesson marks the end of our "Python Fundamentals" module. Congratulations! You now have all the foundational Python skills needed to start building your web application.
In our next lesson, we will begin Module 2: Building the Web Page Structure with HTML. We'll leave the terminal for a bit and start creating the user-facing part of our application. Our first task will be to set up our project folder, create a virtual environment, and write the basic HTML that forms the skeleton of any webpage.