Mac OS : ImportError: No module named requests

J’ai voulu suivre la procédure avec brew, pip, …. mais sans succès avec la version 2.7.2

$ python --version
Python 2.7.2

J’avais des erreurs du type :

$ brew reinstall python
....
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: An exception occurred within a child process:
  CompilerSelectionError: python cannot be built with any available compilers.
Install GNU's GCC
  brew install gcc

$ python -m pip install --user requests
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip

$ sudo easy_install pip
Searching for pip
Reading http://pypi.python.org/simple/pip/
Couldn't find index page for 'pip' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading http://pypi.python.org/simple/
No local packages or download links found for pip
Best match: None
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/bin/easy_install", line 8, in <module>
    load_entry_point('setuptools==0.6c11', 'console_scripts', 'easy_install')()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1712, in main
    
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1700, in with_ei_usage
    
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1716, in <lambda>
    
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 211, in run
    
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 434, in easy_install
    
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/package_index.py", line 475, in fetch_distribution
AttributeError: 'NoneType' object has no attribute 'clone'



J’ai donc changé de fusil d’épaule :

$ brew install python3
Warning: Building python from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select --install

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: An exception occurred within a child process:
  CompilerSelectionError: python cannot be built with any available compilers.
Install GNU's GCC
  brew install gcc

$ xcode-select --install
...
$ brew install python3
...
$ brew link --overwrite python
Linking /usr/local/Cellar/python/3.7.2_1... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

$ sudo mkdir /usr/local/Frameworks
...
$ sudo chown $(whoami):admin /usr/local/Frameworks
...
$ brew link --overwrite python
Linking /usr/local/Cellar/python/3.7.2_1... 1 symlinks created
$ python --version
Python 2.7.2
$ ls -l /usr/bin/py*
-rwxr-xr-x  4 root  wheel    925 18 aoû 02:45 /usr/bin/pydoc
lrwxr-xr-x  1 root  wheel     74 24 jan 08:32 /usr/bin/pydoc2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7
-rwxr-xr-x  1 root  wheel  66880 16 jan 02:21 /usr/bin/python
-rwxr-xr-x  4 root  wheel    925 18 aoû 02:45 /usr/bin/python-config
lrwxr-xr-x  1 root  wheel     75 24 jan 08:32 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel     82 24 jan 08:32 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x  1 root  wheel  66880 16 jan 02:21 /usr/bin/pythonw
lrwxr-xr-x  1 root  wheel     76 24 jan 08:32 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
....
$ brew reinstall python
...
$ pip3 install requests
...

Et cela fonctionne … en résumé, le Python c’est super simple a installer. Enfin au bout d’un moment cela tombe en marche et tu es bien content …. Misère.

Tout cela pour écrire quelques lignes de Python ( Pour utiliser l’API REST JOLIN en Python : https://joplin.cozic.net/api/ ) :

import json
import requests

ip = "127.0.0.1"
port = "41184"
Diaro_UID = "12345678901234567801234567890123"
token = "bcf7475af70806df3e0b88dd10fcf88971e7088e31"
url = (
    "http://"+ip+":"+port+"/notes?"
    "token="+token
)
url2 = (
    "http://"+ip+":"+port+"/folders?"
    "token="+token
)
payload = {
    "id": Diaro_UID,
    "title": "Diaro Import"
}

try:
    resp = requests.post(url2, json=payload)
    resp.raise_for_status()
    resp_dict = resp.json()
    print(resp_dict)
    print("My ID")
    print(resp_dict['id'])
except requests.exceptions.HTTPError as e:
    print("Bad HTTP status code:", e)
except requests.exceptions.RequestException as e:
    print("Network error:", e)

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.