# pip



## Installation:

### Installer pip (python 2):

[:fa-link: https://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade](https://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade)

##### Documentation pip:

[:fa-link: https://pip.pypa.io/en/stable/](https://pip.pypa.io/en/stable/)



### Installer pip3 (python 3):

#### Installer pip:

```bash
$ sudo easy_install pip
```

 Pour [Python 3](python3.md), **pip** est installé d'origine.

#### Version courrante de pip:

```bash
$ pip3 --version
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
```

#### Mettre à jour pip:

```bash
$ sudo pip install --upgrade pip
```

```bash
$ pip3 install --user --upgrade pip
```

!!! attention
    Sur macOS, à cause de SIP (System Integrity Protection), l'installation se fait avec le mot-clé —user



#### Cache:

Linux and Unix

```bash
~/.cache/pip  # and it respects the XDG_CACHE_HOME directory.
```

OS X

```bash
~/Library/Caches/pip
```

Windows

```bash
%LocalAppData%\pip\Cache
```

pip peut installer en ignorant le cache:

```bash
$ pip3 --no-cache-dir install mkdocs
```



## Environnement virtuel:



```bash
# Création de l'environnement virtuel
$ python3 -m venv mkdocs_env

# Mise-à-jour
$ mkdocs_env/bin/pip3 install -U pip setuptools

# Installation de mkdocs
$ mkdocs_env/bin/pip3 install mkdocs
# Installation du thème
$ mkdocs_env/bin/pip3 install mkdocs-material pymdown-extensions pygments
# Installation des plugins
$ mkdocs_env/bin/pip3 install mkdocs-pdf-export-plugin mkdocs-minify-plugin
# Installation du plugin depuis les sources
$ mkdocs_env/bin/pip3 install mkdocs-pdf-export-plugin-0.5.5.tar.gz

# Mise-à-jour
$ mkdocs_env/bin/pip3 install -U mkdocs

# Mise-à-jour de l'environnement virtuel
$ python3 -m venv --upgrade mkdocs_env
```



```bash
~/Documents/mydocs master*

# Création d'un nouveau projet
$ ~/Documents/mkdocs_env/bin/mkdocs new

# Démarrage de mkdocs
$ ~/Documents/mkdocs_env/bin/mkdocs serve
INFO    -  Building documentation...
INFO    -  Cleaning site directory
```

L'activation n'est pas obligatoire, elle simplifie juste les chemins.

```bash
~/Documents/mydocs master*
$ source ~/Documents/mkdocs_env/bin/activate

~/Documents/mydocs master*
mkdocs_env ❯ mkdocs serve

~/Documents/mydocs master*
mkdocs_env ❯ deactivate
```



## Modules:

#### Installer des modules (depuis [PyPi](https://pypi.org)):

##### Installer un module:

```bash
$ pip3 install django
```

Les modules sont ici:

`/usr/local/lib/python3.7/site-packages`

`/usr/local/lib/python3.8/site-packages`

##### Installer un module pour l'utilisateur courant (mkdocs):

```bash
$ pip3 install --user mkdocs
```

Les modules sont ici:

`/Users/bruno/Library/Python/3.7/lib/python/site-packages`

#### Installer des modules (depuis une archive):

```bash
$ pip3 install mkdocs-pdf-export-plugin-0.5.5.tar.gz
```

#### Installer des modules (depuis les sources):

```bash
~/Downloads/
$ cd mkdocs-pdf-export-plugin-0.5.5

~/Downloads/mkdocs-pdf-export-plugin-0.5.5
$ pip3 install .
```

#### Installer des modules (depuis un [VCS](https://pip.pypa.io/en/latest/reference/pip_install/#vcs-support)):

```bash
$ pip3 install -e git+https://github.com/zhaoterryy/mkdocs-pdf-export-plugin.git#egg=mkdocs-pdf-export-plugin
# avec un commit
$ pip3 install -e git+https://github.com/zhaoterryy/mkdocs-pdf-export-plugin.git@7c6c82c96490a84b4bd617e21977259f60dd5007#egg=mkdocs-pdf-export-plugin
# avec un tag
$ pip3 install -e git+https://github.com/zhaoterryy/mkdocs-pdf-export-plugin.git@v0.5.3#egg=mkdocs-pdf-export-plugin


$ pip3 install -e "git+ssh://git.example.com/MyProject#egg=MyProject"
```

#### Requirements files:

[*requirements.txt*](https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format) est une liste de modules à installer.

```bash
$ pip3 install -r requirements.txt

$ pip3 freeze > requirements.txt
$ pip3 install -r requirements.txt
```

#### Installer une version précise d'un module:

```bash
$ pip3 show tornado
Name: tornado
Version: 5.0
.../...

$ pip3 install tornado==4.5.3
Collecting tornado==4.5.3
Installing collected packages: tornado
  Found existing installation: tornado 5.0
    Uninstalling tornado-5.0:
      Successfully uninstalled tornado-5.0
Successfully installed tornado-4.5.3

$ pip3 install 'tornado>=4.1.0,<4.5.3'
# Installe une version comprise entre 4.1.0 et 4.5.3

$ pip3 install 'tornado~=4.5.2'
#  Installe une version “==4.5.*” qui est aussi “>=4.5.2”.
```



#### Désinstaller un module:

```bash
$ pip3 uninstall <module>
```



#### Informations sur un module:

```bash
$ pip3 show <module>

$ pip3 show mkdocs
Name: mkdocs
Version: 0.17.2
Summary: Project documentation with Markdown.
Home-page: http://www.mkdocs.org
Author: Tom Christie
Author-email: tom@tomchristie.com
License: BSD
Location: /Users/bruno/Library/Python/2.7/lib/python/site-packages
Requires: tornado, PyYAML, click, Markdown, Jinja2, livereload
```



#### Installer une liste de modules requis:

```bash
$ pip3 install -r requirements.txt
```



#### Liste des modules installés:

```bash
# --format=columns (par defaut)

$ pip3 list

Package                                Version

-------------------------------------- ---------

altgraph                               0.10.2

backports-abc                          0.5

bdist-mpkg                             0.5.0
```

```bash
$ pip3 freeze
appnope==0.1.0
attrs==18.2.0
autopep8==1.4.3
backcall==0.1.0
bleach==3.1.0
Click==7.0
decorator==4.3.2
defusedxml==0.5.0
Django==2.1.7

# Exporter la liste
$ pip3 freeze > export_liste_pip.txt

# puis l'importer sur une autre machine
$ pip3 install -r export_liste_pip.txt

# Ou créer un bundle
$ pip3 bundle <nom_du_bundle>.pybundle -r export_liste_pip.txt

# et importer les lib
$ pip3 install <nom_du_bundle>.pybundle
```



#### Liste des modules mis-à-jour:

```bash
$ pip3 list --outdated

Package                                Version  Latest Type

-------------------------------------- -------- ------ -----

altgraph                               0.10.2   0.15   wheel

macholib                               1.5.1    1.9    wheel

matplotlib                             1.3.1    2.1.1  wheel
```



```bash
$ pip3 list --outdated --format=freeze
packaging==16.8
Sphinx==1.7.0
```

```bash
$ pip3 list --outdated --format=columns
Package   Version Latest Type

------

packaging 16.8    17.1   wheel
Sphinx    1.7.0   1.7.1  wheel
```

```bash
$ pip3 list --outdated --format=legacy
packaging (16.8) - Latest: 17.1 [wheel]
Sphinx (1.7.0) - Latest: 1.7.1 [wheel]
```

```bash
$ pip3 list --outdated --format=json
[{"name": "packaging", "version": "16.8", "latest_version": "17.1", "latest_filetype": "wheel"}, {"name": "Sphinx", "version": "1.7.0", "latest_version": "1.7.1", "latest_filetype": "wheel"}]
```



#### Mettre à jour un module:

```bash
$ pip3 install --upgrade <module>

$ pip3 install -U django
```

```bash
$ pip3 install --user --upgrade <module>

$ pip3 install --user -U mkdocs-material
```



#### Chercher un module:

```bash
$ pip3 search <module>

$ pip3 search mkdocs-pdf-export-plugin
mkdocs-pdf-export-plugin (0.5.5)                   - An MkDocs plugin to export content pages as PDF files
  INSTALLED: 0.5.5 (latest)
mkdocs-mk2pdf-plugin (0.1.5)                       - An MkDocs plugin to export content pages as PDF files
  INSTALLED: 0.1.5 (latest)
mkdocs-autolinks-plugin (0.2.0)                    - An MkDocs plugin
mkdocs-with-pdf (0.1.0)                            - Generate a single PDF file from MkDocs repository
mkdocs-toc-sidebar-plugin (0.1.0)                  - An MkDocs plugin
mkdocs-tooltipster-links-plugin (0.1.0)            - An MkDocs plugin
```





## Dépendances:

#### Montrer les dépendances d'un module:

```bash
$ pip3 show mkdocs | grep Requires
Requires: click, Markdown, PyYAML, Jinja2, tornado, lunr, livereload
```



#### pipdeptree:

Savoir quel module requiert tel module (dépendances inverses):

```bash
$ pipdeptree -r -p mkdocs
mkdocs==1.1
  - mkdocs-material==4.6.3 [requires: mkdocs>=1.0]
  - mkdocs-minify-plugin==0.2.3 [requires: mkdocs>=1.0.4]
  - mkdocs-mk2pdf-plugin==0.1.5 [requires: mkdocs>=0.17]
  - mkdocs-pdf-export-plugin==0.5.5 [requires: mkdocs>=0.17]
  - mkdocs-windmill==1.0.4 [requires: mkdocs]
  - mkpdfs-mkdocs==1.0.1 [requires: mkdocs>=0.17]
```

Montrer les dépendances:

```bash
$ pipdeptree -p mkdocs
mkdocs==1.1
  - click [required: >=3.3, installed: 7.1]
  - Jinja2 [required: >=2.10.1, installed: 2.11.1]
    - MarkupSafe [required: >=0.23, installed: 1.1.1]
  - livereload [required: >=2.5.1, installed: 2.6.1]
    - six [required: Any, installed: 1.14.0]
    - tornado [required: Any, installed: 6.0.4]
  - lunr [required: ==0.5.6, installed: 0.5.6]
    - future [required: >=0.16.0, installed: 0.18.2]
    - six [required: >=1.11.0, installed: 1.14.0]
  - Markdown [required: >=3.2.1, installed: 3.2.1]
    - setuptools [required: >=36, installed: 46.0.0]
  - PyYAML [required: >=3.10, installed: 5.3]
  - tornado [required: >=5.0, installed: 6.0.4]
```

