Python3.x和Python2.x版本相容問題__Python

來源:互聯網
上載者:User
Python3.x和Python2.x版本相容問題
重組和重新命名標準庫

為了一致性和使用起來更容易,Python的標準庫在Python 3已經被重組了。所有的模組名現在符合Python代碼的風格引導,PEP 8[1];並且一些模組被合并了。

2to3包含的固定器可以處理所有的這些,所以這個章節更多的是引起你是否需要不用2to3轉換同時支援Python 2和Python 3的興趣。

six模組[2]對大多數重組的標準庫的整合都有支援。你可以從six.moves匯入重組的模組:

>>> from six.moves import cStringIO

在Python 2這等效於:

>>> from cStringIO import StringIO

然而在Python 3這將等效於:

>>> from io import StringIO

如果你想要不用轉換並且不想用six模組同時Python 2和Python 3,這也是很容易的。你只需要償試從一個位置匯入,捕捉錯誤然後從其他位置匯入。它和你放Python 3的位置在前或者在後沒有關係,都可以同樣很好地起作用:

>>> try:...     from io import StringIO... except ImportError:...     from cStringIO import StringIO

這個表格包含除了另外列出表格的urllib、urllib2 和urlparse重組外的所有標準庫的重新命名和重組:

Python 2 名字 Python 3 名字 six名字
anydbm dbm  
BaseHTTPServer http.server BaseHTTPServer
__builtin__ builtins builtins
CGIHTTPServer http.server CGIHTTPServer
ConfigParser configparser configparser
copy_reg copyreg copyreg
cPickle pickle cPickle
cProfile profile  
cStringIO.StringIO io.StringIO cStringIO
Cookie http.cookies http_cookies
cookielib http.cookiejar http_cookiejar
dbhash dbm.bsd  
dbm dbm.ndbm  
dumbdb dbm.dumb  
Dialog tkinter.dialog tkinter_dialog
DocXMLRPCServer xmlrpc.server  
FileDialog tkinter.FileDialog tkinter_filedialog
FixTk tkinter._fix  
gdbm dbm.gnu  
htmlentitydefs html.entities html_entities
HTMLParser html.parser html_parser
httplib http.client http_client
markupbase _markupbase  
Queue queue queue
repr reprlib reprlib
robotparser urllib.robotparser urllib_robotparser
ScrolledText tkinter.scolledtext tkinter_scrolledtext
SimpleDialog tkinter.simpledialog tkinter_simpledialog
SimpleHTTPServer http.server SimpleHTTPServer
SimpleXMLRPCServer xmlrpc.server  
StringIO.StringIO io.StringIO  
SocketServer socketserver socketserver
test.test_support test.support tkinter
Tkinter tkinter tkinter
Tix tkinter.tix tkinter_tix
Tkconstants tkinter.constants tkinter_constants
tkColorChooser tkinter.colorchooser tkinter_colorchooser
tkCommonDialog tkinter.commondialog tkinter_commondialog
Tkdnd tkinter.dnd tkinter_dnd
tkFileDialog tkinter.filedialog

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.