1

I think there is a bug with respect to how PyDev (version 4.6) recognizes intra-package imports when selecting Grammar 3.x for the project preferences. I have a project like this:

foobar
    mypack
        __init__.py
        mod1.py
        mod2.py

mod2.py simply says

from mod1 import fun1

mod1.py simply says

def fun1():
    print("Hey we are in fun1 in mod1")

If the project Python project preferences are set to use Grammar 3.0-3.5, with a Python 3.4 interpreter, and I open up mod2.py the line from mod1 import fun1 is highlighted with an error Unresolved import: fun1. If I change the Python project preferences to use Grammar 2.7, close the file mod2.py and reopen it, the error disappears. Just by changing the grammar back and forth, and closing/reopening the file, I can make the error appear/disappear.

So it seems that setting the Grammar to 3.x in PyDev causes intra-package imports to be incorrectly flagged as having an import error.

Any suggestions?

Irv
  • 540
  • 4
  • 13

2 Answers2

2

PyDev is doing right... on Python 3, relative imports must be written as:

from .mod1 import fun1

If the import doesn't start with a dot, it'll consider it an absolute import (and will properly show the error for you as with that absolute path the imported file cannot be resolved).

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • The issue is then if I have `mod2.py` with a `main` in it (for testing purposes) and I try to run mod2.py from a Run Configuration in PyDev, I will get the error "Parent module '' not loaded, cannot perform relative import". I can't see how to do what is stated here: [link](http://stackoverflow.com/questions/16981921/relative-imports-in-python-3) in PyDev, i.e., use the "-m" option from within PyDev. – Irv May 05 '16 at 14:18
  • Humm, in the main module you currently have to write absolute imports (because PyDev does not provide a way to execute the module with the '-m' option). – Fabio Zadrozny May 05 '16 at 14:28
  • In my project, I have multiple packages, subpackages, etc., many have a main() module so I can run them for testing purposes. I guess it relates to the discussion here [link](http://stackoverflow.com/questions/31904307/how-to-run-a-module-inside-a-package-using-relative-imports) and the PyDev request embedded there. Sigh... – Irv May 05 '16 at 14:59
0

So my real issue was getting PyDev to not report errors about the imports, and to be able to debug modules buried in packages that have a main() in there for debugging. The solution for me was to use relative imports (as said in Fabio's answer), and then to do the following for debugging purposes. Let's say I want to run a module pack1.subpack2.subpack3.subpack4.modtodebug using PyDev, that has relative imports, and has a main() function. At the top level of my project, I have a module debugmain.py that reads

from pack1.subpack2.subpack3.subpack4.modtodebug import main as debugmain

if __name__ == '__main__':
    debugmain()

I then have one run configuration for debugmain.py and each time I want to debug a different module, I just have to edit the code in debugmain.py to point to that module.

I hope this helps someone else with this kind of problem.

Irv
  • 540
  • 4
  • 13