[Python-Dev] Import by filename with import ? (original) (raw)

Guido van Rossum guido at python.org
Wed Jan 9 06:25:37 CET 2008


Sounds like a coincidence. Looks like it's using os.path.join(X, name

Feel free to add a check that the module name doesn't contain '/', '\' or '.'. (I think that silently accepting other non-identifier characters is fine, since it doesn't interfere with parsing either the module name or the filename.)

On Jan 8, 2008 9:09 PM, Christian Heimes <lists at cheimes.de> wrote:

Today I stumbled about an unknown and undocumented (?) feature. At least it's not documented in our docs. import can import a module by file name:

>>> open("/tmp/example.py", "w").write("test = 23\n") >>> mod = import("/tmp/example") >>> mod <module '/tmp/example' from '/tmp/example.py'> >>> mod.name '/tmp/example' >>> mod.file '/tmp/example.py' >>> mod.test 23 Is it just a coincidence? Is it a desired feature? Why isn't it documented? Christian


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list