在python中从子文件夹层次结构导入 [英] Importing from sub-folder hierarchy in python

查看:189
本文介绍了在python中从子文件夹层次结构导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从test_file层次结构导入指定的模块

i am trying to import specified modules from test_file hierarchy

类似:

test_case1.py
test_subsuite_2
test_sub_2.1.1.py
test_suite2

是否可以在此层次结构上进行运行导入

is it possible to do a run import on this hierarchy

/project/main.py
/project/test_files

test_files文件夹层次结构是这样的:

test_files folder hierarchy is like this :

test_files
    test_suite1
        test_case1.py
        test_subsuite_1
            test_sub1_1.py
            test_sub1_2.py
        test_subsuite_2
            test_subsuite_2_1
                test_sub_2.1.1.py
            test_sub2_1.py
            test_sub3_2.py

    test_suite2
        test_case2.py
        test_subsuite2_1
            test_sub21_1.py
            test_sub21_2.py
        test_subsuite2_2
            test_sub22_1.py
            test_sub23_2.py

推荐答案

在所有文件夹中创建一个名为__init__.py的空文件.然后,您可以使用.作为文件夹分隔符进行导入. 此处的文档.

Create an empty file called __init__.py in all you folders. Then you can import using . as a folder separator. Documentation here.

这篇关于在python中从子文件夹层次结构导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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