Python 强制从当前目录导入模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14216200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:47:11  来源:igfitidea点击:

Force importing module from current directory

python

提问by Zaar Hai

I have package pthat has modules aand b. arelies on b:

我有p包含模块ab. a依赖于b

b.pycontents:

b.py内容:

import a

However I want to ensurethat bimports my amodule from the same ppackage directory and not just any amodule from PYTHONPATH.

不过,我想确保的是b进口我a来自同一个模块p包目录,不是任何a的模块PYTHONPATH

So I'm trying to change b.pylike the following:

所以我试图改变b.py如下:

from . import a

This works as long as I import bwhen I'm outside of ppackage directory. Given the following files:

只要b我在p包目录之外导入,这就会起作用。鉴于以下文件:

/tmp
    /p
       a.py
       b.py
       __init__.py

The following works:

以下工作:

$ cd /tmp
$ echo 'import p.b' | python

The following does NOT work:

以下不起作用:

$ cd /tmp/p
$ echo 'import b' | python
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "b.py", line 1, in <module>
    from . import a
ValueError: Attempted relative import in non-package

Why?

为什么?

P.S. I'm using Python 2.7.3

PS我使用的是Python 2.7.3

采纳答案by Zaar Hai

After rereading the Python import documentation, the correct answer to my original problem is:

重读 Python导入文档后,我原来问题的正确答案是:

To ensure that bimports afrom its own package its just enough to write the following in the b:

为了确保从它自己的包中b导入a,只需在 中写入以下内容b

import a

Here is the quote from the docs:

这是文档中的引用:

The submodules often need to refer to each other. For example, the surround module might use the echo module. In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

子模块经常需要相互引用。例如,环绕声模块可能使用回声模块。事实上,这样的引用是如此常见,以至于 import 语句在查找标准模块搜索路径之前首先在包含包中查找。

Note:As J.F. Sebastian suggest in the comment below, use of implicit imports is not advised, and they are, in fact, gone in Python 3.

注意:正如 JF Sebastian 在下面的评论中所建议的,不建议使用隐式导入,事实上,它们在 Python 3 中已经消失了。

回答by Aaron Digulla

Because there is an __init__.pyfile in /p. This file tells Python: "All modules in this folder are in the package p".

因为里面有一个__init__.py文件/p。该文件告诉 Python:“此文件夹中的所有模块都在包中p”。

As long as the __init__.pyfile exists, you can import bas p.b, no matter where you are.

只要__init__.py文件存在,无论您身在何处,都可以导入bp.b

So the correct import in b.pywould be: import p.a

所以正确的导入b.py是:import p.a

回答by glglgl

Relative imports only work within packages.

相对导入仅在包内有效。

If you import bfrom where you are, there is no notion of a package, and thus there is no way for a relative import.

如果b从您所在的位置导入,则没有包的概念,因此无法进行相对导入。

If you import p.b, it is module bwithin package c.

如果你导入p.b,它是b包内的模块c

It is not the directory structure which matters, but the packages structure.

重要的不是目录结构,而是包结构。