import urllib3 works in terminal but not on IDLE

2019-09-13 02:31发布

I am using Mac OSX 10.10.5, and Python version 3.5.2, and IDLE version 3.5.2.

I am extremely new to Python, and am trying to use the urllib3 module in IDLE. I have used the following code in the Terminal with success (the number 200 is returned):

import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/robots.txt')
r.status

But the same code does not work in IDLE. In IDLE I get the following error:

Traceback (most recent call last):
  File "/Users/faculty/Documents/Python/Scraping_v1_d1.py", line 1, in <module>
    import urllib3
ImportError: No module named 'urllib3'

I have also attempted to use other code such as the following in IDLE:

import urllib3
htmlfile = urllib3.urlopen("http://google.com")
htmltext = htmlfile.read()
print (htmltext)

But I get the same error.

In my site-packages folder I have these pip and urllib3 folders:

1) pip

2) pip-9.0.1.dist-info

3) urllib3

4) urllib3-1.19.dist-info

I found one source that suggested that I try to do the following:

import sys
sys.version
sys.path

This is the response in Terminal:

import sys sys.version '2.7.10 (default, Jul 14 2015, 19:46:27) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]' sys.path ['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

When I type the same code into IDLE, nothing happens (this is all I get):

========= RESTART: /Users/faculty/Documents/Python/Scraping_v1_d1.py =========

I have searched the web and stackoverflow.com extensively, but can't locate a solution. Does anyone have any insight?

Thanks!

1条回答
Deceive 欺骗
2楼-- · 2019-09-13 03:03

Like the error message already shows:

You have two Python Versions on your Mac. Python 3.5 and Python 2.7 (by default).

The IDLE process depends on the python version in which IDLE starts. So make sure your using the same Python version (same IDLE). The command "pip install urllib3" only works for the default python version - which is different to your python version in IDLE.

So you just have to install urllib3 for Python3.5 too.

查看更多
登录 后发表回答