Python3:尝试以非包形式进行相对导入


问题内容

我对此基本问题感到非常抱歉,因为它与以下内容类似:
受相对进口困扰

但是我正在尝试遵循PEP328 http://www.python.org/dev/peps/pep-0328/#guido-s-
decision

,它对我不起作用:(

这些是我的文件:

dev@desktop:~/Desktop/test$ ls
controller.py  __init__.py  test.py

2to3说的没错:

dev@desktop:~/Desktop/test$ 2to3 .
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: No files need to be modified.

文件内容:

dev@desktop:~/Desktop/test$ cat controller.py 
class Controller:
    def __init__(self):
        pass

dev@desktop:~/Desktop/test$ cat __init__.py 
# -*- coding: utf-8 -*-

dev@desktop:~/Desktop/test$ cat test.py 
#!/usr/bin/env python
from .controller import Controller 
if __name__ == '__main__':
    print('running...')

但是导入它不起作用:

dev@desktop:~/Desktop/test$ python3 test.py 
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from .controller import Controller 
ValueError: Attempted relative import in non-package
dev@desktop:~/Desktop/test$

任何帮助表示赞赏!提前致谢!


问题答案:

您不能 包中使用脚本;你在跑步test不是 test.test。因此,顶层脚本不能使用相对导入。

如果你想运行包作为一个脚本,你需要移动test/test.pytestpackage/__main__.py,在你的shell移动一个目录时~/Desktop,告诉Python来运行一个包python -m testpackage

演示:

$ ls testpackage/
__init__.py   __main__.py   __pycache__   controller.py
$ cat testpackage/controller.py 
class Controller:
    def __init__(self):
        pass

$ cat testpackage/__init__.py 
# -*- coding: utf-8 -*-

$ cat testpackage/__main__.py 
from .controller import Controller
if __name__ == '__main__':
    print('running...')

$ python3.3 -m testpackage
running...

您无法命名包裹test; Python已经为测试套件提供了这样一个软件包,可以在找到当前工作目录中的软件包之前找到它。

另一种方法是在包 外部 创建脚本,然后从脚本导入包。