Python full path import not working across branches in package tree

和自甴很熟 提交于 2019-12-25 03:05:21

问题


I have the following project structure:

Project
  - GUI
    - ...Modules
  - Data
    - Database
      - ...Modules
    - Files
      - ...Modules
  - Utilities
    - ...Modules

And I am trying to do some imports over package borders, for example:

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?


回答1:


The fully specified import failed because the current working directory of python(or jython) was set to Project. you can:

add the parent directory of Project to your python lib

import sys
sys.path.append('/parent/of/project')

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.)



来源:https://stackoverflow.com/questions/11047086/python-full-path-import-not-working-across-branches-in-package-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!