Python3 import a yet non installed package

68 Views Asked by At

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

1

There are 1 best solutions below

2
On BEST ANSWER

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()