简体   繁体   中英

Python3 import a yet non installed package

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

Don't import scriptB until you need it. Eg, 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM