Python从模块子目录导入所有内容 [英] Python importing everything from module subdirectory

查看:61
本文介绍了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 的代码: pass

__ 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天全站免登陆