12.4. Installing external libraries with pip
Table of Contents
Why install external libraries?
The Python standard library is powerful, but many useful tools live in external libraries created by other people. To use them, you need to install them first.
pip is the standard tool that downloads and installs these external libraries from the Python Package Index (PyPI), which is a huge online collection of Python packages.
This chapter focuses on how to use pip to install and manage these libraries.
Checking if `pip` is installed
In most modern Python installations, pip comes pre-installed.
Open your terminal (Command Prompt, PowerShell, Terminal app, etc.) and type:
pip --versionYou might see something like:
pip 24.0 from C:\Python311\Lib\site-packages\pip (python 3.11)
On some systems, pip might be tied to Python 3 and called pip3:
pip3 --version
If you see a version number, pip is available and ready.
If you get an error like “pip is not recognized” or “command not found”, try:
- Using
python -m pip(orpy -m pipon Windows) instead ofpip - Making sure Python was installed with the “Add to PATH” option (Windows)
Example:
python -m pip --version
or on Windows if python doesn’t work:
py -m pip --versionBasic `pip` install command
The core command to install a package is:
$$
\texttt{pip install package\_name}
$$
For example, to install the requests library:
pip install requests
If pip is not directly recognized, use the Python module form:
python -m pip install requestsOn Windows, this may be:
py -m pip install requestsAfter installing, you can use the library in your Python code:
import requestsInstalling a specific version
Sometimes you want a particular version of a library (for compatibility with code or tutorials).
Use == followed by the version number:
pip install requests==2.31.0Other useful version specifiers:
- Newer than or equal to a version:
pip install requests>=2.30.0- Between versions:
pip install "requests>=2.30.0,<2.32.0"
(Quotes help avoid issues with > and < in some shells.)
Upgrading an existing library
To upgrade a library to the latest version:
pip install --upgrade requestsShort form:
pip install -U requestsYou can check the installed version in Python:
import requests
print(requests.__version__)Uninstalling a library
If you no longer need a library, or want to remove a broken install:
pip uninstall requests
pip will ask you to confirm before removing it.
Listing installed packages
To see what libraries are currently installed:
pip listYou will see output like:
Package Version
---------- -------
pip 24.0
requests 2.31.0
setuptools 68.0.0To search for a specific package in the list:
pip list | find "requests" # Windows (Command Prompt)or on macOS/Linux with a typical shell:
pip list | grep requestsGetting information about a package
To see more details about an installed package:
pip show requestsThis will print:
- Version
- Install location
- Dependencies
- Homepage / project URL
This is useful if you’re debugging or want to know what version you’re using.
Installing multiple packages at once
You can install several libraries in a single command by listing them:
pip install requests flaskYou can also mix in versions:
pip install requests==2.31.0 flask>=3.0.0Using `requirements.txt` files
Projects often use a requirements.txt file to record which packages (and versions) they need. This makes it easy to set up the same environment on another computer.
A simple requirements.txt could look like:
requests==2.31.0
flask>=3.0.0,<3.1.0To install everything listed in the file:
pip install -r requirements.txtThis is especially useful when:
- Sharing a project with someone else
- Moving a project to a new computer
- Deploying a project to a server
Using `pip` with `python` or `py`
On some systems, there are multiple Python versions installed (for example, Python 3.10 and 3.12). Each version has its own packages.
To make sure you’re using pip for a specific Python:
- For “the default” Python:
python -m pip install requests- If you use the
pylauncher on Windows:
py -m pip install requests- For a specific version with
py:
py -3.12 -m pip install requestsThis ensures that the package is installed for the correct Python interpreter.
Common `pip` problems and tips
“command not found” or “is not recognized”
If you see something like:
pip: command not found'pip' is not recognized as an internal or external command
Try:
python -m pip ...py -m pip ...(Windows)
For example:
python -m pip install requestsIf that works, continue using this style.
Permission or access errors
If you see errors about permission or access (especially on macOS/Linux), avoid using sudo at first. Instead:
- Use a virtual environment for your project (often the best approach in real projects)
- Or install packages just for your user (not system-wide):
pip install --user requestsVirtual environments and deeper environment management are usually covered in more advanced material, but be aware that they exist to keep projects’ dependencies separate.
Proxy or network problems
If you are behind a corporate proxy or have connection issues, pip may fail to download packages. In that case, you may need to:
- Check that your internet connection works
- Ask your network administrator how to configure
pipwith your proxy
These setups are environment-specific and often outside Python itself.
Verifying that an installation worked
A quick way to confirm that a library installed correctly:
- Install it with
pip. - Start Python (interactive mode).
- Try importing it.
Example:
pip install requests
pythonThen in Python:
import requests
print("Requests version:", requests.__version__)If no error appears and you see a version number, the installation was successful.
Simple practice ideas
You can practice using pip with these small tasks:
- Install
requests, import it in Python, and print its version. - Install a specific version of a package (for example,
pip install requests==2.31.0) and confirm it in Python. - Create a
requirements.txtfile with at least two packages and install them usingpip install -r requirements.txt. - List all installed packages with
pip listand pick one to uninstall, then reinstall it.
These exercises will make you comfortable with the basic pip workflow you will use in almost every real-world Python project.
Views: 0
Where to Move
Move chapter:
- ☰ 1. Chapter 1: What Is Programming?
- ☰ 1.1. What is a computer program?
- ☰ 1.2. What is Python?
- ☰ 1.3. Where is Python used?
- ☰ 1.4. How programming helps solve problems
- ☰ 1.5. Your first Python program
-
- ☰ 2. Chapter 2: Setting Up Python
- ☰ 2.1. Installing Python (Windows / macOS / Linux)
- ☰ 2.2. Using Python in the terminal
- ☰ 2.3. Using an IDE (IDLE / VS Code)
- ☰ 2.4. Running Python scripts
- ☰ 2.5. Python interactive mode
-
- ☰ 3. Chapter 3: Variables and Data Types
- ☰ 3.1. What is a variable?
- ☰ 3.2. Creating variables
- ☰ 3.3. Numbers
- ☰ 3.4. Test
- ☰ 3.5. Boolean values
- ☰ 3.6. Type checking
-
- ☰ 4. Chapter 4: Basic Operations
- ☰ 4.1. Arithmetic operations
- ☰ 4.2. Working with numbers
- ☰ 4.3. String operations
- ☰ 4.4. Combining text and variables
- ☰ 4.5. Comments in Python
-
- ☰ 5. Chapter 5: User Input and Output
- ☰ 5.1. Getting input from the user
- ☰ 5.2. Converting input to numbers
- ☰ 5.3. Formatting output
- ☰ 5.4. Writing interactive programs
-
- ☰ 6. Chapter 6: Conditions
- ☰ 6.1. Comparison operators
- ☰ 6.2. Logical operators
- ☰ 6.3. if, elif, else
- ☰ 6.4. Nested conditions
- ☰ 6.5. Common beginner mistakes
-
- ☰ 7. Chapter 7: Loops
- ☰ 7.1. Why loops are useful
- ☰ 7.2. for loops
- ☰ 7.3. while loops
- ☰ 7.4. Loop counters
- ☰ 7.5. break and continue
- ☰ 7.6. Loop exercises
-
- ☰ 8. Chapter 8: Data Collections
- ☰ 8.1. Lists
- ☰ 8.2. Tuples
- ☰ 8.3. Dictionaries
- ☰ 8.4. Sets (basics)
- ☰ 8.5. Choosing the right data structure
-
- ☰ 9. Chapter 9: Functions
- ☰ 9.1. Why functions matter
- ☰ 9.2. Defining functions
- ☰ 9.3. Parameters and arguments
- ☰ 9.4. Return values
- ☰ 9.5. Scope (local vs global)
- ☰ 9.6. Reusing code
-
- ☰ 10. Chapter 10: Working with Files
- ☰ 10.1. What is a file?
- ☰ 10.2. Reading text files
- ☰ 10.3. Writing and appending files
- ☰ 10.4. File paths
- ☰ 10.5. Simple file-based programs
-
- ☰ 11. Chapter 11: Errors and Debugging
- ☰ 11.1. Types of errors
- ☰ 11.2. Reading error messages
- ☰ 11.3. Common beginner errors
- ☰ 11.4. try and except
- ☰ 11.5. Debugging strategies
-
- ☰ 12. Chapter 12: Using Python Libraries
- ☰ 12.1. What is a library?
- ☰ 12.2. Importing modules
- ☰ 12.3. Standard libraries
- ☰ 12.3.1. math
- ☰ 12.3.2. random
- ☰ 12.3.3. datetime
-
- ☰ 12.4. Installing external libraries with pip
-
- ☰ 13. Chapter 13: Object-Oriented Programming
- ☰ 13.1. What is object-oriented programming?
- ☰ 13.2. Classes and objects
- ☰ 13.3. The `__init__` method
- ☰ 13.4. Attributes and methods
- ☰ 13.5. Encapsulation
- ☰ 13.6. Inheritance (basics)
- ☰ 13.7. Simple OOP examples
-
- ☰ 14. Chapter 14: Automation with Python
- ☰ 14.1. What is automation?
- ☰ 14.2. Automating simple tasks
- ☰ 14.3. Working with files and folders
- ☰ 14.4. Automating text processing
- ☰ 14.5. Scheduling scripts
- ☰ 14.6. Practical automation examples
-
- ☰ 15. Chapter 15: Web Development with Python
- ☰ 15.1. How the web works (basic overview)
- ☰ 15.2. Introduction to Flask
- ☰ 15.3. Creating a simple web server
- ☰ 15.4. Routing and templates
- ☰ 15.5. Handling user input (forms)
- ☰ 15.6. Very basic APIs
-
- ☰ 16. Chapter 16: Data Science with Python
- ☰ 16.1. What is data science?
- ☰ 16.2. Working with data
- ☰ 16.3. Introduction to NumPy
- ☰ 16.4. Introduction to pandas
- ☰ 16.5. Data visualization with matplotlib
- ☰ 16.6. Simple data analysis examples
-
- ☰ 17. Chapter 17: Mini Projects
- ☰ 17.1. Number guessing game
- ☰ 17.2. Simple calculator
- ☰ 17.3. To-do list
- ☰ 17.4. Password generator
- ☰ 17.5. Quiz application
- ☰ 17.6. Small web or data project
-
- ☰ 18. Chapter 18: Next Steps
- ☰ 18.1. How to practice Python
- ☰ 18.2. Writing clean and readable code
- ☰ 18.3. Choosing a specialization
- ☰ 18.3.1. Web development
- ☰ 18.3.2. Data science
- ☰ 18.3.3. Automation
- ☰ 18.3.4. Software development
-
- ☰ 18.4. Learning resources and communities
-
- ☰ 19. Appendix
- ☰ 19.1. Python syntax cheat sheet
- ☰ 19.2. Common errors and solutions
- ☰ 19.3. Glossary of terms
-
Comments
Please login to add a comment.
Don't have an account? Register now!