Python Brother/Sister AWS CodeBuild 文件夹中的相对导入
Python Relative Import within Brother/Sister Folder for AWS CodeBuild
我正在使用 Python 2.7 在 AWS CodeBuild 上构建,但我相信这更像是一个通用的 python 导入问题。我有一个目录设置,如下所示。我在测试文件夹中 运行 test.py。作为此测试的一部分,我想导入依赖项 mainScript.py。但是,我似乎无法获得正确的相关依赖关系,并且在测试文件夹中导入 mainScript 时遇到了很多困难。下面是我的目录文件夹的布局
main
src
mainScript.py
test
test.py
例如,如果我的目录设置类似于
main
test
test.py
mainScript.py
我可以通过以下方式完成导入
from mainScript import *
我已经确认这有效。但是,我喜欢它在它自己的 src 文件夹中。我已经尝试了所有这些这些是我尝试过的以下相对路径尝试
from ..src/mainScript import * #SyntaxError: invalid syntax
from ..src.mainScript import * #ValueError: attempted relative import beyond top-level package
from mainScript import * #ModuleNotFoundError: No module named 'mainScript'
from src.mainScript import * #ModuleNotFoundError: No module named 'src'
from src/mainScript import * #SyntaxError: invalid syntax
我已经苦苦挣扎了一段时间,但我找不到关于访问 brother/sister 文件夹脚本的问题。预先感谢您的帮助。
Python 将包含 __init__.py
文件的目录视为包。将您的结构更新为以下内容应该可以解决问题:
__init__.py
src
__init__.py
mainScript.py
test
__init__.py
test.py
现在,从 test.py 开始,您可以做到 from ..src import *
。关于init.py的更多细节,可以看这里:What is __init__.py for?
除了添加 init.py 文件。结果是我必须 运行 python 在我的命令中使用 -m 参数,这是在 Python 2.4 中添加的。
Python 2.4 adds the command line switch -m to allow modules to be
located using the Python module namespace for execution as scripts.
The motivating examples were standard library modules such as pdb and
profile, and the Python 2.4 implementation is fine for this limited
purpose.
所以从顶层目录启动的命令是:
python -m test.test
这似乎有效并获得了正确的命名空间。然后在您的 test.py 文件中,您将按以下方式导入 mainScript
from src.mainScript import *
我正在使用 Python 2.7 在 AWS CodeBuild 上构建,但我相信这更像是一个通用的 python 导入问题。我有一个目录设置,如下所示。我在测试文件夹中 运行 test.py。作为此测试的一部分,我想导入依赖项 mainScript.py。但是,我似乎无法获得正确的相关依赖关系,并且在测试文件夹中导入 mainScript 时遇到了很多困难。下面是我的目录文件夹的布局
main
src
mainScript.py
test
test.py
例如,如果我的目录设置类似于
main
test
test.py
mainScript.py
我可以通过以下方式完成导入
from mainScript import *
我已经确认这有效。但是,我喜欢它在它自己的 src 文件夹中。我已经尝试了所有这些这些是我尝试过的以下相对路径尝试
from ..src/mainScript import * #SyntaxError: invalid syntax
from ..src.mainScript import * #ValueError: attempted relative import beyond top-level package
from mainScript import * #ModuleNotFoundError: No module named 'mainScript'
from src.mainScript import * #ModuleNotFoundError: No module named 'src'
from src/mainScript import * #SyntaxError: invalid syntax
我已经苦苦挣扎了一段时间,但我找不到关于访问 brother/sister 文件夹脚本的问题。预先感谢您的帮助。
Python 将包含 __init__.py
文件的目录视为包。将您的结构更新为以下内容应该可以解决问题:
__init__.py
src
__init__.py
mainScript.py
test
__init__.py
test.py
现在,从 test.py 开始,您可以做到 from ..src import *
。关于init.py的更多细节,可以看这里:What is __init__.py for?
除了添加 init.py 文件。结果是我必须 运行 python 在我的命令中使用 -m 参数,这是在 Python 2.4 中添加的。
Python 2.4 adds the command line switch -m to allow modules to be located using the Python module namespace for execution as scripts. The motivating examples were standard library modules such as pdb and profile, and the Python 2.4 implementation is fine for this limited purpose.
所以从顶层目录启动的命令是:
python -m test.test
这似乎有效并获得了正确的命名空间。然后在您的 test.py 文件中,您将按以下方式导入 mainScript
from src.mainScript import *