Python3.x和Python2.x版本兼容问题
目录
Python3.x和Python2.x版本兼容问题
Python3.x和Python2.x版本兼容问题
重组和重命名标准库 为了一致性和使用起来更容易,Python的标准库在Python 3已经被重组了。所有的模块名现在符合Python代码的风格引导,PEP 8
;并且一些模块被合并了。 2to3 包含的固定器可以处理所有的这些,所以这个章节更多的是引起你是否需要不用2to3转换同时支持Python 2和Python 3的兴趣。 six模块
对大多数重组的标准库的整合都有支持。你可以从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 | tkinter_tkfiledialog | tkFont | tkinter.font | tkinter_font | tkMessageBox | tkinter.messagebox | tkinter_messagebox | tkSimpleDialog | tkinter.simpledialog | tkinter_tksimpledialog | turtle | tkinter.turtle | UserList | collections | UserString | collections | whichdb | dbm | _winreg | winreg | winreg | xmlrpclib | xmlrpc.client | urllib、urllib2和urlparse urllib、urllib2和urlparse这三个模块被重组进了urllib.request、urllib.parse和urllib.error这三个新的模块。因为没有six的支持,所有你必须要使用前面提到的try/except技巧。 | Python 2 名字 | 移动到 | — | — | — | — | urllib._urlopener | urllib.request | urllib.ContentTooShortError | urllib.error | urllib.FancyURLOpener | urllib.request | urllib.pathname2url | urllib.request | urllib.quote | urllib.parse | urllib.quote_plus | urllib.parse | urllib.splitattr | urllib.parse | urllib.splithost | urllib.parse | urllib.splitnport | urllib.parse | urllib.splitpasswd | urllib.parse | urllib.splitport | urllib.parse | urllib.splitquery | urllib.parse | urllib.splittag | urllib.parse | urllib.splittype | urllib.parse | urllib.splituser | urllib.parse | urllib.splitvalue | urllib.parse | urllib.unquote | urllib.parse | urllib.unquote_plus | urllib.parse | urllib.urlcleanup | urllib.request | urllib.urlencode | urllib.parse | urllib.urlopen | urllib.request | urllib.URLOpener | urllib.request | urllib.urlretrieve | urllib.request | urllib2.AbstractBasicAuthHandler | urllib.request | urllib2.AbstractDigestAuthHandler | urllib.request | urllib2.BaseHandler | urllib.request | urllib2.build_opener | urllib.request | urllib2.CacheFTPHandler | urllib.request | urllib2.FileHandler | urllib.request | urllib2.FTPHandler | urllib.request | urllib2.HTTPBasicAuthHandler | urllib.request | urllib2.HTTPCookieProcessor | urllib.request | urllib2.HTTPDefaultErrorHandler | urllib.request | urllib2.HTTPDigestAuthHandler | urllib.request | urllib2.HTTPError | urllib.request | urllib2.HTTPHandler | urllib.request | urllib2.HTTPPasswordMgr | urllib.request | urllib2.HTTPPasswordMgrWithDefaultRealm | urllib.request | urllib2.HTTPRedirectHandler | urllib.request | urllib2.HTTPSHandler | urllib.request | urllib2.install_opener | urllib.request | urllib2.OpenerDirector | urllib.request | urllib2.ProxyBasicAuthHandler | urllib.request | urllib2.ProxyDigestAuthHandler | urllib.request | urllib2.ProxyHandler | urllib.request | urllib2.Request | urllib.request | urllib2.UnknownHandler | urllib.request | urllib2.URLError | urllib.request | urllib2.urlopen | urllib.request | urlparse.parse_qs | urllib.parse | urlparse.parse_qsl | urllib.parse | urlparse.urldefrag | urllib.parse | urlparse.urljoin | urllib.parse | urlparse.urlparse | urllib.parse | urlparse.urlsplit | urllib.parse | urlparse.urlunparse | urllib.parse | urlparse.urlunsplit | urllib.parse | 移除的模块 一些标准库模块已经被丢弃。UserDict是一个,但这些类中的一部分有一些差不多的替代品,但不是完全的兼容。更多信息见 。 大多数其他被丢弃的模块都是些已经被替换掉很长久的模块,或者是一些给支持时期不长的特殊平台的模块。这个规则适用并例外的是异常模块。它包含异常的等级,但所有它们也是内置的,所以你从来都不需要从异常模块导入任何东西。因此它已经在Python 3被完全移除了。 除了给Solaris、RIX和Mac OS 9的特殊模块,在Python 3中被移除模块列表: | 模块名 | 注释 | — |