Python导入子目录 [英] Python importing subdirectories

查看:160
本文介绍了Python导入子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从子目录导入所有文件,所以我想我可以在该子目录中编写 __ init __。py 来导入文件。但是,当我这样做时,它似乎没有导入任何东西。

I'm trying to import all files from a sub-directory, so I figured I could write __init__.py in that sub-directory to import the files. However, when I do this it does not seem to import anything.

文件结构:

prog.py
module/
    __init__.py
    code.py

代码 prog.py 传递

代码 __ init __。py 导入代码

代码 code.py print('hello')

当我运行prog.py时没有任何反应。为什么不打印 hello ,是否有更好的方法可以轻松导入子目录中的所有内容?

When I run prog.py nothing happens. Why does it not print hello, and is there a better way to easily import everything from a sub-directory?

推荐答案

将此放入 prog.py

import module

Python只会加载导入的包或模块。

Python will only load packages or modules that are imported.

为了使它成功,你可能也需要jcollado的答案。

To make it work, you probably need jcollado's answer as well.

这篇关于Python导入子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