在python中用点名导入 [英] Import with dot name in python
本文介绍了在python中用点名导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在python中导入带点名的文件?
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 "."
例如:'.test.py'
是文件名.
import .test
将在父包中搜索带有 py
模块的 test
包.
import .test
will search for a test
package with a py
module in a parent package.
推荐答案
情况很棘手,因为点对 python 来说意味着子包结构.无论如何,使用 imp
仍然有可能:
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中用点名导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文