简体   繁体   中英

How to import one class from many classes on one python file

Here is is the directory structure:

 ->src/dir/classes.py
 ->src/run.py
# classes.py

class A():
   def methA():
      # class A

class B():
   def MethB():
      # class B

class C():
   def methC():
      # class C

then i need to import Class A in run.py file.

from dir.classes import A

A.methA()

i already tried with using from dir.classes import A but it gives me

ModuleNotFoundError: No module named 'dir.classes'; 'classes' is not a package ModuleNotFoundError: No module named 'dir.classes'; 'classes' is not a package error

So how can i do that?

You need to put__init__.py file on your dir folder. This way dir will be recognized as python package.

First, you must have __init__.py in each directory for Python to recognize them as packages.

Then, you should use from dir.classes import A . A is the name of the class, you shouldn't use Class A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM