简体   繁体   English

如何从与main.py脚本处于同一层次结构级别的文件夹中导入脚本?

[英]How to import script from a folder at the same hierarchy level at which the main.py script is?

This might seem like a dumb question I found the answer to my question but it wasn't something I was looking for. 我似乎找到了问题的答案,这似乎是一个愚蠢的问题,但这不是我要找的东西。 So here I have a folder structure with scripts as follows: 因此,这里有一个包含脚本的文件夹结构,如下所示:

project :
  -- main_scripts
     ---main.py
     ---test.py
  -- module_scripts
     __init__.py          # This method doesn;t work as to what I found.
     ---module1.py
     ---module2.py

I want to import the functions in the module1.py and module2.py in my main.py script but the only answers I found were based on sys.path.append or the PYTHON PATH ENVIRONMENT variable method. 我想在main.py脚本中导入module1.py和module2.py中的函数,但是我发现的唯一答案是基于sys.path.append或PYTHON PATH ENVIRONMENT变量方法。 But those aren't the methods I am looking for. 但这不是我要寻找的方法。 Is there any other way around to achieve this or these are the only possible options of method of module imports available in python 是否有其他方法可以实现这一目标,或者这些是python中可用的模块导入方法的唯一可能选择

__init__.py适用于子目录,并且对于相同的层次结构或父目录,您必须先导入sys模块,然后将目录追加到sys.path,所以答案是否定的。

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

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