Import a module from a specific file path (was: PyWart: "Python's import statement and the history of external dependencies")
ben+python at benfinney.id.au
Sat Nov 22 01:37:55 CET 2014
Chris Angelico <rosuav at gmail.com> writes:
> In other words, what you want is:
> # today's method, import based on search path
> import sys
> # new explicit path method
> import '/usr/local/lib/python3.5/lib-dynload/math.cpython-35m.so'
I don't think I'd ever want to specify an absolute file path for the
module. But it would make my Python life immeasurably better if I could
specify *relative* file paths for importing a module.
import '../foo/bar/beans.py' as beans
> Can you tell me, please, how this is going to work for *any* system
> other than the one it was built on?
Allowing relative paths makes this portable, so long as the
application's relative tree structure is maintained.
> There are a VERY VERY few cases where you really do want to import a
> module from a specific file path. For those situations, there are
> already techniques you can use. Why change the behaviour of import
* I write a program, ‘fooprog’, using the hypothetical “import a module
at a specified file path” feature::
import './bar/beans.py' as beans
* The program ‘fooprog’ and the module ‘beans.py’ are in a sensible
* Running the program ‘fooprog’ invokes Python (via its standard
shebang), which invokes the file ‘fooprog’ as the ‘__main__’ module
*with no package awareness*.
Or, equivalently, ‘python foo/fooprog’ skips the shebang step.
* Regardless of package considerations, specifying ‘./bar/beans.py’
locates the module ‘beans’ for import, just fine.
That example will work in the presence of this “import from a named file
But it will fail with the standard Python import feature today. How
would you expect this example to change so it will work with current
Solutions usually seem to entail contortions of cluttering the import
block by discovering the current path, and fussing around with
‘sys.path’, before finally doing the import::
program_dir = os.path.dirname(__file__)
parent_dir = os.path.dirname(program_dir)
if not parent_dir in sys.path:
import foo.bar.beans as beans
Maybe you can suggest a better portable method to do this today in
Importantly, once you've come up with how to do it today in Python: Do
you really consider that superior to simply specifying a filesystem path
for a file containing the module?
\ “I'd take the awe of understanding over the awe of ignorance |
`\ any day.” —Douglas Adams |
More information about the Python-list