I have been trying to fix this for a bit, and I must be missing something basic here (forgive me, I am relatively new to Python development):
I have a package structure like this:
base
|
-->util
__init__.py
Class1.py
Class2.py
__init__.py
Main.py
My classes are fairly benign:
class Class1(object):
def __init__(self):
# some methods...
class Class2(object):
def __init__(self):
# more methods...
In the Main.py, I have:
import utils
if __name__ == '__main__':
c1 = utils.Class1()
c2 = utils.Class2()
My PYTHONPATH is setup to include src, src\base, and src\base\utils. But, Python gives me this error when trying to run Main.py:
AttributeError: 'module' object has no attribute 'Class1'
Has someone encountered this, and do you know how to fix it? Thanks!
This is a little different than Java. In java each file is usually a class, in python, each file is a module. Given the scenario you describe here, you would do:
You could do a number of things here. For example, you could have a module called “resources” like this:
which contains both Class1 and Class2. Then you could do something like:
etc. But the key is that classes != files in python