为了账号安全,请及时绑定邮箱和手机立即绑定

python中如何挂载自己写的在子目录下的模块?

python中如何挂载自己写的在子目录下的模块?

慕容3067478 2019-02-26 01:21:08
main.py layout_format = {} for filename in os.listdir('mod'): if filename.endswith('py'): for line in open(os.path.join('mod',filename)): eval(line) print(layout_format) exit() mod/md.py markdown_template = "# %(title)s\r\nAuthor: %(author)s\r\nCreate Date: %(created_time)s\r\nLast Update: %(updated_time)s\r\n\r\n%(content)s\r\n" def markdown(self): return markdown_template % self layout_format['md'] = markdown 然而执行出错。。。我想问下,一般python中怎么挂载自己写的子模块呢?
查看完整描述

3 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

layout_format = {}

for filename in os.listdir('mod'):
    if filename.endswith('py'):
        exec(open(os.path.join('mod',filename)).read())

print(layout_format)
exit()

原来这样就可以了。。。。JavaScript的eval!=python的eval,对应的是exec

查看完整回答
反对 回复 2019-03-04
?
四季花海

TA贡献1811条经验 获得超5个赞

import sys,os
sys.path.append(os.path.abspath('.'))
查看完整回答
反对 回复 2019-03-04
  • 3 回答
  • 0 关注
  • 649 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信