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

 那0年_277 发布于 2022-11-26 04:15

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中怎么挂载自己写的子模块呢?

2 个回答
  • layout_format = {}
    
    for filename in os.listdir('mod'):
        if filename.endswith('py'):
            exec(open(os.path.join('mod',filename)).read())
    
    print(layout_format)
    exit()


    2022-11-26 04:24 回答
  • 直接 import 就好了啊。

    import mod.md

    和 import 其他模块一样的。


    2022-11-26 04:24 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有