Python Package Management: Basic Usage of pip and Environment Setup (install, uninstall, requirements.txt)

One of Python’s greatest strengths is its rich ecosystem of “external libraries (packages)” for data analysis, web development, and automation. The standard tool used to manage (install, uninstall, version control) these packages is pip.

This article explains the basic commands of pip and how to manage environments, which is essential for project development.


目次

What is pip?

pip is Python’s package management system. It allows you to easily download and install packages registered in PyPI (Python Package Index), the official package repository.

1. Installing Packages (pip install)

This is the most basic command used to install a new external library.

Syntax:

pip install <package_name>

Example: To install pandas, a data analysis library, run the following command in your terminal:

pip install pandas

2. Uninstalling Packages (pip uninstall)

Removes an unnecessary package from your environment.

Syntax:

pip uninstall <package_name>

Example: To remove pandas:

pip uninstall pandas

When executed, it lists the files to be deleted and asks for confirmation (Proceed (y/n)?). Type y to proceed.

3. Upgrading Packages (pip install -U)

Updates an installed package to the newest version available on PyPI. -U is short for --upgrade.

Syntax:

pip install -U <package_name>

Example: To update pandas to the latest version:

pip install -U pandas

4. Project Environment Management (requirements.txt)

In Python development, it is common to fix the libraries and versions used for each project. This allows other developers to reproduce the same environment or deploy the code smoothly. The file requirements.txt is widely used for this purpose.

pip freeze: Freezing the Environment

The pip freeze command lists currently installed packages (and their versions). You can redirect (>) this output to a file to save it.

Syntax:

pip freeze > requirements.txt

This creates a requirements.txt file in your current directory with content like this:

pandas==2.2.0
numpy==1.26.3
python-dateutil==2.9.0
...

pip install -r: Installing from a File

If you have a requirements.txt file, pip can install all listed packages at the specified versions at once. -r is short for --requirement.

Syntax:

pip install -r requirements.txt

This allows you to quickly build the exact same Python environment on another PC or server.


Supplement: Using Virtual Environments (venv)

By default, pip installs packages globally (system-wide). However, if Project A needs pandas version 1.0 and Project B needs version 2.0, conflicts will occur. To avoid this, using a Virtual Environment (venv) is strongly recommended.

venv creates an isolated Python environment and pip configuration for each project.

# 1. Create a virtual environment named 'my_project_env'
python -m venv my_project_env

# 2. Activate the virtual environment (Windows)
my_project_env\Scripts\activate

# 2. Activate the virtual environment (macOS / Linux)
source my_project_env/bin/activate

Once activated, pip install will install packages only inside my_project_env, keeping your system clean.


Summary

pip is an essential tool for Python development.

  • pip install <pkg>: Install a package.
  • pip uninstall <pkg>: Uninstall a package.
  • pip install -U <pkg>: Upgrade a package.
  • pip freeze > requirements.txt: Export environment settings.
  • pip install -r requirements.txt: Reproduce environment from file.

By combining these commands with venv (virtual environments), you can maintain a clean and reproducible development environment.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次