2

I'll try to be as clear as possible:

This is my project structure:

PYDEV PROJECT FOLDER
     src folder
         package
            module1
            module2

1 - In module1 I say:

import module2

and eclipse marks an unresolved import error. But if I hit run or try to run the script via command line (outside Eclipse), everything goes well. The module is used properly.

2 - Now, if I write:

import package.module2

Eclipse is happy, the error dissapears and the program runs from within Eclipse but if I try to run it via console outside Eclipse I get "No module named package.module2".

What is wrong?

I tried this solution: Unresolved import errors yet successful import still occurs

But I can't move everything up. Things have to be inside that package. Other that adding a @UnresovedImport to have eclipse stop complaining, what should I do?

Community
  • 1
  • 1
A R
  • 500
  • 2
  • 7
  • 20
  • How do you run module 1? Is it the top-level script? Eclipse clearly thinks `package` is on your python module path, but when you are running module 1, it is not. – Martijn Pieters Jun 15 '13 at 21:31
  • Everything is inside package and package is inside src – A R Jun 15 '13 at 21:49
  • Yes, I know. That is not what I asked you. :-) – Martijn Pieters Jun 15 '13 at 21:50
  • 1
    Sorry, I don't understand the question then. What is a top level script? When you ask how I run the script: I click on Run as> Python run. I run the Module1 file. – A R Jun 15 '13 at 21:59
  • how do you run your python code? What file do you open with Python? – Martijn Pieters Jun 15 '13 at 22:00
  • Hmmm, I'm totally lost here. Eclipse is suggesting to "move import to global scope". I don't understand what's wrong with eclipse. – A R Jun 15 '13 at 22:24
  • You are mixing and matching python module search paths; and I am trying to determine how to help you with that, but you are not answering my question. – Martijn Pieters Jun 15 '13 at 22:25
  • Sorry again, I go directly into the package folder and call python Module1.py – A R Jun 15 '13 at 22:39
  • 2
    Exactly. Don't do that. That way `package` is not seen by Python at all; instead `module1` and `module2` are directly on the Python module search path. Eclipse, on the other hand, thinks `package` is on the module search path. Use a script that imports `package.module1` instead. – Martijn Pieters Jun 15 '13 at 22:41
  • uhhh I begin to understand. Python starts searching for packages from the directory where the script being passed to the interpreter is. Alright, so normally, should my main program be in src or in a package inside src? – A R Jun 15 '13 at 23:11
  • OK. Thank you very much for your patience. I got it now. The main script has to be in a folder above everything being called from it. – A R Jun 16 '13 at 00:08

2 Answers2

2

Solution is as follows: -

I removed these errors by going to:

Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore

And for import not found errors: -

It may also be, Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Imports -> Import not found -> Ignore

We forcibly remove these errors because python interpreter do not have a solution for this.

Let me know if you need further info on this.

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
PD Mohanty
  • 64
  • 4
0

I have the same file structure you describe, and I had the same issue. (I'm using Eclipse Oxygen 4.7.0 with Pydev.) The problem was that my PROJECT FOLDER was in the Python Path that Eclipse was aware of, but my "src" folder was not. The solution that worked for me was to tell Eclipse about the src folder, as described here: https://stackoverflow.com/a/43453932/5886424

Bill Olsen
  • 51
  • 5