some bits flipped
some bits flipped

Reputation: 3092

pydev - ide complains about module importing itself

NOTE: I haven't accepted an answer because my real question is why this is being marked as a compile error. I've voted up @TorelTwiddler answer because he gave a workaround, but I'd like to understand the problem.


I have a simple module that contains self-test code. To facilitate use in an interactive session, the self-test code reloads itself (the module).

This works OK in both PyDev (RunAs) and running in an interactive window (after manually amending sys.path and importing); but the PyDev IDE still gives a 'compile' / red-X error on the line

import Mod1  

The reason I import Mod1 from itself is that the name must be resolvable in whatever context is running Test(), in order for the reload to succeed. For instance, if you import MyMain from a fresh interactive window, the call to reload would fail without the import, since the name Mod1 is not known.

Is there any way I can fix this?? Mod1 is one part of a much larger project and having it consistently marked as not-compiling makes development more difficult...

Module with the problem:

# Mod1.py
def Test():
    """ 
    run the self-test, but first force a reload of the module under test (this mod)
    """        
    import Mod1      # *****'COMPILE' ERROR HERE******  
    import imp
    Mod1 = imp.reload(Mod1)

    TestImpl()

def TestImpl():
    """
    self test here
    since I reload above, I can change this code and re-run from an interactive window
    this has to be a seperate function for the reload in Test to have an effect on this code
    """ 
    print(input("enter"))

Extra bootstrap module only used in PyDev (so I can 'run-as')

# MyMain.py
import Mod1
Mod1.Test()

PyDev/Project PYTHONPATH (appears correct, this folder is at the root of my workspace). Since it does actually run in PyDev OK, it is definitely correct?

/MyDirectory

Thanks!

Upvotes: 2

Views: 271

Answers (4)

Mathias Helminger
Mathias Helminger

Reputation: 11

I went with the #@UnresolvedImport annotation that tells PySide to ignore it. In my case there is no real workaround since when using pickle (im using it indirectly through yaml) the correct import path depends on how a module was loaded. If you want to un-pickle an object from outside a module it will not work if pickling was done from the inside. The self-import gives you that possibility.

Upvotes: 1

TorelTwiddler
TorelTwiddler

Reputation: 6156

Adding a separate answer from the discussion on Snaxib's answer, for formatting.

Have one module that doesn't change, TestMyStuff and another that you update with your changes, TheTest.

#TestMyStuff.py
def go():
    import TheTest
    reload(TheTest)
    TheTest.runTest()

and

#TheTest.py
def runTest():
    #run my tests and change things often here.
    print(input("enter"))

Now, from the iteractive shell, you should be able to run TestMyStuff.go(), which will have TheTest reload every time it's run.

Upvotes: 2

TorelTwiddler
TorelTwiddler

Reputation: 6156

I would probably go with Snaxib's answer, however if you'd prefer to keep the current format, you can have Eclipse ignore the error by adding #@UnresoledImport at the end of your line (hit Ctrl-1 to auto fill it in).

Upvotes: 2

Snaxib
Snaxib

Reputation: 1690

If you reload a module within itself you create an infinite loop, because you would reload the module only to reload the module again, and again etc...

In your MyMain.py you might be able to do this:

import Mod1
reload(Mod1)

Mod1.Test()

I guess without further code samples its hard to gauge exactly WHY you must reload the module at all, but if you have to, that should work (without doing it in the module)

Upvotes: 2

Related Questions