How to import the class within the same directory or sub directory?

后端 未结 13 1122
醉梦人生
醉梦人生 2020-11-22 06:34

I have a directory that stores all the .py files.

bin/
   main.py
   user.py # where class User resides
   dir.py # where class Dir resides
         


        
13条回答
  •  遥遥无期
    2020-11-22 07:08

    In your main.py:

    from user import Class
    

    where Class is the name of the class you want to import.

    If you want to call a method of Class, you can call it using:

    Class.method

    Note that there should be an empty __init__.py file in the same directory.

提交回复
热议问题