Search code examples
pythoneclipsepydev

Module calling in Pydev plugin in Eclipse


I have Pydev installed in Eclipse and everything works fine. I can import other modules installed through "pip install xxx" without any warnings or errors.

However, I got such a warning when I created two files to test:

test/HelloWorld.py

def hello():
    print('Hello World')

test/HelloWorldTest.py

from HelloWorld import hello

def test():
    hello()

I can run this "HelloWorldTest" without problem, which correctly prints "Hello World". However, in my PyDev editor, it always warns that:

hello Found at: test.HelloWorldTest

from HelloWorld import hello

―――――――――――――――――――――――――――――――――――――――――
Unresolved import: hello

Why does it show that 'hello' is unresolved import, which is highlighted in red? However, I can still run it with correct result.

Very strange!

Edited: enter image description here


Solution

  • Maybe your IDE is pointing to a different Python interpreter.

    I do not use Eclipse but it happens with PyCharm as well. In PyCharm I can select a different Python interpreter for the code editor and a different one for running. Normally I would not select different interpreter for the same project and confuse myself but it is possible for edge cases.

    I think similar thing is happening with your Eclipse IDE. Please double check your settings and make sure the code editor is pointing to the right Python interpreter.

    If you are working on multiple Python projects, it is highly recommended to use some sort of Python Virtual Environment to avoid dependency conflicts. PyEnv (https://github.com/yyuu/pyenv) is a good one if you are on Linux or Mac.

    Im pretty sure the path for the Python interpreter is not set properly on your IDE.

    Answer updated

    Why does your folder icons look so different? Are they inside a zip folder or something?

    It works perfectly fine on my machine. I do not see any errors.

    enter image description here

    Please check my settings.

    Window > Preferences > type python interpreter in the search box. Try removing the interpreter and manually add again.

    enter image description here

    Right click on the project folder > Properties > type interpreter in the search box.

    enter image description here

    Hopefully this solves your issue.

    Python Virtual Environment

    As you are working on many different projects, it is highly recommended to use a Python Virtual Environment if you haven't already. PyEnv (https://github.com/yyuu/pyenv) is a good one if you are on Linux or Mac. Or you can use the default VirtualEnv with VirtualEnvWrapper if you are on Windows.

    Quick note on Coding Style

    It has nothing to do with your question but I think its worth mentioning.

    I highly recommend you to enable PEP8 feature on your IDE because it helps you to make your project and code much more consistent.

    It is recommended to have module names in lowercase. ref = https://www.python.org/dev/peps/pep-0008/#package-and-module-names