Skip to main content

Python Installing and using external packages with pip

 Installing and Using External Packages with pip

Python's pip (Python Package Installer) is a powerful tool for managing external packages. This guide will walk you through the basics of installing and using external packages with pip.

Installing pip

pip usually comes pre-installed with Python. You can check if pip is installed by running:

pip --version


If pip is not installed, you can install it by following the instructions on the official pip installation page.

Installing Packages

To install a package, use the pip install command followed by the package name:

pip install package_name

For example, to install the requests package, you would run:

pip install requests


Installing Specific Versions

To install a specific version of a package, specify the version number:

pip install package_name==version

For example:

---------

---------

Upgrading Packages

To upgrade an existing package to the latest version, use the --upgrade flag:

pip install --upgrade package_name

Listing Installed Packages

To list all installed packages, use:

pip list

Checking for Outdated Packages

To check for packages that have newer versions available:

pip list --outdated

Uninstalling Packages

To uninstall a package, use the pip uninstall command followed by the package name:

pip uninstall package_name

Using Installed Packages

Once a package is installed, you can import and use it in your Python code. For example, after installing requests, you can use it as follows:

import requests response = requests.get('https://www.example.com') print(response.text)


-------

-------

Requirements Files

For managing project dependencies, you can create a requirements.txt file listing all the packages your project needs. Each line should contain the package name and optionally the version number.

Example requirements.txt:

requests==2.25.1 flask>=1.1.2 numpy


To install all the packages listed in a requirements.txt file, use:

pip install -r requirements.txt

Virtual Environments

It's a good practice to use virtual environments to manage dependencies for different projects. Virtual environments isolate the packages installed for each project.

Creating a Virtual Environment

Use venv to create a virtual environment:

python -m venv env_name

Activating a Virtual Environment

Windows:

env_name\Scripts\activate

macOS/Linux:

source env_name/bin/activate

Deactivating a Virtual Environment

To deactivate the virtual environment, simply run:

deactivate

Example Workflow

Create a Virtual Environment:

python -m venv myenv

Activate the Virtual Environment:

Windows:

myenv\Scripts\activate

macOS/Linux:

source myenv/bin/activate

Install Packages:

pip install requests flask numpy

Freeze Installed Packages:

pip freeze > requirements.txt

Deactivate the Virtual Environment:

deactivate

Recreate the Environment Later:

python -m venv myenv source myenv/bin/activate pip install -r requirements.txt

By following these steps, you can effectively manage and use external packages in your Python projects.


Comments