简体   繁体   English

Python - 从文件系统中水平文件夹的相对导入

[英]Python - Relative import from folder horizontal in file system

Diagram of file system:文件系统示意图:

└ main folder
   ├ folder 1
   │  └ module1
   └ folder 2
      └ module2

I want to import module2 from module1 .我想从module1导入module2 However, its horizontal to module1 in the file tree.但是,它与文件树中的module1水平。 Is there an easy way to go about this without having to import sys , os , and etc.?有没有一种简单的方法来 go 关于这个而无需import sysos等?

Thank you.谢谢你。

PS: I am using Windows 10. PS:我使用的是 Windows 10。

I do not know where is your main script to execute, however, if you want to execute module1 like我不知道你的主脚本在哪里执行,但是,如果你想像这样执行module1

python module1.py
# or
python /PATH/TO/module1.py

you can simply do it like:你可以简单地这样做:

PYTHONPATH=/PATH/TO/folder_2:$PYTHONPATH python module1.py

/PATH/TO/folder_2 can be relative to where you execute python . /PATH/TO/folder_2可以相对于您执行python的位置。

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

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