简体   繁体   English

如何从另一个文件夹导入python文件

[英]how to import python file from another folder

My folders hierarchy looks like this我的文件夹层次结构看起来像这样

├───folder1 
│   ├───pythonFile1.py
│   └───pythonFile2.py
├───app.py

and i'm trying to import python file from another folder like this我正在尝试从这样的另一个文件夹导入 python 文件
(the import is from pythonFile2 to app.py) (导入是从 pythonFile2 到 app.py)

from folder1 import pythonFile2

but i get this error ➡ ModuleNotFoundError: No module named 'pythonFile1'但我收到此错误 ➡ ModuleNotFoundError: No module named 'pythonFile1'

Can you tell me how to import python file from another folder?你能告诉我如何从另一个文件夹导入python文件吗?

Thank you !!谢谢 !!

I think the problem is in the file path you want to import, either you put the correct relative or absolute path and it works fine look at the example on the picture我认为问题出在您要导入的文件路径中,无论您输入正确的相对路径还是绝对路径,它都可以正常工作,请查看图片上的示例

execution exemple执行实例

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

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