Python3从同级目录导入模块/包 [英] Python3 Importing module/package from sibling directories

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

问题描述

这是我的代码目录结构:

Here is my code directory structure:

/root
-/proj1
--/module1.py
--/__init__.py
--/sub_proj1
---/module2.py
---/__init__.py
-/proj2
--/module3.py

如果要将代码从module2.py导入到module3.py中,请尝试以下导入语句:

If I want to import code from module2.py into module3.py I tried the following import statement:

from .proj1.sub_proj1 import *

但是我收到导入错误.您需要我修理什么吗?我不确定相对导入的作用是什么.

but I am getting import error. Is thee anything I need to fix? I am not sure what I am doing wrong with relative imports.

推荐答案

类似于此问题:

从同级文件夹中的Python导入模块

您的根目录和两个项目文件夹都需要一个__init__.py

You need an __init__.py in your root and both project folders

这也应该是您的导入语句:

Also this should be your import statement:

from ..proj1.sub_proj1 import *

这篇关于Python3从同级目录导入模块/包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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