Python Forum
importing module from specific file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: importing module from specific file (/thread-3563.html)



importing module from specific file - Skaperen - Jun-03-2017

i wanted to import a module from a specific file that may be in a subdirectory maybe even many levels deep. the current directory will always be at some level above the module. the path will never have '/../' in it and will always be relative to the current directory. there will never be any symlinks involved. suggestions?


RE: importing module from specific file - metulburr - Jun-03-2017

from subdir.subdir2.subdr3 import my_deep_module



RE: importing module from specific file - Skaperen - Jun-03-2017

the path is in a string, not hard coded in the source. the depth is unknown until the string with the path is known.

i found this ... what do you think?

https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path


RE: importing module from specific file - Skaperen - Jun-03-2017

my goal is to expand on the apparent limitation that mod-wsgi only operates on the file named in the apache directive. you can't just enable a directory. so my idea is that my code will take PATH_INFO and use that to select a module, and invoke that in the same way, passing on the WSGI environment (with PATH_INFO truncated as needed). that way i can make a site with a number of different scripts. my code will restrict the path as needed for security (only stuff under the script's own directory can be invoked).


RE: importing module from specific file - Skaperen - Jun-13-2017

it turns out you can enable a directory for mod-wsgi execution.  you just have to append the directory names with a '/' in the apache config directive.