0

First of all, here's the look of my project :

/
 scriptA.py 
 scriptB.py
 /redist/
        /mod1/
             /setup.py

Here's a quick detail of what's going on :

scriptA.py

import scriptB
def install_MyMod():
    #some code that install mod1 in the python3 /dist-packages

def other_Stuff():
    return scriptB.stuff()

OR, the problem is that scriptB needs mod1 to run :

scriptB.py

import mod1 as mm
def stuff():
    return mm.some_Function()

The issue is that whenever I launch scriptA, I got an error saying that scriptB cannot import mod1, which is logicial since my first script is supposed to install it, and then call the other script which will use it.

Is there a way to avoid this error ?

Thanks

Zabka
  • 104
  • 1
  • 10

1 Answers1

3

Don't import scriptB until you need it. E.g., put the import statement inside the other_Stuff() function:

def install_MyMod():
    #some code that install mod1 in the python3 /dist-packages

def other_Stuff():
    import scriptB
    return scriptB.stuff()
  • Thanks, didn't knew it was possibe ! So the import will only be local ? – Zabka Jun 09 '15 at 09:36
  • The import namespace is local, but if you import `scriptB` again in the same file somewhere else, Python will do that [efficiently](http://stackoverflow.com/questions/296036/does-python-optimize-modules-when-they-are-imported-multiple-times) (it keeps a list of loaded modules). –  Jun 09 '15 at 10:36