What is pip and how do you use it?

What is pip?

pip is one of python's package managers, others include conda and pipenv. In python3 series, if it is version 3.4 or later, pip will be installed at the same time as python is installed. You can install and manage packages that are not included in the python standard library.

Let's see how to use pip in a virtual environment

Check if you can use pip:

$ pip --version
pip 10.0.1 from /Users/place/venv/lib/python3.7/site-packages/pip (python 3.7)

pip update

$ pip install --upgrade pip

Recheck pip version

$ pip --version
pip 19.3.1 from /Users/place/venv/lib/python3.7/site-packages/pip (python 3.7)

Let's take a look at the commands you can use with pip:

$ pip help

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode,
                              ignoring environment variables and
                              user configuration.
  -v, --verbose               Give more output. Option is
                              additive, and can be used up to 3
                              times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is
                              additive, and can be used up to 3
                              times (corresponding to WARNING,
                              ERROR, and CRITICAL logging
                              levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each
                              connection should attempt (default
                              5 times).
  --timeout <sec>             Set the socket timeout (default 15
                              seconds).
  --exists-action <action>    Default action when a path already
                              exists: (s)witch, (i)gnore, (w)ipe,
                              (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as
                              trusted, even though it does not
                              have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a
                              single file containing the private
                              key and the certificate in PEM
                              format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to
                              determine whether a new version of
                              pip is available for download.
                              Implied with --no-index.
  --no-color                  Suppress colored output

Install package with pip

The standard python library is extensive, but there are other python frameworks, tools, libraries, etc. created by developers around the world on the Python Package Index (PyPI). ) Is published. (Read as pie pie, not pie pie.) Check the packages installed in the current environment with pip list (nothing installed yet):

$ pip list
Package    Version
---------- -------
pip        19.3.1
setuptools 39.0.1

Use the pip install command to install the package you want to use from PyPI. If you want to use the machine learning library scikit-learn:

$ pip install scikit-learn
Collecting scikit-learn
  Downloading https://files.pythonhosted.org/packages/82/d9/69769d4f79f3b719cc1255f9bd2b6928c72f43e6f74084e3c67db86c4d2b/scikit_learn-0.22.1-cp37-cp37m-macosx_10_6_intel.whl (11.0MB)
     |████████████████████████████████| 11.0MB 851kB/s
Collecting scipy>=0.17.0
  Using cached https://files.pythonhosted.org/packages/85/7a/ae480be23b768910a9327c33517ced4623ba88dc035f9ce0206657c353a9/scipy-1.4.1-cp37-cp37m-macosx_10_6_intel.whl
Collecting joblib>=0.11
  Downloading https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl (294kB)
     |████████████████████████████████| 296kB 1.1MB/s
Collecting numpy>=1.11.0
  Using cached https://files.pythonhosted.org/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl
Installing collected packages: numpy, scipy, joblib, scikit-learn
Successfully installed joblib-0.14.1 numpy-1.18.1 scikit-learn-0.22.1 scipy-1.4.1

Check the packages installed in your current environment:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1

I just installed scikit-learn, but joblib, numpy, and scipy are installed in addition to scikit-learn. This is because scikit-learn depends on other packages joblib, numpy and scipy. In other words, scikit-learn alone does not work, so it installed other required packages together.

You can check the version and dependency information of installed packages with the pip show command:

$ pip show scikit-learn
Name: scikit-learn
Version: 0.22.1
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: /Users/place/venv/lib/python3.7/site-packages
Requires: scipy, joblib, numpy
Required-by:

Requires: scipy, joblib, numpy, so you can see that scikit-learn depends on scipy, joblib, numpy. Since Required-by: is blank, you can confirm that there are no packages that depend on scikit-learn so far.

Let's also take a look at the scipy installed with it:

$ pip show scipy
Name: scipy
Version: 1.4.1
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author: None
Author-email: None
License: BSD
Location: /Users/place/venv/lib/python3.7/site-packages
Requires: numpy
Required-by: scikit-learn

You can see that scipy needs numpy and is needed for scikit-learn.

pip install always installs the latest publicly available version, so if you want to install a fixed version of a package, you need to specify the version. For example, the latest version of scikit-learn is 0.22.1, but if you want to install version 0.21.3:

$ pip install scikit-learn==0.21.3
Collecting scikit-learn==0.21.3
  Using cached https://files.pythonhosted.org/packages/e9/57/8a9889d49d0d77905af5a7524fb2b468d2ef5fc723684f51f5ca63efed0d/scikit_learn-0.21.3-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Requirement already satisfied: scipy>=0.17.0 in ./venv/lib/python3.7/site-packages (from scikit-learn==0.21.3) (1.4.1)
Requirement already satisfied: joblib>=0.11 in ./venv/lib/python3.7/site-packages (from scikit-learn==0.21.3) (0.14.1)
Requirement already satisfied: numpy>=1.11.0 in ./venv/lib/python3.7/site-packages (from scikit-learn==0.21.3) (1.18.1)
Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.22.1
    Uninstalling scikit-learn-0.22.1:
      Successfully uninstalled scikit-learn-0.22.1
Successfully installed scikit-learn-0.21.3

Even if you have already installed scikit-learn-0.22.1, it will uninstall it first and then install the specified version. As for joblib, numpy, and scipy, they are already installed and meet the requirements of scikit-learn-0.21.3, so they will be left as they are.

When you want to install by specifying the versions of multiple packages, you can write them together in requirements.txt (any name) and pip install.

$ cat requirements.txt
joblib==0.14.1
numpy==1.18.1
scikit-learn==0.21.3
scipy==1.4.1
$ pip install -r requirements.txt
Collecting joblib==0.14.1
  Using cached https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl
Collecting numpy==1.18.1
  Using cached https://files.pythonhosted.org/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl
Collecting scikit-learn==0.21.3
  Using cached https://files.pythonhosted.org/packages/e9/57/8a9889d49d0d77905af5a7524fb2b468d2ef5fc723684f51f5ca63efed0d/scikit_learn-0.21.3-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting scipy==1.4.1
  Using cached https://files.pythonhosted.org/packages/85/7a/ae480be23b768910a9327c33517ced4623ba88dc035f9ce0206657c353a9/scipy-1.4.1-cp37-cp37m-macosx_10_6_intel.whl
Installing collected packages: joblib, numpy, scipy, scikit-learn
Successfully installed joblib-0.14.1 numpy-1.18.1 scikit-learn-0.21.3 scipy-1.4.1

If you want to duplicate the current environment to another project etc., you can write the entire current environment to a file with the pip freeze command:

$ pip freeze > requirements.txt
$ cat requirements.txt
joblib==0.14.1
numpy==1.18.1
scikit-learn==0.21.3
scipy==1.4.1

In addition to'==', you can add conditions such as'<=' and'> ='. requirement-specifiers For example, if scikit-learn> = 0.21.3 is set:

$ cat requirements.txt
joblib==0.14.1
numpy==1.18.1
scikit-learn>=0.21.3
scipy==1.4.1

Current environment check:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.21.3
scipy        1.4.1
setuptools   39.0.1

Try pip install -r requirements.txt:

$ pip install -r requirements.txt
Requirement already satisfied: joblib==0.14.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (0.14.1)
Requirement already satisfied: numpy==1.18.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.18.1)
Requirement already satisfied: scikit-learn>=0.21.3 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 3)) (0.21.3)
Requirement already satisfied: scipy==1.4.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 4)) (1.4.1)

