简体   繁体   中英

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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