Import with dot name in python

2019-01-23 08:18发布

问题:

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:

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.



标签: python import