It is said that the conditions described in requirements.txt are met. Well, scikit-learn 0.21.3 certainly satisfies scikit-learn> = 0.21.3. When you want to update to the latest version that meets the conditions --upgrade:

$ pip install --upgrade -r requirements.txt
Requirement already up-to-date: joblib==0.14.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (0.14.1)
Requirement already up-to-date: numpy==1.18.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.18.1)
Collecting scikit-learn>=0.21.3
  Using cached https://files.pythonhosted.org/packages/82/d9/69769d4f79f3b719cc1255f9bd2b6928c72f43e6f74084e3c67db86c4d2b/scikit_learn-0.22.1-cp37-cp37m-macosx_10_6_intel.whl
Requirement already up-to-date: scipy==1.4.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 4)) (1.4.1)
Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.21.3
    Uninstalling scikit-learn-0.21.3:
      Successfully uninstalled scikit-learn-0.21.3
Successfully installed scikit-learn-0.22.1

You can also write the requirements file inside the requirements file:

$ cat requirements_old.txt
numpy==1.18.1
scipy==1.4.1
$ cat requirements_new.txt
-r requirements_old.txt
scikit-learn>=0.21.3
$ pip install -r requirements_new.txt
Collecting numpy==1.18.1
  Using cached https://files.pythonhosted.org/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl
Collecting scipy==1.4.1
  Using cached https://files.pythonhosted.org/packages/85/7a/ae480be23b768910a9327c33517ced4623ba88dc035f9ce0206657c353a9/scipy-1.4.1-cp37-cp37m-macosx_10_6_intel.whl
Collecting scikit-learn>=0.21.3
  Using cached https://files.pythonhosted.org/packages/82/d9/69769d4f79f3b719cc1255f9bd2b6928c72f43e6f74084e3c67db86c4d2b/scikit_learn-0.22.1-cp37-cp37m-macosx_10_6_intel.whl
Collecting joblib>=0.11
  Using cached https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl
Installing collected packages: numpy, scipy, joblib, scikit-learn
Successfully installed joblib-0.14.1 numpy-1.18.1 scikit-learn-0.22.1 scipy-1.4.1
$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1

How to find a package

You can use pip search to search for packages published on PyPI. For example, search for Qiita:

$ pip search qiita
qiita (0.1.1)              - Qiita api wrapper for Python
qiita-spots (0.2.0)        - Qiita: Spot Patterns
qiita_v2 (0.2.1)           - Python Wrapper for Qiita API v2
qiitacli (1.1.0)           - CLI Application for Qiita API v2
qiitap (1.3.1)             - Add include function to Qiita
                             Markdown
