首页 >社区问答列表 >python中如何挂载自己写的在子目录下的模块?

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

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


  • 面对疾风吧
  • 面对疾风吧    2016-11-09 16:56:431楼

    layout_format = {}


    for filename in os.listdir('mod'):

        if filename.endswith('py'):

            exec(open(os.path.join('mod',filename)).read())


    print(layout_format)

    exit()


    +0添加回复

  • 回复