我同意有类似的问题,但没有一个符合我的目的。
我有一个 python 脚本,没有 .py 扩展名。
我既不能更改文件名也不能添加符号链接。文件名很重要。
我需要将上述文件导入到另一个python脚本中
我尝试过以下方法
>>> imp.load_source('test','.')
<module 'test' from '.'>
and
>>> importlib.import_module('test','.')
<module 'test' from '.'>
模块所在位置test
is just
print 'hello world'
我的要求是导入语句的工作方式就像导入文件一样test.py
,即打印hello world
进口时。
有没有办法“运行”使用 imp 或 imortlib 导入的模块?
我想补充一点,我正在谈论的是control
文件在自动测试项目 https://github.com/autotest,如果重要的话。
您可以使用imp.load_source http://docs.python.org/2/library/imp.html#imp.load_source
>>> import imp
>>> mod = imp.load_source("test", "test")
hello world
>>> mod.a
1
abc:
print "hello world"
a = 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)