英文:
how to resolve ModuleNotFoundError python error
问题
I have translated the content you provided:
exampleProject
--> models
----> database
-------> __init__.py
-------> example1.py
-------> example2.py
--> services
----> __init__.py
----> service1.py
----> service2.py
我已经翻译了您提供的内容:
示例项目
--> 模型
----> 数据库
-------> __init__.py
-------> example1.py
-------> example2.py
--> 服务
----> __init__.py
----> service1.py
----> service2.py
请注意,我只翻译了您提供的文本内容,没有包括其他内容。
英文:
exampleProject
--> models
----> database
-------> __init__.py
-------> example1.py
-------> example1.py
--> services
----> __init__.py
----> service1.py
----> service1.py
i have created this table structure for my project and the problem is when i am importing example1.py file in service1.py i am getting ModuleNotFoundError and same error getting when importing service2.py file in example2.py
i have tried sys.path.append method. but still getting same error, files imported in init.py file.
i want to run this project in production, i had read somewhere its not good practice to use sys.path.append method in production.
this is normal structure of folder but if i want to increase folders and file and import file from various folder how can i do that?
can somebody help me in this, how to resolve this problem.
thank you.
答案1
得分: 0
首先,dags文件夹被添加到sys路径中。因此,dags文件夹内的任何内容都可以在Airflow环境中导入。
我将以您提供的示例为例。所以让我们考虑以下文件夹结构位于dags文件夹内,例如/home/airflow/dags
exampleProject
--> models
----> database
-------> __init__.py
-------> example1.py
--> services
----> __init__.py
----> service1.py
假设exampleProject/models/database/example1.py
看起来像这样
def example1_func():
print("In example1_func")
现在,让我们在services/service1.py
内导入上述函数,下面是我们将如何导入example1_func
的方式
from exampleProject.models.database.example1 import example1_func
def service1_func():
example1_func()
print('inside service1_func')
现在,我们可以通过类似的方法在任务中导入service1_func
并调用它
from exampleProject.services.service1 import service1_func
# 创建dag
...
# 在任务中调用函数
service1_func()
...
英文:
first of all, the dags folder gets added to the sys path. Hence anything inside dags folder can be imported in Airflow env.
I will take an example that you have given. So let's consider the below folder structure resides inside dags folder eg /home/airflow/dags
exampleProject
--> models
----> database
-------> __init__.py
-------> example1.py
--> services
----> __init__.py
----> service1.py
Lets assume that exampleProject/models/database/example1.py
looks something like this
def example1_func():
print("In example1_func")
Now, let's import the above function inside the services/service1.py
, so below is how we will import the example1_func
from exampleProject.models.database.example1 import example1_func
def service1_func():
example1_func()
print('inside service1_func')
Now we can call the service1_func
in our tasks by importing in similar method
from exampleProject.services.service1 import service1_func
# creating dag
...
# calling the function in a task
service1_func()
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论