0

I have a simlair problem as in this question: AttributeError: 'module' object has no attribute

I'm doing a mutual top level import which is causing me to have AttributeError: 'module' object has no attribute error so but I'm not sure if I can import within a function because I'm basically importing a file that controls all the other modules.

Here's an example(file2.py):

import file1
count = 0
while count < file1.number_of_loops:
 .....

My functions are being triggered by the control variables so I'm unable to import within the function itself. Short of creating a sperate file to control, is there a way to refer to variables in another file without doing a mutual top level import?

Community
  • 1
  • 1
Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • pycharm seems to be a lot nicer than eclipse. – Paulo Scardine Jan 14 '12 at 05:10
  • is this a problem with eclipse or how I am designing my program? – Lostsoul Jan 14 '12 at 05:48
  • Sorry, why you can't import file1 from file2 functions? May be you've already red this [FAQ](http://docs.python.org/faq/programming.html#how-can-i-have-modules-that-mutually-import-each-other), I just leave it here :) – demalexx Jan 14 '12 at 05:55
  • @Lostsoul: perhaps both. for pure parametric variables, approaches like using the ConfigParser module may be better design. – Paulo Scardine Jan 14 '12 at 06:05

2 Answers2

2

As pointed out by a comment, if you're using file1.py to store some configuration variables, then it would be better to have a look at alternative ways of dealing with that configuration.

In my opinion, a simple way to address your problem would be to move all those variables to another module called, for example, settings.py so that you can get the configuration from there without having to use any mutual import.

jcollado
  • 39,419
  • 8
  • 102
  • 133
0

I will advice against an import from a module that controls "all the other modules".

My suggestion is to put all that control code inside functions (or classes), and then import the functions and the paramenters where you need to execute them.

For example, file2.py:

def loop_control(num_loops):
    count = 0
    while count < num_loops:
        # ...

another file:

import file1
import file2

file2.loop_control(file.number_of_loops)

If you need to communicate back and forth the results of this controls you may consider building a Manager just for that (implemented in the way that you prefer).

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82