python import出错_Python ImportError: cannot import name urlopen错误分析
在學習python的socket編程過程中遇到一個錯誤,錯誤的提示信息如下:
ImportError: cannot import name urlopen,出現該問題的原因,主要是因為我的目錄下有一個socket.py的問題,這個文件正好和socke模塊中的socket.py文件名重名導致的。
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
出現上面的問題,可以查閱一下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導入模塊時,Python使用PYTHONPATH的搜索路徑,我們可以通過下面的命令,查看Python的搜索路徑:
import sys,pprint
>>> pprint.pprint(sys.path)
['', //表示的是當前路徑,也就是現在python測試程序的路徑
'/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']
上面的問題就是因為,在Import模塊式,先搜索的是home/zfz/python目錄,在該目錄下也正好有個socket.py的Python文件。
對于模塊的導入會有下面的情況,在使用自己編寫的python模塊時,在另個文件中進行導入操作,對應的導入文件會生成.pyc結尾的文件。假設我們有個python的測試文件test_import_exam.py文件,需要導入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
第一次導入test_import時,test_import.py就會被編譯為字節碼,此時該目錄下就會出現一個名為test_import.py的文件,因為*.pyc格式比*.py格式導出的速度更快,如果更新了test_import.py則必須把它之前被編譯的字節碼test_import.pyc刪除,否則在使用這個模塊的時候就不會看到更新的部分,因為它是從第一次生成的test_import.pyc里面導出來;必須對新的test_import.py腳本編譯成*.pyc文件或是直接把test_import.py(第一次生成)刪掉,python解釋器會自動沒有沒有相應*.pyc的*.py文件生成相應的*.pyc文件(基本規則:當導入一個模塊時,python解釋器先把*.py文件編譯成*.pyc,然后在從*.pyc里面導出)。編譯出來的pyc文件都是字節碼文件,當使用cat打印該文件時,查看到的是亂碼。
什么是pyc文件
pyc是一種二進制文件,是由py文件經過編譯后,生成的文件,是一種byte code,py文件變成pyc文件后,加載的速度有所提高,而且pyc是一種跨平臺的字節碼,是由python的虛擬機來執行的,這個是類似于JAVA或者.NET的虛擬機的概念。pyc的內容,是跟python的版本相關的,不同版本編譯后的pyc文件是不同的,2.5編譯的pyc文件,2.4版本的python是無法執行的。
總結
以上是生活随笔為你收集整理的python import出错_Python ImportError: cannot import name urlopen错误分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python怎么安装turtle_Pyt
- 下一篇: python爬取换页_一个可识别翻页的简