I wanted to know if you can use the import statement in python from a directory that is not your local directory if that directory is not a package? Also, do all the directories on your system path have to be packages? If you add a relative path to your system path, what is it relative to?
Share
you can alter sys.path in order to achieve all the results you are asking for.
Yes you can. To add a directory that is not your local directory:
If you need to load the module from an arbitrary path in the filesystem without adding it to sys.path you can use also imp.load_module
do all the directories on your system path have to be packages? No, they do not
If you add a relative path to your system path, what is it relative to?
to the directory containing the script that was used to invoke the Python interpreter.
I suggest, however, to set it in this way:
or from the path of the script:
both the examples work also from interactive shells. Both examples ensure the relative path is what you meant, regardless of the OS
see also this post for more details on relative paths in python