首页  >  问答  >  正文

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

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

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

PHPzPHPz2741 天前835

全部回复(3)我来回复

  • 天蓬老师

    天蓬老师2017-04-17 15:03:15

    使用绝对导入

    雷雷

    回复
    0
  • 高洛峰

    高洛峰2017-04-17 15:03:15

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

    project_test/

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

    |---init.py

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

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

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

    回复
    0
  • ringa_lee

    ringa_lee2017-04-17 15:03:15

    import A

    回复
    0
  • 取消回复