user3738838
user3738838

Reputation: 281

Import Request not found

I just started a new project and the client gave me a new Mac Book Pro along with some Python scripts that they developed. Unfortunately, I cant' seem to get the Python script to run correctly.

I am new to using a Mac.

When I try to run the Python program, I get an error at "import requests" line despite that I have installed the requests module via pip. It may be an issue with I am new to Python and Mac and downloaded the latest Python before I realized that 2.7 is pre-installed or it could be something completely different.

The first 2 lines in the file xxx.py seem to work

import csv
from bs4 import BeautifulSoup
import requests

Error:

File /Users/ad/Downloads/xxx.py, Line 5, in <module>
     from import requests
ImportError: No module named requests

From Terminal:

ADs-MacBook-Pro:Downloads ad$ pip3 list

pip (1.5.6)

requests (2.4.1)

setuptools (2.1) 

ADs-MacBook-Pro:Downloads ad$ echo "$PATH"

/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Any suggestion would be appreciated...

By the way, from the downloads folder that I am running the xxx.py file from I get:

$ python

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
...
>>> import requests
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ImportError: No module named requests

But from the folder that has requests library

ad$ pwd
/Library/Python/2.7/site-packages/requests-2.4.1

I get it to work in TERMINAL

requests-2.4.1 ad$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
...
>>> import requests

So I suspect its a PATH or environment variable setup error.

Upvotes: 0

Views: 4617

Answers (2)

Mauro Baraldi
Mauro Baraldi

Reputation: 6575

There is a few issues on this problem.

Are you using virtualenv? If yes, maybe you are forgettting to activate it.

You said that you installed packages using pip3, but trying to run script using Python2.7. Install packages using correct pip version or run script with Python 3. As @josh-smeaton already pointed.

You should add this line below in the first line of your script.

#!/usr/bin/env python

Or this for Python 3

#!/usr/bin/env python3

Upvotes: 0

Josh Smeaton
Josh Smeaton

Reputation: 48730

When you use pip3, you're using the pip associated with python 3.x. You need to execute your script with the related python 3. Try this:

python3 yourscript.py

Upvotes: 1

Related Questions