0

This question probably has been answered before on this forum but I did not manage to find it.

So my problem is the following: Let's say I am working with two scripts:

#script 1

import script2
reload (script2)
from script2 import *

def thisisatest():
    print "test successfull"
    return ()

def main():
    realtest()
    return ()

main()

and:

#script 2

def realtest():
    thisisatest()
    return()

Now if I run script1 I get an error message saying that global name "thisisatest" is not defined. However a thisisatest()? call on python gives me the function help.

EDIT:

My question is: Is there is a way to proceed with many scripts while doing the import part (for all the scripts) in one script or is it impossible ?

Thanks in advance,

Enzoupi

Enzoupi
  • 541
  • 5
  • 10
  • 1
    possible duplicate of [Import Python Script Into Another?](http://stackoverflow.com/questions/15696461/import-python-script-into-another) – albert Jun 30 '15 at 09:43
  • 1
    That ``reload()`` call is not necessary. You almost never need to use ``reload``. – René Fleschenberg Jun 30 '15 at 10:06
  • 1
    `return` is not a function. Don't use `return ()` unless you want to return an empty tuple; just simply `return`. –  Jun 30 '15 at 10:11
  • thanks @Evert for the info. – Enzoupi Jun 30 '15 at 10:25
  • 1
    Actually, some more FYI: if a function doesn't return anything, *and* it ends normally (no early return), you don't even need a `return` statement: it is implicit in Python. So you can delete the last line in these functions. –  Jun 30 '15 at 10:27

1 Answers1

0

It is best to avoid circular dependencies completely. If module1 imports from module2, then module2 should not import from module1. If a function that is defined in module2 needs to use a function from module1, you can pass the function as an argument.

module1:

from module2 import otherfunc

def myfunc()
    print "myfunc"

def main()
    otherfunc(myfunc)

main()

module2:

def otherfunc(thefunc):
    print "otherfunc"
    thefunc()
René Fleschenberg
  • 2,418
  • 1
  • 15
  • 11