Python Modules

crackp0t

010001100100011101010100
Jun 24, 2009
3,636
70
0
This might sound like a stupid question, but how can I use subdirectories in my modules.

Code:
tools/
  __init__.py
  base/
    base.py
  filehandlers/
    somehandler.py

  something.py

Currently, I have to put all the files in one directory and personally I think it looks unorganized. If there isn't a good way to do what I want then fuck it I guess.

I realize that I can add a __init__.py file in all of the subdirectories, but then they aren't all really part of the same module. Inheritance and other things act different.
 


This might sound like a stupid question, but how can I use subdirectories in my modules.

Code:
tools/
  __init__.py
  base/
    base.py
  filehandlers/
    somehandler.py

  something.py

Currently, I have to put all the files in one directory and personally I think it looks unorganized. If there isn't a good way to do what I want then fuck it I guess.
Great idea, organize thing into nested hierarchies where it makes sense to do so!
I realize that I can add a __init__.py file in all of the subdirectories,
Yes, you can, and you should!
but then they aren't all really part of the same module. Inheritance and other things act different.

wat ?
 
I know it makes sense that's why I was wanting to do it. I structure most of my code like that regardless of the language, because if I don't shit gets unorganized fast.

I got it figured out. I forgot to inherit from object inside of my base class. Being a Python newb that bit me in the ass. I initially thought it was the way I had it structured in the directories, because it appeared to work when all the files were in the same directory. Turns out when I actually tried using it it shit on it's self. Lesson learned and it won't be forgotten anytime soon.
 
FYI if you don't have an __init__.py in a subdirectory it is just treated as a regular subdirectory and you can import like

Code:
from 'subdir/module.py' import ModuleClass

At least I'm PRETTY sure you can do that. But I'm not totally positive because I never do it that way...it's not the best way because directories can get messed up depending on where you're calling it from.

If it has an __init__.py in it, it will be treated as a package.

Read here: http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder