python3.4中怎么导入__init__.py中的类


项目的结构时
lib------
      |--__init__.py
      |
      |--page.py

我在 __init__.py中定义了一个class A,请问怎么在page.py中导入这个类?
注意在python3.4环境中。

python3.x python import

vertu 9 years, 3 months ago

import A

renhono answered 9 years, 3 months ago

我的测试环境的 python 3.4
题主,我建立了一个Project测试了一下,文件结构如下:

project_test/

|---test.py
|---test

|--- init .py

可见,test是一个package
__init__.py 中:


 python


 class module_1:
    print('This is a test!')
    pass

当我在test.py中 import test ,运行
输出的结果是:
This is a test
说明,在 import 这个 package 的时候,该类自动就实例化了, 这样, 对于你的问题,应该直接调用就好~

扑翼飞行小裤裤 answered 9 years, 3 months ago

利用absolute_import


 python


 from . import A

伊蓝月eR answered 9 years, 3 months ago

Your Answer