我想从同一目录中的另一个文件导入一个函数。

通常,以下工作之一:

from .mymodule import myfunction
from mymodule import myfunction

…但另一个给了我一个错误:

ImportError: attempted relative import with no known parent package
ModuleNotFoundError: No module named 'mymodule'
SystemError: Parent module '' not loaded, cannot perform relative import

这是为什么?


当前回答

我在使用Django时经常遇到这种情况,因为很多功能都是从manage.py脚本执行的,但我也想让我的一些模块也可以直接作为脚本运行(理想情况下,你可以让它们成为manage.py指令,但我们还没有)。

这是这样一个项目可能看起来的模型;

├── dj_app
│   ├── models.py
│   ├── ops
│   │   ├── bar.py
│   │   └── foo.py
│   ├── script.py
│   ├── tests.py
│   ├── utils.py
│   └── views.py
└── manage.py

这里的重要部分是manage.py、dj_app/script.py和dj_app/tests.py。我们还有子模块dj_app/ops/bar.py和dj_app/ops/foo.py,它们包含了我们希望在整个项目中使用的更多项目。

问题的根源通常是希望您的dj_app/script.py脚本方法在dj_app/tests.py中包含测试用例,当您运行manage.py测试时会调用这些测试用例。

这是我如何设置项目及其导入的;

# dj_app/ops/foo.py
# Foo operation methods and classes
foo_val = "foo123"

.

# dj_app/ops/bar.py
# Bar operations methods and classes
bar_val = "bar123"

.

# dj_app/script.py
# script to run app methods from CLI

# if run directly from command line
if __name__ == '__main__':
    from ops.bar import bar_val
    from ops.foo import foo_val

# otherwise
else:
    from .ops.bar import bar_val
    from .ops.foo import foo_val

def script_method1():
    print("this is script_method1")
    print("bar_val: {}".format(bar_val))
    print("foo_val: {}".format(foo_val))


if __name__ == '__main__':
    print("running from the script")
    script_method1()

.

# dj_app/tests.py
# test cases for the app
# do not run this directly from CLI or the imports will break
from .script import script_method1
from .ops.bar import bar_val
from .ops.foo import foo_val 

def main():
    print("Running the test case")
    print("testing script method")
    script_method1()

if __name__ == '__main__':
    print("running tests from command line")
    main()

.

# manage.py
# just run the test cases for this example
import dj_app.tests
dj_app.tests.main()

.

从manage.py运行测试用例;

$ python3 manage.py
Running the test case
testing script method
this is script_method1
bar_val: bar123
foo_val: foo123

自行运行脚本;

$ python3 dj_app/script.py
running from the script
this is script_method1
bar_val: bar123
foo_val: foo123

请注意,如果尝试直接运行test.py,则会出现错误,因此不要这样做;

$ python3 dj_app/tests.py
Traceback (most recent call last):
  File "dj_app/tests.py", line 5, in <module>
    from .script import script_method1
ModuleNotFoundError: No module named '__main__.script'; '__main__' is not a package

如果我遇到了更复杂的进口情况,我通常会执行这样的操作来破解它;

import os
import sys
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, THIS_DIR)
from script import script_method1
sys.path.pop(0)

其他回答

将其放入包的__init__.py文件中:

# For relative imports to work in Python 3.6
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

假设您的包是这样的:

├── project
│   ├── package
│   │   ├── __init__.py
│   │   ├── module1.py
│   │   └── module2.py
│   └── setup.py

现在在您的包中使用常规导入,例如:

# in module2.py
from module1 import class1

这在python 2和3中都有效。

太长,读不下去了通过在python脚本的入口点添加以下内容,将脚本路径附加到系统路径。

import os.path
import sys
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

现在,您可以在PyCharma和Terminal中运行项目了!!

从同一目录导入

首先,您可以从同一目录导入。

这是文件结构。。。

Folder
 |
 ├─ Scripts
 |   ├─ module123.py
 |
 ├─ main.py
 ├─ script123.py

这是main.py

from . import script123
from Scripts import module123

如您所见,从导入。从当前目录导入。

注意:如果使用IDLE以外的任何方法运行,请确保在运行之前将终端导航到与main.py文件相同的目录。

此外,从本地文件夹导入也有效。

从父目录导入

正如我在GitHub中看到的,有以下方法。

采用以下文件树。。。

ParentDirectory
 ├─ Folder
 |   |
 |   ├─ Scripts
 |   |   ├─ module123.py
 |   |
 |   ├─ main.py
 |   ├─ script123.py
 |
 ├─ parentModule.py

然后,只需将此代码添加到main.py文件的顶部。

import inspect
import os
import sys

current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)

from ParentDirectory import Stuff

如果以下导入:

from . import something 

不适用于您,这是因为这是python打包导入,不会与您的常规实现一起使用,下面是一个示例来演示如何使用它:

文件夹结构:

.
└── funniest
    ├── funniest
    │   ├── __init__.py
    │   └── text.py
    ├── main.py
    └── setup.py 

内部__init__.py添加:

def available_module(): 
    return "hello world"

text.py添加:

from . import available_module

在setup.py中添加

from setuptools import setup

setup(name='funniest',
  version='0.1',
  description='The funniest joke in the world',
  url='http://github.com/storborg/funniest',
  author='Flying Circus',
  author_email='flyingcircus@example.com',
  license='MIT',
  packages=['funniest'],
  zip_safe=False)

现在,这是安装软件包最重要的部分:

pip install .

在我们的系统中使用相同Python的任何地方,我们现在都可以这样做:

>> import funnies.text as fun
>> fun.available_module() 

这应该输出“hello world”

您可以在main.py中测试它(这不需要安装任何软件包)

这也是main.py

import funniest.text as fun 
print(fun.available_module())

这是我的项目结构

├── folder
|   | 
│   ├── moduleA.py
|   |   |
|   |   └--function1()
|   |       └~~ uses function2()
|   | 
│   └── moduleB.py
|       | 
|       └--function2()
|   
└── main.py
     └~~ uses function1()

这里我的模块A导入模块B,主导入模块A

我在模块A中添加了下面的代码段以导入模块B

try:
    from .moduleB import function2 
except:
    from moduleB import function2 

现在我可以单独执行main.py和moduleA.py

这是解决方案吗?