0

I have two scripts:

scriptone.py

import scripttwo
def test():
    print "Hello World"

scripttwo.test()

scripttwo.py

def test():
    print "Hello World"
    scriptone.test()

The problem I'm having is that the script I imported (scripttwo.py) is unable to access functions from the script it was imported from (scriptone.py); how would one go about doing this?

I do not wish to restructure my code, as all I need is to be able to access functions from the script that a script was imported from.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
jjcyalater
  • 49
  • 2
  • 9
  • 1
    Possible duplicate of [Python files - import from each other](http://stackoverflow.com/questions/9642451/python-files-import-from-each-other) – Mel Oct 23 '15 at 10:27
  • *"I do not wish to restructure my code"* - seriously, why not? You've identified a structural problem, that should be your *first* port of call. – jonrsharpe Oct 23 '15 at 10:38
  • This is not my actual code, rather an example of where this problem may be prevalent. I didn't want answers that only referred to the structure as it would not be relevant to the problem I am facing with my real code. – jjcyalater Oct 23 '15 at 10:40
  • Then could you give an example that is actually relevant to the real problem you're facing? – jonrsharpe Oct 23 '15 at 10:45
  • The example I have provided is fine and faces precisely the same problem that my real code does. I just need a simple answer and if you can not provide one for the example then my real code is no different. – jjcyalater Oct 23 '15 at 10:48
  • 1
    The simple (and correct) answer is to restructure the code, which works just fine for your example. If there's some reason that won't work for your actual code, then **it's not a representative example**. You can't have it both ways. – jonrsharpe Oct 23 '15 at 11:12

1 Answers1

1

You can solve this by restructuring your project, or by using callbacks.

Restructure approach:

  • consider merging the two scripts into one
  • consider creating a third script, that contains everything that the other two scripts need, but doesn't require either of them - then just import the new script in both original scripts.

Callback approach:

scriptone.py

import scripttwo
def test():
    print "Hello World"

scripttwo.test(test)

scripttwo.py

def test(callback):
    print "Hello World"
    callback()

now, scripttwo doesn't really need to see functions from scriptone - you pass the function it needs to call as an argument (the callback).

Another way to work around the circular imports, would be to use function variables, it would look something like this:

scriptone.py

import scripttwo
def test():
    print "Hello World"

scripttwo.scriptone_test = test
scripttwo.test()

scripttwo.py

scriptone_test = None
def test():
    print "Hello World"
    scriptone_test()

This is very similar to the callback approach, except you don't really need to change function - instead you need to perform a "setup" of sorts, before you make any calls to any of the scripttwo functions.

Paulius
  • 5,790
  • 7
  • 42
  • 47