简体   繁体   English

无法使用 python 从其他目录导入模块?

[英]Not able to import module from other directory using python?

Please find my project structure attached as image below请在下面的图片中找到我的项目结构

在此处输入图像描述

I'm running jobs/my_job.py from project root directory ie from sample_project, when I'm running the jobs/my_job.py using python.\jobs\my_job.py getting the following error我正在从项目根目录运行 jobs/my_job.py,即从 sample_project,当我使用python.\jobs\my_job.py运行 jobs/my_job.py 时出现以下错误

Traceback (most recent call last):
  File ".\jobs\my_job.py", line 1, in <module>
    from dependencies.spark import test
ModuleNotFoundError: No module named 'dependencies'

This is the code I'm running这是我正在运行的代码

from dependencies.spark import test从 dependencies.spark 导入测试

def main():
    test()

Since I'm running the code from root directory, why I'm getting that error?由于我是从根目录运行代码,为什么会出现该错误?

please find the screen of whole请找到整个屏幕在此处输入图像描述

I think you should add dependencies folder to system paths, because the script you are trying to run locates in another directory.我认为您应该将 dependencies 文件夹添加到系统路径,因为您尝试运行的脚本位于另一个目录中。

import sys
sys.path.append('../dependencies')

from dependencies.spark import test
if __name__ == '__main__':
    test()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM