Home  >  Q&A  >  body text

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

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

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

PHPzPHPz2741 days ago838

reply all(3)I'll reply

  • 天蓬老师

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

    Use absolute_import

    pythonfrom . import A
    

    reply
    0
  • 高洛峰

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

    My test environmentpython 3.4
    Questioner, I created a Project and tested it. The file structure is as follows:

    project_test/

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

    |---init.py

    It can be seen that test is a package
    __init__.py:

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

    When I import test in test.py, run
    The output result is:
    This is a test
    Note that when importthispackage occurs, the class is automatically instantiated. In this way, for your question, you should just call it directly~

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:03:15

    import A

    reply
    0
  • Cancelreply