python import出错_Python ImportError: cannot import name urlopen错误分析
在學(xué)習(xí)python的socket編程過(guò)程中遇到一個(gè)錯(cuò)誤,錯(cuò)誤的提示信息如下:
ImportError: cannot import name urlopen,出現(xiàn)該問(wèn)題的原因,主要是因?yàn)槲业哪夸浵掠幸粋€(gè)socket.py的問(wèn)題,這個(gè)文件正好和socke模塊中的socket.py文件名重名導(dǎo)致的。
root@team:~/python# ls
char_format.py numberlines.py socket.pyc
test_help.py test_search.py test_translate.py
drawing re_split.py test_copy.py test_if.py
test_sequeue.py test_wordbook1.py
heap.py reverseargs.py test_fibs.py
test_index.py test_slices.py test_wordbook.py
hello.py socket.py test_for.py test_in.py test_sys.py
root@team:~/python# socket.py
Traceback (most recent call last):
File "./socket.py", line 2, in
from urllib import urlopen
File
"/usr/lib/python2.6/urllib.py", line 26, in
import socket
File "/home/zfz/python/socket.py",
line 2, in
from urllib import urlopen
ImportError: cannot
import name urlopen
出現(xiàn)上面的問(wèn)題,可以查閱一下python查找模塊的方式,找到下面一段話:
Actually, modules are searched in the
list of directories given by the variable sys.path which is initialized from
the directory containing the input script (or the current directory),
PYTHONPATH and the installation- dependent default. This allows Python programs
that know what they’re doing to modify or replace the module search path. Note
that because the directory containing the script being run is on the search
path, it is important that the script not have the same name as a standard
module, or Python will attempt to load the script as a module when that module
is imported.
首先,在使用import導(dǎo)入模塊時(shí),Python使用PYTHONPATH的搜索路徑,我們可以通過(guò)下面的命令,查看Python的搜索路徑:
import sys,pprint
>>> pprint.pprint(sys.path)
['', //表示的是當(dāng)前路徑,也就是現(xiàn)在python測(cè)試程序的路徑
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/pymodules/python2.6',
'/usr/lib/pymodules/python2.6/gtk-2.0',
'/usr/local/lib/python2.6/dist-packages']
上面的問(wèn)題就是因?yàn)?#xff0c;在Import模塊式,先搜索的是home/zfz/python目錄,在該目錄下也正好有個(gè)socket.py的Python文件。
對(duì)于模塊的導(dǎo)入會(huì)有下面的情況,在使用自己編寫的python模塊時(shí),在另個(gè)文件中進(jìn)行導(dǎo)入操作,對(duì)應(yīng)的導(dǎo)入文件會(huì)生成.pyc結(jié)尾的文件。假設(shè)我們有個(gè)python的測(cè)試文件test_import_exam.py文件,需要導(dǎo)入test_import.py,
python# cat test_import_exam.py
#!/usr/bin/python
import test_import
root@team:~/python# cat test_import.py
#!/usr/bin/python
root@team:~/python# cat test_import.pyc
?
+WSc@s
dS
第一次導(dǎo)入test_import時(shí),test_import.py就會(huì)被編譯為字節(jié)碼,此時(shí)該目錄下就會(huì)出現(xiàn)一個(gè)名為test_import.py的文件,因?yàn)?.pyc格式比*.py格式導(dǎo)出的速度更快,如果更新了test_import.py則必須把它之前被編譯的字節(jié)碼test_import.pyc刪除,否則在使用這個(gè)模塊的時(shí)候就不會(huì)看到更新的部分,因?yàn)樗菑牡谝淮紊傻膖est_import.pyc里面導(dǎo)出來(lái);必須對(duì)新的test_import.py腳本編譯成*.pyc文件或是直接把test_import.py(第一次生成)刪掉,python解釋器會(huì)自動(dòng)沒(méi)有沒(méi)有相應(yīng)*.pyc的*.py文件生成相應(yīng)的*.pyc文件(基本規(guī)則:當(dāng)導(dǎo)入一個(gè)模塊時(shí),python解釋器先把*.py文件編譯成*.pyc,然后在從*.pyc里面導(dǎo)出)。編譯出來(lái)的pyc文件都是字節(jié)碼文件,當(dāng)使用cat打印該文件時(shí),查看到的是亂碼。
什么是pyc文件
pyc是一種二進(jìn)制文件,是由py文件經(jīng)過(guò)編譯后,生成的文件,是一種byte code,py文件變成pyc文件后,加載的速度有所提高,而且pyc是一種跨平臺(tái)的字節(jié)碼,是由python的虛擬機(jī)來(lái)執(zhí)行的,這個(gè)是類似于JAVA或者.NET的虛擬機(jī)的概念。pyc的內(nèi)容,是跟python的版本相關(guān)的,不同版本編譯后的pyc文件是不同的,2.5編譯的pyc文件,2.4版本的python是無(wú)法執(zhí)行的。
總結(jié)
以上是生活随笔為你收集整理的python import出错_Python ImportError: cannot import name urlopen错误分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python怎么安装turtle_Pyt
- 下一篇: python爬取换页_一个可识别翻页的简