qiita_api_wrapper (0.1.0)  - Qiita API V2 wrapper for Python
qiidly (1.0.0)             - Sync Qiita feeds for followees and
                             following tags to Feedly. -> Qiita&#
                             12391;&#12501;&#12457;&#12525;&#1254
                             0;&#20013;&#12398;&#12479;&#12464;&#
                             12392;&#12518;&#12540;&#12470;&#1254
                             0;&#12434;Feedly&#12395;&#21516;&#26
                             399;&#12290;

However, this method doesn't give you the details of the package, so most of the time you'll search on the PyPI website.

Uninstall package with pip

Current environment check:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1

Before uninstalling the package, use pip show to make sure there are no other packages that require this package before pip uninstall. If you don't need scikit-learn anymore:

$ pip show scikit-learn
Name: scikit-learn
Version: 0.22.1
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: /Users/place/venv/lib/python3.7/site-packages
Requires: joblib, scipy, numpy
Required-by:
$ pip uninstall scikit-learn
Uninstalling scikit-learn-0.22.1:
  Would remove:
    /Users/place/venv/lib/python3.7/site-packages/scikit_learn-0.22.1.dist-info/*
    /Users/place/venv/lib/python3.7/site-packages/sklearn/*
Proceed (y/n)? y
  Successfully uninstalled scikit-learn-0.22.1
$ pip list
Package    Version
---------- -------
joblib     0.14.1
numpy      1.18.1
pip        19.3.1
scipy      1.4.1
setuptools 39.0.1

When I installed scikit-learn, I got joblib, numpy, and scipy, but when I uninstall it, I can keep them! b_1.jpg Picture1.jpg Typical

You can skip the confirmation with -y:

$ pip uninstall joblib -y
$ pip uninstall scipy -y
$ pip uninstall numpy -y

Multiple uninstalls:

$ pip uninstall -y joblib scipy numpy

Uninstall by specifying in requirements file:

pip uninstall -r requirements.txt -y

Check if the dependencies are met

The pip check command will check for dependencies between installed packages. scipy and scikit-learn depend on numpy, but without numpy:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1
$ pip check
scipy 1.4.1 requires numpy, which is not installed.
scikit-learn 0.22.1 requires numpy, which is not installed.

It tells me that there is no numpy.

reference

What Is Pip? A Guide for New Pythonistas

Recommended Posts

What is pip and how do you use it?
[Python] What is pip? Explain the command list and how to use it with actual examples
Is Parallel Programming Hard, And, If So, What Can You Do About It?
How to install and use pyenv, what to do if you can't switch python versions
[Pandas] What is set_option [How to use]
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
How to use is and == in Python
What is the difference between `pip` and` conda`?
What are you comparing with Python is and ==?
How to install Cascade detector and how to use it
If you try to install Python2 pip after installing Python3 pip and it is rejected
What to do if you can't pip install mysqlclient
% And str.format () in Python. Which one do you use?
[Python] How do you use lambda expressions? ?? [Scribbles] [Continued-1]
What you can and cannot do with Tensorflow 2.x
Install tweepy with pip and use it for API 1.1
[Python] Python and security-① What is Python?
How do you collect information?
[AWS] What to do when you want to pip with Lambda
What to do if you can't use WiFi on Linux
Why django-import-export import is so slow and what to do
What to do if you get an Undefined error when trying to use pip with pyenv
What is Python? What is it used for?
What to do if you cat or tail a binary file and the terminal is garbled
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
What to do if you can't install pyaudio with pip #Python
How to give and what the constraints option in scipy.optimize.minimize is
When it is troublesome to copy what you built with vue
What to do if you grep a text file and it becomes Binary file (standard input) matches
What to do if you get a UnicodeDecodeError with pip install
How to use .bash_profile and .bashrc
How to install and use Graphviz
Don't you know it? pip command
Introduction to how to use Pytorch Lightning ~ Until you format your own model and output it to tensorboard ~
What to do if you can't use the trash in Lubuntu 18.04.
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
Python | What you can do with Python
What to do if you can't use scikit grid search in Python
When you want to use it as it is when using it with lambda memo
What to do if you can't install with pip in babun environment
What to do if you get Could not fetch URL 443 with pip
What to do when a warning message is displayed in pip list
[Python] What is pandas Series and DataFrame?
What do you roughly say "sudo su"?
How to install and use pandas_datareader [Python]
What you can do with API vol.1
Data analysis, what do you do after all?
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
python: How to use locals () and globals ()
Day 64 pip install tensorflow and 2.0 is here.
How to use Python zip and enumerate
What you can do with programming skills
Let's summarize what you want to do.
How to use pandas Timestamp and date_range
It is convenient to use stac_info and exc_info when you want to display traceback in log output by logging.
Tips for those who are wondering how to use is and == in Python
Do you understand the confidence intervals correctly? What is the difference from the conviction section?
Why Docker is so popular. What is Docker in the first place? How to use
[EC2] What to do when selenium is stuck and processing does not proceed
What do you like about how to convert an array (list) to a string?
[Introduction to statistics] What kind of distribution is the t distribution, chi-square distribution, and F distribution? A little summary of how to use [python]