Resolved – ImportError: No module named requests | /usr/bin/python: No module named pip on MacOS

Topic General

You may face an error while running the script which calls any python script which internally calls out requests module.

ImportError: No module named requests”

Traceback (most recent call last):
  File "dev-scripts/find-latest-version.py", line 7, in <module>
    import requests
ImportError: No module named requests

When you verify you can see – you already have python installed

Ravi-MacBook-Pro:~ code$ which python
/usr/bin/python
Ravi-MacBook-Pro:~ code$ python --version
Python 2.7.10

Try to search for the module, if it is installed or not

Login to Python console and List out the modules (Once done press Cntrl + D )

Ravi-MacBook-Pro:~ code$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help("modules")

Please wait a moment while I gather a list of all available modules...

Accounts            __future__          distutils           pwd
AddressBook         _abcoll             dl                  py2app
AppKit              _ast                doctest             py_compile

Once we go through the list, we found that the module is missing.

Now lets install it through Python

Ravi-MacBook-Pro:~ code$ python -m pip install --user requests
/usr/bin/python: No module named pip

Ohhh ….. pip itself seems to be missing  “/usr/bin/python: No module named pip”

Lets install pip through easy_installEasy Install is a python module ‘easy_install’ bundled with ‘setuptools that lets you automatically download, build, install, and manage Python packages )

You need to use sudo since it requires admin permission to make the system changes

Ravi-MacBook-Pro:~ code$ sudo easy_install pip
Password:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 9.0.1
Downloading https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
Processing pip-9.0.1.tar.gz
Writing /tmp/easy_install-qVQWVa/pip-9.0.1/setup.cfg
Running pip-9.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-qVQWVa/pip-9.0.1/egg-dist-tmp-nIpTfa
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
  warnings.warn(msg)
warning: no previously-included files found matching '.coveragerc'
warning: no previously-included files found matching '.mailmap'
warning: no previously-included files found matching '.travis.yml'
warning: no previously-included files found matching '.landscape.yml'
warning: no previously-included files found matching 'pip/_vendor/Makefile'
warning: no previously-included files found matching 'tox.ini'
warning: no previously-included files found matching 'dev-requirements.txt'
warning: no previously-included files found matching 'appveyor.yml'
no previously-included directories found matching '.github'
no previously-included directories found matching '.travis'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
creating /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
Extracting pip-9.0.1-py2.7.egg to /Library/Python/2.7/site-packages
Adding pip 9.0.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin

Installed /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip

Now lets try again to install requests module

Ravi-MacBook-Pro:~ code$ python -m pip install --user requests
Collecting requests
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 1.1MB/s 
Collecting idna<2.7,>=2.5 (from requests)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 1.5MB/s 
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 1.3MB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
    100% |████████████████████████████████| 153kB 1.5MB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 1.1MB/s 
Installing collected packages: idna, urllib3, certifi, chardet, requests
Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
Ravi-MacBook-Pro:~ code$

Fantastic – Now both pip and requests are installed – You can verify the module list through pip

Ravi-MacBook-Pro:~ code$  pip freeze | grep requests
requests==2.18.4

Now try to run the same script where you were getting requests module error – This time it should run without any issue.

You have now successfully resolved the python module issue.

Happy learning ……

12 comments… add one

Leave a Comment