Import with point name in python

How to import dot file in python?

I do not mean the relative path, but the file name starting or containing a period "."

For example: '.test.py'is the file name.

import .testwill search for the package testwith the module pyin the parent package.

+4
source share
1 answer

The situation is complicated because dots mean the subpackage structure for python. However, this is still possible with imp:

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

Note: this is unorthodox, and you are advised to simply rename your modules.

+8
source

All Articles