views:

63

answers:

2

I'm developing a Python application for the GAE.

The application consists of a bunch of classes and functions which are at the moment all in the same file main.py.

The application is running without problems.

Now, I want to refactor the application and outsource all the classes. Every class should be in her own file. The files shall be arranged in directories like this:

main.py
/directory1/class1.py
/directory1/class2.py
/directory2/class1.py

My problem is that inside these outsourced classes, I cannot use the functions of main.py.

I tried this inside the class-files.

from main import name_of_function

But the compiler says

    from main import name_of_function
ImportError: cannot import name name_of_function

What did I wrong?

The name of the funktion is login. Maybe this causes the problem?

A: 

Sometimes it is good to leave classes in same module not separate without purpose if they belong together.

The problem of using function from main is sign that you should refactor one module say common_utils.py out of those functions and separate it from main. You can import that to your modules, which use those. Do not think classes only think whole use case.

If you could give pseudo code of your program's logic, we could check the refactoring better together.

Tony Veijalainen
Pseudocode is not so easy because it is over 8000 LOC. The (working) code is here: http://code.google.com/p/koalacloud/source/browse/ This is before I tried to refactor and outsource classes.
Neverland
+2  A: 

Try moving the extra functions from main.py into a separate file.

main.py
library.py # contains login() and other functions from main
/directory1/class1.py
/directory1/class2.py
/directory2/class1.py
robert
I tried to outsource some functions from main.py into library.py.I also copied from main.py into library.py the from ... import ... statements I need for these functions. Inside main.py I insertedfrom library import login1from library import login2from library import login3...But now I have the problem that I get ImportErrors from packages that never made problems before. Is this because I have the same from ... import ... statements inside main.py and library.py ?
Neverland