搜尋

首頁  >  問答  >  主體

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

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

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

PHPzPHPz2803 天前874

全部回覆(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
  • 取消回覆