简体   繁体   English

导入urllib3在终端中有效,但在IDLE上无效

[英]import urllib3 works in terminal but not on IDLE

I am using Mac OSX 10.10.5, and Python version 3.5.2, and IDLE version 3.5.2. 我正在使用Mac OSX 10.10.5,Python版本3.5.2和IDLE版本3.5.2。

I am extremely new to Python, and am trying to use the urllib3 module in IDLE. 我对Python极为陌生,并且正在尝试在IDLE中使用urllib3模块。 I have used the following code in the Terminal with success (the number 200 is returned): 我已经在终端成功使用了以下代码(返回了数字200):

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. 但是相同的代码在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: 我还试图在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: 在我的site-packages文件夹中,有以下pip和urllib3文件夹:

1) pip 1)点

2) pip-9.0.1.dist-info 2)pip-9.0.1.dist-info

3) urllib3 3)urllib3

4) urllib3-1.19.dist-info 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-pack import sys sys.version'2.7.10(default,Jul 14 2015,19:46:27)\\ n [GCC 4.2.1兼容的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-pack ages'] 年龄]

When I type the same code into IDLE, nothing happens (this is all I get): 当我在IDLE中键入相同的代码时,什么也没发生(这就是我得到的一切):

========= RESTART: /Users/faculty/Documents/Python/Scraping_v1_d1.py ========= =========重新启动:/Users/faculty/Documents/Python/Scraping_v1_d1.py =========

I have searched the web and stackoverflow.com extensively, but can't locate a solution. 我已经广泛搜索了web和stackoverflow.com,但是找不到解决方案。 Does anyone have any insight? 有人有见识吗?

Thanks! 谢谢!

Like the error message already shows: 就像已经显示的错误消息一样:

You have two Python Versions on your Mac. 您的Mac上有两个Python版本。 Python 3.5 and Python 2.7 (by default). Python 3.5和Python 2.7(默认情况下)。

The IDLE process depends on the python version in which IDLE starts. IDLE进程取决于IDLE开始的python版本。 So make sure your using the same Python version (same IDLE). 因此,请确保您使用相同的Python版本(相同的IDLE)。 The command "pip install urllib3" only works for the default python version - which is different to your python version in IDLE. 命令“ pip install urllib3”仅适用于默认的python版本-与您在IDLE中的python版本不同。

So you just have to install urllib3 for Python3.5 too. 因此,您也只需要安装适用于Python3.5的urllib3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM