GICT knowledge base
Beginner’s Guide to pip: How to Install and Manage Python Packages
Published
Python is one of the most popular programming languages today. It is simple to learn and powerful to use. One big reason for its popularity is the huge number of packages available.
A Python package is a collection of code that adds extra features to Python. Instead of writing everything yourself, you can use these ready-made tools. For example, you can use a package to handle data, build websites, or even work on machine learning projects.
Packages save time and reduce mistakes. They are already tested and used by many developers around the world. For instance, NumPy and pandas are useful for data science, while Django and Flask are popular for web development. Using packages lets you focus on solving problems instead of coding everything from scratch.
To manage these packages, Python uses a tool called pip. The name stands for “Pip Installs Packages.” Pip is included in most Python setups. With pip, you can install new packages, upgrade old ones, or remove the ones you do not need anymore.
Pip works with the Python Package Index (PyPI), which is an online store of packages. It has thousands of free packages made by the community.
In this guide, you will learn how to use pip step by step. By the end, you will be able to manage Python packages with confidence.
What is pip?
Definition and Role of pip
Pip is the official package manager for Python. It allows you to install, upgrade, and remove Python packages with simple commands. Instead of downloading and copying files manually, pip handles everything for you.
In the early days of Python 2, developers used another tool called easy_install to manage packages. However, it had many limitations and was not very user-friendly. With the release of Python 3, pip became the default tool. Today, pip comes pre-installed with most Python versions, making it the standard way to manage packages.
The reason pip became popular is its ease of use. A single command like pip install requests can bring powerful libraries into your project. This makes Python development faster and more efficient.
How pip Works Under the Hood
When you run a pip command, it connects to PyPI (Python Package Index). PyPI is a large online repository that stores over half a million packages created by developers worldwide. Pip searches PyPI, downloads the right files, and installs them into your Python environment.
Pip also manages dependencies. A dependency is another package that your chosen package needs to work. For example, if you install a web framework that requires other libraries, pip will automatically download and install them.
In short, pip is like a bridge between your Python code and the global library of tools available on PyPI.
Installing pip
Checking if pip is Installed
Before using pip, you need to check if it is already available on your system. On Windows, open the Command Prompt and type py -m pip --version or python -m pip --version. If pip is installed, you will see its version number.
On macOS and Linux, open the Terminal and type python3 -m pip --version. You can also use which pip3 to check its location. If you get an error or no output, pip is either missing or not added to your system path.
Installing or Upgrading pip
If pip is missing, you can install it using Python’s built-in tool called ensurepip. Run python -m ensurepip --upgrade in your terminal or command prompt, and pip will be installed or updated automatically.
Another method is by downloading the get-pip.py script from the official website. Once downloaded, run python get-pip.py to install pip, setuptools, and wheel together. This method is often used when ensurepip is not available.
If pip is installed but outdated, you can upgrade it with a simple command: python -m pip install --upgrade pip. On Linux and macOS, you may need to add --user at the end, or use sudo if permission issues occur.
After completing the installation or upgrade, run the version command again to confirm that pip is working and up to date.
Basic pip Commands Every Beginner Should Know
Installing a Package
The most common use of pip is installing new packages. The general syntax is:
pip install package_name
For example, if you want to install the requests library, you can type pip install requests. Pip will download the package from PyPI and install it into your Python environment. After this, you can start using it in your code with a simple import requests.
Installing Specific Versions
Sometimes you need a particular version of a package. This is common when your project depends on older versions that work well together. The syntax for this is:
pip install package_name==version
For instance, running pip install requests==2.28.0 installs version 2.28.0 of the requests package. This ensures compatibility and prevents your code from breaking due to changes in newer releases.
Upgrading and Uninstalling Packages
If you want to update a package to the latest version, you can use:
pip install --upgrade package_name
For example, pip install --upgrade requests will upgrade the requests library.
To remove a package you no longer need, run:
pip uninstall package_name
This keeps your environment clean and free from unused libraries.
Listing Installed Packages
To see all the packages currently installed, type:
pip list
This shows the package names and versions. Another useful command is:
pip freeze
pip freeze is often used to create a requirements.txt file because it lists packages in a format that pip can read later. Beginners can start with pip list for a quick overview and use pip freeze when preparing to share or deploy a project.
Working with Requirements Files
Creating a requirements.txt
When you work on Python projects, you often install many packages. To keep track of them, you can create a requirements.txt file. This file lists all the packages and their versions. The easiest way to make one is by running:
pip freeze > requirements.txt
This command saves the current environment’s packages into a text file. You can then share this file with others so they can set up the same environment.
Installing from a requirements.txt
If you get a project that already has a requirements.txt file, you can install everything in one go. The command is:
pip install -r requirements.txt
This reads the file and installs all the packages listed inside. It is very useful when setting up projects on a new computer or when working in a team.
Best Practices for requirements.txt
When making a requirements file, you can either pin versions or keep them flexible. Pinning versions means writing exact versions, like numpy==1.24.0. This ensures the code always runs with the same package versions.
Flexible versions, like numpy>=1.24, give more freedom but may cause issues if new versions change behavior. For most projects, pinning versions is safer. Updating can be done later in a controlled way.
Virtual Environments and pip
Why Use Virtual Environments?
When you start working on multiple Python projects, you may face dependency conflicts. This happens when two projects need different versions of the same package. For example, one project might need Django 3, while another requires Django 4. If both are installed globally, they will clash.
A virtual environment solves this problem by creating a separate space for each project. Inside this space, you can install packages without affecting other projects or the system Python. This makes your work more reliable and keeps projects fully isolated.
Creating a Virtual Environment
Python comes with a built-in module called venv. You can use it to create virtual environments.
On Windows, open Command Prompt and run:
python -m venv env
This creates a folder named env that holds the environment.
On macOS and Linux, the command is:
python3 -m venv env
You can replace env with any name you like.
Using pip Inside Virtual Environments
After creating the environment, you need to activate it. On Windows, run:
env\Scripts\activate
On macOS and Linux, run:
source env/bin/activate
Once activated, your terminal will show the environment name at the beginning of the line. Any package you install with pip now goes only into this environment. For example, pip install requests will install requests just for this project.
To leave the environment, type deactivate. You can always reactivate it later when you return to the project.
Common Errors and How to Fix Them
Version Conflicts
One common issue is version conflicts. This happens when two packages depend on different versions of the same library. For example, installing one package may downgrade another, causing your code to break. The easiest fix is to use a virtual environment so each project keeps its own versions. You can also check conflicts with pip check and then manually adjust versions in your requirements.txt.
Permission Errors
Another frequent problem is permission errors, often seen on Linux or macOS. This usually appears when you try to install a package globally without admin rights. A simple solution is to add the --user flag, like this:
pip install --user package_name
This installs the package only for your user account. A better long-term solution is to use a virtual environment. That way, you never need admin access, and packages stay isolated per project.
Outdated pip Issues
Sometimes errors occur because pip itself is outdated. Older versions may not support the latest packaging standards. If you see strange installation errors, try upgrading pip with:
python -m pip install --upgrade pip
This updates pip to the newest release and often solves installation issues.
Advanced pip Features
Installing from GitHub or Local Files
Pip can install packages not only from PyPI but also directly from GitHub or local files. This is useful when a package is not yet on PyPI or if you want to use the latest development version. For example, you can install from GitHub with:
pip install git+https://github.com/user/repo.git
You can also install from a local wheel file or folder:
pip install ./package_folder
This is often used when testing your own package before publishing it.
Using pip Options
Pip has many extra options to control how installations work. The --no-cache-dir option prevents pip from using cached files, which is useful if you face corruption issues. The --pre option lets you install pre-release versions of a package. There are also flags like --quiet to reduce output or --upgrade to update packages. Learning these options gives you more control over package management.
Managing Multiple Python Versions
If you have multiple versions of Python installed, you may need to manage packages separately. On many systems, pip is linked to Python 2 while pip3 is for Python 3. To be safe, you can run pip as a module:
python3 -m pip install package_name
This ensures pip works with the correct Python version you are using.
Best Practices for Using pip
Using pip becomes much easier when you follow a few good habits. These practices will keep your projects clean, safe, and easy to maintain.
The first and most important rule is to always use virtual environments. This prevents conflicts between projects and keeps your global Python installation untouched. Each project should have its own isolated environment with the exact packages it needs.
It is also a good idea to regularly update pip and your packages. An old version of pip may cause errors when installing modern packages. Similarly, updating your libraries ensures you get the latest features and security fixes. You can upgrade pip with python -m pip install --upgrade pip. For packages, run pip list --outdated to see which ones need updates.
Another habit is to keep your requirements.txt file updated. This file is essential when sharing your project with others or deploying it to a server. You can regenerate it anytime with pip freeze > requirements.txt. This makes sure your file always matches your current environment.
Finally, always document your dependencies. If your project relies on a specific library or version, note it in your project’s documentation or README. This helps teammates and future you understand why certain packages were chosen.
By following these simple best practices, you can avoid common problems and make your Python projects easier to manage.
Conclusion
Pip is one of the most important tools in the Python ecosystem. It allows you to install, update, and remove packages with simple commands. Throughout this guide, you learned what pip is, how to install it, and how it connects to the Python Package Index (PyPI).
You also explored key commands such as installing, upgrading, and uninstalling packages, as well as using requirements.txt files and virtual environments.
By now, you should see why pip is essential for every Python project. It saves time, avoids manual work, and helps manage dependencies smoothly. The more you practice pip commands, the more confident you will become in handling Python packages.
As your projects grow, you will find virtual environments especially useful for keeping your work organized and conflict-free. If you want to explore further, check out tutorials on virtualenv, conda, and the PyPI repository to discover more advanced ways of managing packages.
Start small by installing a few popular packages, try upgrading them, and experiment with requirements files. With practice, pip will become second nature, and managing Python packages will no longer feel challenging.
Related guides
How to Deploy a FastAPI App with Gunicorn and Nginx
Deploy FastAPI behind Nginx with Gunicorn and systemd. Separate application packages, check HTTPS and verify worker replacement and application health.
PythonLearn How to Modify Python Dictionary Values Easily
Learn how to modify Python dictionary values effectively with various methods, including direct assignment, update(), and comprehensions, along with common mistakes to...
PythonPython Set Copy Tutorial: Avoid Common Mistakes and Data Errors
Learn how to copy sets in Python safely and efficiently while avoiding common mistakes. Explore methods like copy(), set(), and deepcopy().
PythonDifferent Methods to Loop Through Sets in Python (Beginner-Friendly Guide)
Learn diverse methods to loop through sets in Python, including practical examples and use cases, ideal for beginners looking to master set operations.
