Here's the structure I'm working with:
directory/
script.py
subdir/
__init__.py
myclass01.py
myclass02.py
What I want to do is import in script.py the classes defined in myclass01.py
and myclass02.py
. If I do:
from subdir.myclass01 import *
It works fine for the class defined in myclass01.py
. But with this solution if there are many classes defined in different files in subdir
and I want to import all of them, I'd have to type one line for each file. There must be a shortcut for this. I tried:
from subdir.* import *
But it didn't work out.
EDIT: here are the contents of the files:
This is __init__.py
(using __all__
as Apalala suggested):
__all__ = ['MyClass01','MyClass02']
This is myclass01.py
:
class MyClass01:
def printsomething():
print 'hey'
This is myclass02.py
:
class MyClass02:
def printsomething():
print 'sup'
This is script.py
:
from subdir import *
MyClass01().printsomething()
MyClass02().printsomething()
This is the traceback that I get when I try to run script.py
:
File "script.py", line 1, in <module>
from subdir import *
AttributeError: 'module' object has no attribute 'MyClass01'