Don't you know it? pip command

We will introduce frequently used commands to complicated commands for each usage case.

Case 1. Dependency care

The script doesn't work because of a problem with the package dependencies ...

(1) Check for dependency issues.

$ pip check
#If there are no problems with the package dependencies,
# No broken requirements found.
#Is output.

(2) After confirming the requested version of the package, check the available version.

$ pip install <package>==
# ERROR: Could not find a version that satisfies the requirement <package>== (...
#Error is output, and you can check the available package version.

(3) Install by specifying the version

$ pip install <package>==<version>

#When you want to be up to date
$ pip install -U <package>
#When you want to update to the latest version below a certain version
$ pip install -U "<package> < <version>" #Inequality sign<I'm using.

Check installed package dependencies

Use show to see the dependencies along with the package details.

$ pip show <package>

You can use the pipdeptree package to display dependencies that include version information.

$ pip install pipdeptree

# <package>Shows the package requested by and its version.
$ pipdeptree -p <packgage>
# <package>View the package that requests and the version that is requested.
$ pipdeptree -r -p <packgage>

Check details & dependencies of packages not installed

You can also find out more about uninstalled packages by asking PyPi as follows:

import request
from pprint import pprint #Import for output formatting

package = '<package>'
version = '<version>'
url = f'https://pypi.org/pypi/{package}/{version}/json'
#If you do not specify the version
# url = f'https://pypi.org/pypi/{package}/json'

json = requests.get(url).json()
pprint(json['info'])

You can check the packages required by the specified package as follows.

pprint(json['info']['requires_dist'])

Case 2. Install the package in an offline environment

I want to pip install <package> at the destination connected with ssh, but the connection destination is offline!

(1) First, download the required package online directly under ./src (if you want to specify the version, use<package> == <version>).

$ pip download -d ./src <package>
# or
$ pip download -d ./src -r requirements.txt

(2) Transfer ./src with scp.

$ scp -r -C ./src <user>@<host>:<path>

#If the capacity is large, compress it and send it.
$ tar czf src.tar.gz src
$ scp -C ./src.tar.gz <user>@<host>:<path> 

(3) After connecting with ssh, enter the src directory. Installation is complete with the following command!

$ pip install <package (file name)> #Install one by one from the current directory.
# or 
$ pip install ./* #Install all at once.

#If you compressed src, decompress it in advance.
$ tar xzf src.tar.gz

This and that of pip download

Download the source package.

$ pip download -d ./src --no-binary :all: <package>

Download the binary package by specifying the version and platform.

$ pip download -d ./src --only-binary :all: --platform <platform> --python-version <python_version> --implementation <implementation> --abi <abi> <package>

Below is a reference page.

Case 3. Change the save destination of pip install

I want to try a certain package, but I don't want to pollute the global and local pips. Not enough to use a virtual environment ...

(1) Specify the directory and install the package.

$ pip install -t <dir> <package>

(2) By adding the following line to the script, the package directly under <dir> can be used.

import sys
sys.path.append(dirpath)

#How to specify the path Part 1
import os
dirpath = sys.path.append(os.path.join(os.path.dirname(__file__), '<dir>Relative path'))
#How to specify the path Part 2
from pathlib import Path
dirpath = sys.path.append(str((Path(__file__).parent/'<dir>Relative path').resolve()))
#How to specify the path Part 3
dirpath = '<dir>Absolute path'

You can also execute it by writing a relative path in dirpath, but if you want to call the written script from another script, you need to use __file__ or specify dirpath as an absolute path.

(3) Delete the directory when you no longer need the package.

$ rm -r <dir>

I want to try a different version of an installed package

If you run the script in the directory <dir> where you installed the packages, the packages in <dir> will be preferentially imported.

If you run the script outside of <dir> and want to use another version of the package that is already installed globally or locally, add the following line.

sys.path = sys.path[::-1]

The package is read preferentially from the first path of sys.path. By reversing the list after adding a new path, the added path can be read with the highest priority.

Recommended Posts

Don't you know it? pip command
If you don't know it, it's dangerous. Carefully explain how to use the xargs command
pip command collection
Broken pip command
Linux command that can be used from today if you know it (Basic)
Install the pip command
What is pip and how do you use it?
Don't write Python if you want to speed it up with Python
Tensorflow with Raspberry Pi. If you stumble with pip, solve it with virtualenv!
If you don't know how many arguments you will have 2 ** kwargs Variadic arguments
[TensorFlow] If you want to run TensorBoard, install it with pip
How to find if you don't know the Java installation directory