python:强制相对导入从脚本文件中搜索 [英] python: forcing relative imports to search from script file
问题描述
这是我的目录结构:
scripts/
bpydata/
bpymodules/
myCustomScripts/
lib/
__init__.py
customLib.py
plugin/
__init__.py
myParser.py
__init__.py
runner.py
...
my_import_script.py
...
我开始使用my_import_script,它将导入myCustomScripts包。
I start with my_import_script, which will import the myCustomScripts package.
从那里,我导入runner,它将导入myParser。
From there, I import runner, which will import myParser.
myParser需要在customLib中定义的内容,因此它将使用相对导入来获取它
myParser requires something that is defined in customLib, so it will use relative import to get it as such
<。c $ c>来自.lib导入customLib
但它不起作用因为我开始使用my_import_script所以它将搜索其父级目录而不是搜索myParser的父目录。
But it doesn't work because I started with my_import_script so it will search its parent directory rather than search myParser's parent directory.
当myParser想要.lib时,我怎样才能确保我从myParser的目录而不是my_import_script的目录中加强一个目录? / p>
When myParser wants .lib, how can I make sure that I am stepping up one directory from myParser's directory rather than my_import_script's directory?
推荐答案
要从父包导入,请使用 ..
。如果我没有误解你,这应该有效:
To import from a parent package use ..
. If I'm not misunderstanding you, this should work:
from ..lib import customLib
请参阅 http ://docs.python.org/tutorial/modules.html 。
这篇关于python:强制相对导入从脚本文件中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!