Python Notes Help

PIP

pip is the Package Installer for Python, allowing to easily manage libraries and dependencies for projects.

Installing a Package

To install a specific package, use the following command:

pip install package_name

For example, to install requests, run:

pip install requests

In some cases, a package can be installed directly from its Git repository using the following command:

pip install git+https://website.com/repository.git

For example, to install Flask directly from GitHub, run:

pip install git+https://github.com/pallets/flask.git

Installing Packages from a Requirements File

If you have a requirements.txt file that lists the packages for your project, you can install all dependencies with:

pip install -r requirements.txt

This command will read the file and install all the packages specified.

Uninstalling a Package

To uninstall a specific package, use the following command:

pip uninstall package_name

For example, to uninstall requests, run:

pip uninstall requests

Uninstalling All Packages

To remove all installed packages in your environment, use:

pip freeze | xargs pip uninstall -y

This command will list all installed packages and uninstall them in a single operation.

Listing Installed Packages

To list all installed packages in your virtual environment, use:

pip freeze

This will display a list of all packages and their respective versions.

An alternative, more user-friendly version of this command is:

pip list

Managing Dependencies with requirements.txt

The requirements.txt file is a standard way to list all the dependencies for your Python project.

It allows to easily share and install the exact versions of the packages needed for a project.

You can generate a requirements.txt file using the following command:

pip freeze > requirements.txt

This command will list all the installed packages and their versions in the current environment and save them to the requirements.txt file.

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

pip install -r requirements.txt

This will automatically install the exact versions of the packages specified in the file, ensuring your environment is set up exactly as required.

Last modified: 17 March 2025