0

I am trying to build some python script. I have imported (import Handler) set of functions in my main script (start.py). In start.py I call one of the imported functions to dynamically import scripts from config (dir1). I need to use 1 function (message(self, text) what is in my main script start.py in class MyScript(Protocol)) in one of imported files from config. So my directory structure is:

MainDir
  start.py (main script)
  Handler.py
  config.cfg
  dir1 (dir with plugins)
    HelloWorld.py (this needs to use function from start.py)

Can you help me, please? Thank you. EDIT: Start.py is Twisted TCP server, I need tu use some twisted functions in my plugins.

WebScript
  • 67
  • 6
  • Have a look at http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder – suspectus Mar 29 '13 at 20:13
  • The obvious solutions are (a) move those functions out of `start.py` and into a module that both `start` and the plugins can import, (b) make it so that `start` can be imported (that is, make sure you did the `if __name__` bit right, etc.), or (c) pass come collection of functions (maybe even the `start` module itself, if you really want) to whatever needs to call the functions. Is there a reason one of those doesn't work for you? – abarnert Mar 29 '13 at 20:14
  • @suspectus: `RuntimeError: maximum recursion depth exceeded` – abarnert Mar 29 '13 at 20:15
  • My start.py is a twisted TCP server, I need some functions (like send message to client) in my plugins. If I import it, twisted will start new server, so this is not suitable. How can I move function to send message to client outside of twisted protocol? – WebScript Mar 29 '13 at 23:08

1 Answers1

1

Here are a couple more options:

  1. Pass the function as a parameter to the code that calls it (this might be appropriate if you need that code to call different functions is different situations).
  2. Use python's -m option to load start.py as a regular module (i.e., python -m start). Then you can import start and refer to its message function just like any other python module.
Nathan Davis
  • 5,636
  • 27
  • 39