hello everybody!

I have a main app, which imports another python file and uses its functions.

my problem is now, that the external file itself needs some functions defined in the main app...

if i copy the external files code and paste it into the main application, everything works fine, but this is not the thing I want...

is there a python function which is similar to the C's #include function? As far as I know, #include "writes" the external files source into the main application..?

thank you for every bit of help=)

greetings,
CobRalf

There is not (#include is often considered to be one of the ugly warts in C). You can probably refactor your code so you have three files: main.py , external.py , and used_by_both.py , then import used_by_both into main and into external.

If you cannot find a way to refactor the code, then you probably should re-think how your are handling the problem: Circular dependencies are often (though not always) evil.

The external module should never need some code from the main module, so as griswolf said, you should probably refactor the code.

thank you for your quick replies!

I am am developping a python-OS-simulation and I'm currently writing the environement for external programs (you can code them using a simple, special gui-language, i got the idea from AutoIt...=)). The main app organizes the windows, inputs, clicks, and has all the functions needed for creating a gui (GuiCreate,GuiCreateButton,etc...)

perhaps I will try to remove the gui functions from the main app and put it into a third .py file... it will be a lot of work, I suppose...=)

Thanx,
CobRalf

ps: I also tried the "execfile" function... trought this, the external app has access to all functions in my main app, but after the exteral App has finished with execution, all the external functions are not available any more.... =(
is it possible to mix up execfile and import??

One option to look at when you get into this level of coding complexity is callbacks. It will be at least as much work, maybe more, but gives you a lot of flexibility. It works like this:

#commoncode.py
def doSomething(kwargs):
  return str(kwargs)
def useACallback(cbfunction,*args,**kwargs):
  neededValue = doSomething(kwargs)
  return cbfunction(neededValue)

#...

#maincode.py
import commoncode
def getCalledBack(aValue):
  print "This is a callback function, called with %s",aValue
# ...
commoncode.useACallback(getCalledBack,getPrinted="GET PRINTED")

The trick is to think clearly about what functions need this level of complexity, and be clever about the design so it ends up being (re)usable.

Edited 6 Years Ago by griswolf: n/a

This article has been dead for over six months. Start a new discussion instead.