简体   繁体   English

python - 同一目录中模块的绝对导入

[英]python - absolute import for module in the same directory

I have this package: 我有这个包裹:

mypackage/
    __init__.py
    a.py
    b.py

And I want to import things from module a to module b, does it make sense to write in module b 我想从模块a导入到模块b的东西,在模块b中写入是否有意义

from mypackage.a import *

or should I just use 或者我应该使用

from a import *

Both options will work, I'm just wondering which is better (the 2nd makes sense because it's in the same level but I'm considering the 1st to avoid collisions, for example if the system is running from a folder that contains a file named a.py). 两个选项都可以工作,我只是想知道哪个更好(第二个有意义,因为它处于同一级别,但我正在考虑第一个以避免冲突,例如,如果系统从包含名为的文件的文件夹运行a.py)。

You can safely use number 2 because there shouldn't be any collisions - you'll be always importing a module from the same package as the current one. 您可以安全地使用数字2,因为不应该有任何冲突 - 您将始终从与当前包相同的包中导入模块。 Please note, that if your module has the same name as one of the standard library modules, it will be imported instead of the standard one. 请注意,如果您的模块与其中一个标准库模块具有相同的名称,则将导入该模块而不是标准库模块。 From the documentation : 文档

When a module named spam is imported, the interpreter first searches for a built-in module with that name. 导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。 If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path . 如果未找到,则会在变量sys.path给出的目录列表中搜索名为spam.py的文件。 sys.path is initialized from these locations: sys.path从这些位置初始化:

  • the directory containing the input script (or the current directory). 包含输入脚本(或当前目录)的目录。
  • PYTHONPATH (a list of directory names, with the same syntax as the PYTHONPATH (目录名列表,语法与
  • shell variable PATH ). shell变量PATH )。
  • the installation-dependent default. 依赖于安装的默认值。

After initialization, Python programs can modify sys.path . 初始化后,Python程序可以修改sys.path The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. 包含正在运行的脚本的目录位于搜索路径的开头,位于标准库路径之前。 This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. 这意味着将加载该目录中的脚本,而不是库目录中的同名模块。 This is an error unless the replacement is intended. 除非有意更换,否则这是一个错误。 See section Standard Modules for more information. 有关更多信息,请参见标准模块一节

The option from mypackage.a import * can be used for consistency reasons all over the project. from mypackage.a import *的选项可用于整个项目的一致性原因。 In some modules you will have to do absolute imports anyway. 在某些模块中,无论如何都必须进行绝对导入。 Thus you won't have to think whether the module is in the same package or not and simply use a uniform style in the entire project. 因此,您不必考虑模块是否在同一个包中,只需在整个项目中使用统一的样式。 Additionally this approach is more reliable and predictable. 此外,这种方法更可靠,更可预测。

Python style guidelines don't recommend using relative imports: Python样式指南不建议使用相对导入:

Relative imports for intra-package imports are highly discouraged. 不鼓励进行包装内进口的相对进口。 Always use the absolute package path for all imports. 始终对所有导入使用绝对包路径。 Even now that PEP 328 is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; 即使现在PEP 328已经在Python 2.5中完全实现,但它的显式相对导入风格仍然是不鼓励的; absolute imports are more portable and usually more readable. 绝对导入更便携,通常更具可读性。

Since python 2.5 a new syntax for intra-package relative imports has been introduced. 从python 2.5开始,引入了用于包内相对导入的新语法。 Now you can . 现在你可以. to refer to the current module and .. referring to the module being 1 level above. 引用当前模块和..指模块为1级以上。

from . import echo
from .. import formats
from ..filters import equalizer

You should use from mypackage.a import things, you, want . 你应该使用from mypackage.a import things, you, want

There are two issues here, the main one is relative vs absolute imports, the semantics of which changed in Python 3, and can optionally be used in Python 2.6 and 2.7 using a __future__ import. 这里有两个问题,主要是相对vs绝对导入,其语义在Python 3中发生了变化,并且可以选择在使用__future__ import的Python 2.6和2.7中使用。 By using mypackage.a you guarantee that you will get the code you actually want, and it will work reliably on future versions of Python. 通过使用mypackage.a,您可以保证获得实际需要的代码,并且可以在未来的Python版本上可靠地运行。

The second thing is that you should avoid import *, as it can potentially mask other code. 第二件事是你应该避免import *,因为它可能会掩盖其他代码。 What if the a.py file gained a function called sum ? 如果a.py文件获得了一个名为sum的函数怎么办? It would silently override the builtin one. 它会默默地覆盖内置的。 This is especially bad when importing your own code in other modules, as you may well have reused variable or function names. 在其他模块中导入自己的代码时尤其如此,因为您可能已经重用了变量或函数名称。

Therefore, you should only ever import the specific functions you need. 因此,您应该只导入所需的特定功能。 Using pyflakes on your sourcecode will then warn you when you have potential conflicts. 在您的源代码上使用pyflakes将在您有潜在冲突时发出警告。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM