Make application location-independent
I have a setup where I might have to run applications in different subdirectories. Let's say instead of having
/flash/main.py with libraries in /flash/lib, I want to place it in
/flash/dir1/main.py with libraries in /flash/dir1/lib etc.
If I just move an application from the root to a subdirectory, it won't find imports from the lib folder as /flash/lib is in sys.path, but /flash/dir1/lib obviously is not.
How do I make an application location-independent? More specifically, how do I achieve that imports are found independent of the main.py location? Of course, if I know "dir1" I can append the /flash/dir1/lib path to sys.path, but how do I find out the directory of main.py? Also, there is still /flash/lib in the path which might lead to ambiguities if other library versions exist.
Actually what I would need is a relative path import like "import lib/mylibrary", but I have found no documentation on that for micropython. Any suggestions?
I've asked internally and It does not seem like we have a good way to get the equivalent of __FILE__ in micropython. Short of adding ALL of your applications ./lib subfolders to sys.path in boot.py prior to running/importing anything else.
robert-hh last edited by
This post is deleted!
@paul-thornton any idea yet?
I havent had a chance to try this in micropython yet. but in standard python a trick like:
os.path.dirname(os.path.abspath(file)) but im pretty sure that micropython is missing some of the cruicial parts of the os lib to make that work.
Im quite curious about this myself now. Leave it with me and when I get a chance ill see what I can come up with.