Python's pip is a command for managing packages.
Packages are multi-functional extensions available in python, and you can install the required features in a package to implement your program efficiently.
Here is an easy-to-understand explanation of how to use the pip command.
This is written by a Japanese who can't speak English with the help of translation application. Sorry if it's not good.
- What is a Python package?
- Difference between pip and pip3
- How to install the package
- How to update the package
- How to uninstall the package
- How to display the package list
- How to check for package updates
- Subcommands of pip
- install - Install / Update
- uninstall : Uninstall
- list : List display
- freeze : Export the package list
- show : Display detailed package information
- check : Package dependency check
- help : Display help
- -V : Version check
What is a Python package?
Python has many features that can be added. These features are available as packages on GitHub and other sites. The tool to manage these packages is pip.
You can think of it as being similar to composer in php or gem in Ruby.
Language | Package Management |
---|---|
Python | pip |
PHP | composer |
Ruby | gem |
The pip command is used to add, update, and remove packages. Also, frameworks such as Django can be easily installed using this pip.
In Python, you should always assume that pip is used.
Difference between pip and pip3
The major update from Python 2.x series to 3.x series has changed a lot.
The v2 and v3 are no longer compatible, and it is difficult to migrate to v3. v2 programs may not work on v3. So we have to make sure that v2 and v3 coexist.
The pip command is also separated.
version | command |
---|---|
2.x | pip |
3.x | pip3 |
The functions of pip and pip3 commands are the same, only the target version is different.
When you are using only v3, you can use both pip and pip3 and the behavior is the same.
The pip command is installed by default in Python since v2.7, so there is no need to install it.
In this article, I will introduce you to five commands that are the minimum required to install the package.
How to install the package
python -m pip install 'package_name'
To install the package, use the install command.
The pip command is located in the Scripts directory of your python installation. This directory is usually set in the PATH, and "python -m" can be omitted.
On Windows, the PTAH setting for Scripts is also set during installation.
How to update the package
python -m pip install -U 'package_name'
To update the package, use the install command. If you specify the -U (--upgrade) option, it will be an update.
How to uninstall the package
python -m pip uninstall 'package_name'
To uninstall the package, use the uninstall command.
The install / uninstall command is "install" / "uninstall , so it's easy to remember.
How to display the package list
python -m pip list
Displays the list of installed packages.
How to check for package updates
python -m pip list -o
Check if there is a version of the package to be updated.
Subcommands of pip
There are also many other commands available. Here are some of the most commonly used ones.
install - Install / Update
python -m pip install 'package_name'
Install / Upgrade the package.
Option
-U, --upgrade: Update
python -m pip install -U 'package_name'
Upgrade the package.
python -m pip install --upgrade pip
Upgrade the pip command itself.
-r : Import installation
python -m pip install -r import.txt
Install from the package list file (import.txt) created by the freeze command.
It is used to install packages together. It is also used to migrate the python environment.
uninstall : Uninstall
python -m pip uninstall 'package_name'
Uninstall the package.
list : List display
python -m pip list
Displays the list of installed packages.
Option
-o : Update confirmed
python -m pip list -o
Displays the packages that can be updated.
-u : display latest version package
python -m pip list -u
Displays the packages that contain the latest version.
-e : Development mode package display
python -m pip list -e
Displays the packages installed with "install -e".
The packages put in by "install -e" will be installed by setuptools. It is used for packages under development, so it is not used in the production environment.
--pre : Beta version display
python -m pip list --pre
It also shows the beta version.
It is not stable, but it has a lot of challenges such as the latest features.
freeze : Export the package list
python -m pip freeze > export.txt
Outputs the list of currently installed packages to a file.
The output files can be installed together with "pip install -r".
show : Display detailed package information
python -m pip show 'package_name'
Displays detailed information about the package.
check : Package dependency check
python -m pip check
Check for package dependencies.
Some packages do not work by themselves and require other packages.
Package A needs package B
It goes on to say
Package A depends on package B
help : Display help
python -m pip help
Displays help for the pip command.
-V : Version check
python -m pip -V
Displays the version of the pip command.