python3中导入的更改

python3中导入的更改,python,python-3.x,python-2.7,import,python-import,Python,Python 3.x,Python 2.7,Import,Python Import,我正在使用Python3.4.4并通过创建一个示例包来测试“init.py”特性,但无法实现。上述案例在Python2.7.13版本中运行良好。谁能告诉我我犯了什么错误,或者Python3.x版本的语法是否有任何变化。请帮我学习Python 3 目录结构: TestPackage/ __init__.py TestModule.py run.py from TestPackage import TestFun TestFun() Traceback (most recent

我正在使用Python3.4.4并通过创建一个示例包来测试“init.py”特性,但无法实现。上述案例在Python2.7.13版本中运行良好。谁能告诉我我犯了什么错误,或者Python3.x版本的语法是否有任何变化。请帮我学习Python 3

目录结构:

TestPackage/
    __init__.py
    TestModule.py
run.py
from TestPackage import TestFun
TestFun()
Traceback (most recent call last):
  File "D:\CASE03\run01.py", line 1, in <module>
    from TestPackage import TestFun
  File "D:\CASE03\TestPackage\__init__.py", line 1, in <module>
    from TestModule import TestFun
ImportError: No module named 'TestModule'
TestModule.py的内容:

def TestFun():
    print("Welcome")
from TestModule import TestFun
init.py的内容:

def TestFun():
    print("Welcome")
from TestModule import TestFun
run.py的内容:

TestPackage/
    __init__.py
    TestModule.py
run.py
from TestPackage import TestFun
TestFun()
Traceback (most recent call last):
  File "D:\CASE03\run01.py", line 1, in <module>
    from TestPackage import TestFun
  File "D:\CASE03\TestPackage\__init__.py", line 1, in <module>
    from TestModule import TestFun
ImportError: No module named 'TestModule'
执行run.py文件时,出现以下错误:

TestPackage/
    __init__.py
    TestModule.py
run.py
from TestPackage import TestFun
TestFun()
Traceback (most recent call last):
  File "D:\CASE03\run01.py", line 1, in <module>
    from TestPackage import TestFun
  File "D:\CASE03\TestPackage\__init__.py", line 1, in <module>
    from TestModule import TestFun
ImportError: No module named 'TestModule'
回溯(最近一次呼叫最后一次):
文件“D:\CASE03\run01.py”,第1行,在
从TestPackage导入TestFun
文件“D:\CASE03\TestPackage\\uuuuu init\uuuuuu.py”,第1行,在
从TestModule导入TestFun
ImportError:没有名为“TestModule”的模块

但是当我使用Python2.7.13时,它工作得非常好。请指导我。

最简单的解决方案是将
\uuuu init\uuuuu.py
设置为空白文件。如果您有兴趣控制从Testmodule import*执行
时从您的模块导入的内容,如果您更改,您可以将
\UuuuUuUuUuUu=['TestFun']
包含在
\UuInit\UuUuUuUuUuUuUuUuUuPy
文件中

from TestModule import TestFun

您将获得预期的行为


请参阅:(有关使用前导点的相对导入的部分)。

尝试将
\uuuu init\uuuuuuuuuupy
更改为以下代码:

from TestPak.TestModule import TestFun

这是(蟒蛇3的变化)。()虽然这会起作用,但它会将init文件的内容与包名(可能会更改)耦合起来。这不是什么大问题,但相对重要性(前导点)是存在的,而且看起来更干净。