This code indicates what I wish to do, i.e.
- Have a variable to hold the name of a file to import,
- Use
shutil.which()to look up the FOLDER it resides in (via$PATH), - Automatically get that added to
PYTHONPATHto make import find it.
name='numbas'
d=os.path.dirname(shutil.which(name+'.py'))
sys.path.append(d)
from name import *
Now import does not allow a name stored in a variable.
eval() on a string that contains the import statement is also "nonfunctional".
Any ideas on how to get this thing to actually work?
The reason for all this is that I have *.py scripts in a Util/ -folder - which are self-contained scripts AND import modules for other scripts to utilize.
If I move these around or copy them to a friend I wish to have the folder structure remain as is.
importlib.import_module.