8
votes

How can I import a dotted name file in python?

I don't mean a relative path, but a filename starting or containing a dot "."

For example: '.test.py' is the filename.

import .test will search for a test package with a py module in a parent package.

1
Don’t name a module like that. - poke
@poke agreed. Just wondering if it is possible - Persijn
Btw. the general import .test already fails because import something will always make something available as a variable name; and .test is not a valid name. - poke

1 Answers

13
votes

The situation is tricky, because dots mean subpackage structure to python. Never the less it is still kinda possible with imp:

import imp
my_module = imp.load_source('my_module', '.test.py')

note: this is unorthodox and you are recommend to simply rename your modules instead.