Python全路径导入无法在包树中的各个分支上正常工作 [英] Python full path import not working across branches in package tree
问题描述
我具有以下项目结构:
Project
- GUI
- ...Modules
- Data
- Database
- ...Modules
- Files
- ...Modules
- Utilities
- ...Modules
我正在尝试在包装边界上进行一些导入,例如:
And I am trying to do some imports over package borders, for example:
我尝试导入Project.Utilities.Conversions
.如果我使用像import Project.Utilities.Conversions
这样的完全指定的导入,则此操作将失败,它将与import Utilities.Conversions
一起使用,即,我不能指定比其他部分更多的路径.但是,我想使用完全指定的路径,一个原因是eclipse中的pydev更喜欢它们(否则,它向我显示一个错误),第二个原因是我发现这样做令人困惑.
in the file(Module) Project.Database.dbdriver
I try to import Project.Utilities.Conversions
. If I use a fully specified import like import Project.Utilities.Conversions
this fails, it works with import Utilities.Conversions
, i.e. I can not specify more off the path than those part that differ. However I would like to use fully specified paths, one reason beeing that pydev in eclipse likes them better (otherwise it shows me an error), the second reason is I find it confusing not to do so.
I have stumbled over this but think it is wrong/not needed here How do I create a namespace package in Python?
问题:当在包装结构中交叉子支路时,如何使用完全指定的包含物?
Question: how can I use fully specified includes when crossing subbranches in a package structure?
推荐答案
由于python(或jython)的当前工作目录设置为Project,因此完全指定的导入失败.您可以:
The fully specified import failed because the current working directory of python(or jython) was set to Project. you can:
将Project的父目录添加到您的python lib
add the parent directory of Project to your python lib
import sys
sys.path.append('/parent/of/project')
或者只是在调试设置中将jython的工作目录更改为Project的父目录.(我不知道如何做,因为我不使用pydev.)
or just change the working directory of jython to the parent of Project in the debug settings.(I don't know how to do it because I don't use pydev.)
这篇关于Python全路径导入无法在包树中的各个分支上正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!