I have the following directory structure:
ican/
__init__.py
haz/
__init__.py
apple.py
grape/
__init.py
grape.py
where apple.py and grape.py have fruit class definitions. In another test file, I am trying to load a fruit class via something akin to myapple = ican.haz.apple() or mygrape = ican.haz.grape().
What is the correct directory structure and import structure for loading a class of this sort? I have also tried something like import ican.haz as icanhaz and then calling myapple = icanhaz.apple(). I am looking to put all of my classes in the same spot and load them via something like ican.haz.<class>.
You are confusing two concepts. A python "module" is a file containing python code that can be imported. A python "class" is something that can be defined in a module.
In your example, apple and grape are modules. You cannot calls a module by doing something like apple(), it isn't allowed. You would need to import the class contained in the module.
So say apple.py had a GetApple class defined in it. So apple.py looked like this:
class GetApple(object):
def __init__(self):
print("I have an apple!")
Modules can also have functions, so you could have a GetApple function instead:
def GetApple():
print("I have an apple!")
Modules can also have variables. They can have any number of variables, classes, and functions. You can either import them individually (such as with from apple import GetApple, or import the module and access them from the module import apple. But you cannot import and then call a module (at least not in any reasonable way), only the functions or and classes inside it.
For your directory structure, you could then run it using any of these approaches:
>>> from ican.haz.apple import GetApple
>>> GetApple()
I have an apple!
>>> from ican.haz import apple
>>> apple.GetApple()
I have an apple!
>>> import ican.haz.apple
>>> ican.haz.apple.GetApple()
I have an apple!
And, depending on your __init__.py files, possibly also:
>>> import ican.haz
>>> ican.haz.apple.GetApple()
I have an apple!
>>> import ican
>>> ican.haz.apple.GetApple()
I have an apple!
For grape.py, assuming a corresponding class, this sort of thing would work:
>>> from ican.haz.grape.grape import GetGrape
>>> GetGrape()
I have a grape!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With