3

When I run my python script using python3, I get the following error:

Traceback (most recent call last): File "rea_scrape.py", line 2, in import requests ModuleNotFoundError: No module named 'requests'

When I run pip3 install requests it's apparent that requests has been installed.

Requirement already satisfied: requests in /Library/Python/2.7/site-packages (2.18.4) Requirement already satisfied: certifi>=2017.4.17 in /Library/Python/2.7/site-packages (from requests) (2018.4.16) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Python/2.7/site-packages (from requests) (3.0.4) Requirement already satisfied: idna<2.7,>=2.5 in /Library/Python/2.7/site-packages (from requests) (2.6) Requirement already satisfied: urllib3<1.23,>=1.21.1 in /Library/Python/2.7/site-packages (from requests) (1.22)

However, closer inspection of the messages seem to indicate that pip3 is installing packages for Python2.7, which is not my intention.

The result is that nothing I install using the pip3 command can be imported into a script run by python3.

How do I fix this?

FYI I'm using MacOS High Sierra.

p4t
  • 4,317
  • 6
  • 36
  • 58

1 Answers1

8

it looks like your pip3 installation is pointing to the wrong version of python. If you look at the file inside your /usr/local/bin there you can see a file called pip3. Where you can see which version of python it's been pointing. In my case, the file has the content something like this:

#!/usr/bin/python3

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

In you case the first line must be #!usr/bin/python2 . You can varify that by using pip3 -V on your terminal.

The other way to fix this would be to completely uninstall your pip3 installation and again re-installing python3-pip

  1. curl https://bootstrap.pypa.io/get-pip.py | python3
  2. python3 From this answer: How to install pip for Python 3 on Mac OS X?

Also please look at this question: pip3.4 -V refers to python2.7 installation This question should answer you fully.

user2906838
  • 1,060
  • 8
  • 18