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.
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
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
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;フォロӦ
0;中のタグ&#
12392;ユーザӦ
0;をFeedlyに同
399;。
However, this method doesn't give you the details of the package, so most of the time you'll search on the PyPI website.
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! 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
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.
What Is Pip? A Guide for New Pythonistas
Recommended Posts