简体   繁体   English

如何处理python导入,以便它们与py2和py3轻松配合?

[英]How to deal with python imports so they work easily with py2 and py3?

I am looking for a way to write python packages and modules that makes them Python 3 friendly and also it makes easy to import them. 我正在寻找一种编写python软件包和模块的方法,使它们对Python 3友好,并且也易于导入它们。

Most common case is that you have one major class that you want to provide to the users, call it MyCode . 最常见的情况是,您有一个要提供给用户的主要类,称为MyCode

Your package would be named mycode and you would put the body of MyCode class into mycode/mycode.py . 您的程序包将命名为mycode ,并将MyCode类的主体放入mycode/mycode.py

Now, you would expect that people could do one of these: 现在,您希望人们可以执行以下操作之一:

import mycode
obj = mycode.MyClass()

or: 要么:

from mycode import MyClass()
obj = MyClass()

Now, the question is what you should put inside the __init__.py in order to make this work, in both python 2.6+ and 3.x. 现在,问题是在python 2.6+和3.x中都应该在__init__.py中放入什么才能使它起作用。

Use absolute imports, it'll work fine across python versions. 使用绝对导入,它将在python版本之间正常工作。 Inside mycode/__init__.py put: mycode/__init__.py内部放置:

from __future__ import absolute_import

from mycode.mycode import MyClass

where the __future__ import works from Python 2.5 and onwards; 从python 2.5及更高版本开始__future__导入的位置; see PEP 328 . 参见PEP 328 Without the absolute_import import, import mycode is ambiguous; 如果没有absolute_import导入,则import mycode模棱两可; Python 3 will treat it as absolute and load the top-level package, Python 2 treats it as relative and import the nested module instead. Python 3将其视为绝对,并加载顶级包,Python 2将其视为相对,然后导入嵌套模块。

The alternative is to use a relative import: 另一种方法是使用相对导入:

from .mycode import MyClass

which will work across the same spectrum of versions. 可以在相同的版本范围内使用。

from mycode import *添加到mycode/__init__.py将使mycode/mycode.py的内容可从mycode包中导入,就像它只是一个普通的模块(如mycode.py )一样,与从中import mycode时一样与mycode.py脚本( /mycode/ )相同的目录,或相对导入mycode/mycode.py ...

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

相关问题 Py2 到 Py3:添加未来的导入 - Py2 to Py3: Add future imports 如何使用py2和py3在eclipse中运行python? - how to run python in eclipse with both py2 and py3? Django中不再识别带有前导点的Python导入从py3到py2 - Python imports with leading dot no longer recognised going from py3 to py2 in django 使用pyhdf时如何处理py2和py3中不同类型的np.array(list)? - How to deal with the different type of np.array(list) in py2 and py3 when using pyhdf? 如何编写用于处理py2和py3的URLError异常的python代码 - How do I write python code that handles URLError Exception for both py2 and py3 如何在Python中以与py2和py3一起使用的方式定义二进制字符串? - How to define a binary string in Python in a way that works with both py2 and py3? 将硒从Py3更改为Py2 - Changing Selenium from Py3 to Py2 在Py3而不是Py2上嵌套了“ ImportError” - Nested `ImportError` on Py3 but not on Py2 如何创建一个类似List的类,该类允许在与py2和py3一起使用的切片上调用包含的对象方法 - How to create a List like class which allows calling contained objects methods on slices which work with both py2 and py3 在 py2 和 py3 中处理 Python 文件读取选项 rU 的优雅方式 - Elegant way to handle Python file read option rU in both py2 and py3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM