tags:

views:

62

answers:

4

I am trying to keep a somewhat organized directory structure as I plan to add more and more scripts. So lets say I have a structure like this:

/src/main.py
/src/db/<all my DB conn & table manipulation scripts>
/src/api/<all my scripts that call different APIs>

My main.py script will include certain classes from db & api folders as needed. I have the blank __init__.py files in each folder so they are included fine. But say I want to include a class from the db folder in a script in the api folder? Like I would need to back up one dir somehow? The api scripts fail when I have a line like this in them:

from db.Conn import QADB

I am on v2.6.

Update: I have tried a relative import, but get this?

from ..db.Conn import QADB 
     ^ SyntaxError: 
invalid syntax
+1  A: 

Use relative imports:

from ..db.Conn import QADB
awesomo
Hah. I didn't even bother to try that. Thought it would completely fail. That's what I get I suppose. But I get this error: from ..db.Conn import QADB ^ SyntaxError: invalid syntax
Hallik
After meddling with them several times, relative imports are still a mystery to me.
delnan
If the src folder is a package, then you could import from there as well: from src.db import QADB
pmalmsten
@ Hallik: as Gabi mentioned in the question comments, you must be missing the `__init__.py` file in the top-level `src` directory.
awesomo
+1  A: 

FWIW, I don't like that structure. Base your structure on what scripts do. What if you want to write a script that calls an external database API. Does that go in the API directory or the db directory?

And yeah, use relative inputs as awesomo suggested.

aaronasterling
+1  A: 

Move everything under a common package:

mypkg/main.py
mypkg/db/...
mypkg/api/..

then use the absolute import

from mypkg.db.stuff import somestuff

This way you can also distribute mypkg as an egg.

Marco Mariani
+3  A: 

The way you have it setup is creating three different modules - this may or may not be what you want to do. If you want a common module which can manage different tasks you could arrange it as follows:

mymodule
 |- __init__.py
 |--database
 |   |- __init__.py
 |   |- dbclasses.py
 | 
 |--api
 |   |- __init__.py
 |   |- apiclasses.py
 |
 |--other

[etc]

If you have it like this and want to use the API and database functionalities, you can start by saying:

from mymodule.database.dbclasses import MyDBClass
from mymodule.api.apiclasses import MyAPIClass

Notice the way you had it: the name of your top "module" was src (it was not a module because it did not have an __init__.py file).

If you use many common functionalities in the top module (from either submodule) you can include them in the top __init__.py and simply call from mymodule import MyDBClass, MyAPIClass.

Contents of top __init__.py:

from mymodule.database.dbclasses import MyDBClass
from mymodule.api.apiclasses import MyAPIClass
__all__ = ['MyDBClass', 'MyAPIClass']
Arrieta
Perfect! This is what I need to do. Thanks!
Hallik