Aller au contenu

Python 3

Python 3 peut s'installer avec Homebrew:

$ brew install python3

Les extensions Python 3 s'installent dans /usr/local/lib/python3.7/site-packages/ (Intel) ou /opt/homebrew/lib/python3.9/site-packages/ (Arm)

Pour lancer un script Python 3:

$ python3 script.py

Pour que python pointe sur Python 3 (homebrew), rajouter ceci à .bash_profile:

export PATH=/usr/local/opt/python/libexec/bin:$PATH

Pip est installé d'origine avec Python 3. On le lance avec la commande pip3:

$ pip list --outdated --format=columns
$ pip install mkdocs

Installation utilisateur:

$ python3 -m site --user-base
/Users/bruno/Library/Python/3.7

Pipenv

Outil qui remplace pip et virtualenv

https://github.com/pypa/pipenv

Docs

Pyenv

Python Version Management

https://github.com/pyenv/pyenv

Venv (Pyvenv)

Outil pour créer un environnement virtuel

Fournit avec Python (>= 3.4). Similaire à virtualenv.

Passage de Python 3.7 à 3.8:

Erreur avec pip3:

$ pip3 list --outdated --format columns
zsh: /usr/local/bin/pip3: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory
$ pip3 -V
zsh: /usr/local/bin/pip3: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory
pip 19.0.3 from /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)

Il faut donc mettre à jour pip3:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1825k  100 1825k    0     0  2692k      0 --:--:-- --:--:-- --:--:-- 2688k

$ python3 get-pip.py
Collecting pip
  Using cached pip-20.1.1-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.0.2
    Uninstalling pip-20.0.2:
      Successfully uninstalled pip-20.0.2
Successfully installed pip-20.1.1
$ pip3 -V

pip 20.1.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

idle:

idle est l'éditeur par défaut de Python

Pour le lancer depuis le shell:

$ idle

Python et tcl/tk versions

Python 3 utilise la bibliothèque Tcl/Tk fournie par macOS (8.5.9).

Pour utiliser une version plus récente (8.6.8) de la bibliothèque :

$ brew install tcl-tk
$ brew reinstall python3 --with-tcl-tk

jupyter:

jupyter est une web application qui permet de créer et partager des documents contenant du live-code...

Installer:

$ pip install jupyter

Démarrer:

$ jupyter notebook

Modules:

Global

$ pip install <module>

Les modules s'installent dans /usr/local/lib/python3.7/site-packages :

Liste des modules:

$ pip freeze
appnope==0.1.0
asgiref==3.2.3
backcall==0.1.0

Local

$ pip install --user <module>

Les modules s'installent dans /Users/bruno/Library/Python/3.7/lib/python/site-packages/ :

Liste des modules:

$ pip freeze --user
Click==7.0
tornado==6.0.3

Rechercher un module

PyPI - the Python Package Index

$ 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

Il faut installer Mkdocs avec pip dans --user

$ pip install --user mkdocs

Mkdocs s'installe dans /Users/bruno/Library/Python/3.7/bin :

~/Library/Python/3.7/bin$ ./mkdocs --version
mkdocs, version 1.0.4 from /Users/bruno/Library/Python/3.7/lib/python/site-packages/mkdocs (Python 3.7)

On peut ajouter le chemin au $PATH, en ajoutant la ligne suivante au .zshrc ou .bash_profile:

export PATH=/Users/bruno/Library/Python/3.7/bin:$PATH

On installe de la même manière le thème Material:

$ pip install --user mkdocs-material

Django

Installer Django

Problème à l'installation

Erreur lors de l’install de python3 avec Homebrew:

bruno@HackiMac:/usr/local/bin$ brew install python3

==> Downloading https://homebrew.bintray.com/bottles/python-3.7.0.high_sierra.bottle.3.tar.gz

Already downloaded: /Users/bruno/Library/Caches/Homebrew/downloads/d76c2354ae237f190a868bb74f28d606b88cce724222bafa114e91cd8a1462d5--python-3.7.0.high_sierra.bottle.3.tar.gz

==> Pouring python-3.7.0.high_sierra.bottle.3.tar.gz

Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

Solution:

sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks
==> Pouring python@3.9-3.9.1_4.big_sur.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/easy_install-3.9
Target /usr/local/bin/easy_install-3.9
already exists. You may want to remove it:
  rm '/usr/local/bin/easy_install-3.9'

To force the link and overwrite all conflicting files:
  brew link --overwrite python@3.9

To list all files that would be deleted:
  brew link --overwrite --dry-run python@3.9

Possible conflicting files are:
/usr/local/bin/easy_install-3.9
/usr/local/bin/pip3
/usr/local/bin/pip3.9
$ brew link --overwrite --dry-run python@3.9
# puis
$ rm '/usr/local/bin/easy_install-3.9'
$ brew link --overwrite python@3.9

Dernière mise à jour: November 9, 2023