Reputation: 15623
There are many questions about "unresolved import" for Python with PyDev in Eclipse. NB Linux Mint 18.3 OS, Python3, Eclipse "2019-06", PyDev 7.3.0...
I just hoped someone might be able to confirm my understanding on this:
Directory structure:
project directory: PyDevExp
directory: mygame
file: draw.py
file: game.py
game.py looks like this:
# game.py
# import the draw module
import draw
def play_game():
...
def main():
result = play_game()
draw.draw_game()
if __name__ == '__main__':
main()
When I run this at the command line:
python3 game.py
... it runs fine, no complaints. But when I view it in Eclipse there's a horrid red mark next to the import line. I can only get rid of it by adding directory "mygame" to the project's PYTHONPATH.
Surely this is ridiculous? Why can't PyDev "see" other files in the same directory without this having to be explicitly configured? Or am I doing something wrong?
later
The thing even runs OK in Eclipse! So for now I've changed the notification level from "error" to "info" for Windows --> Prefs --> PyDev --> Editor --> Code Analysis --> Imports tab --> "Import not found".
Unsatisfactory!
Upvotes: 4
Views: 1346
Reputation: 11
Right-click on your project (PyDevExp) and select PyDev. You will probably see an option to remove the folder from PYTHONPATH. It looks to me as though project folders are now added to PYTHONPATH by default. In which case you can get rid of the error marker by changing "import draw" to "import mygame.draw". But you might prefer just to put up with the spurious error.
Upvotes: 1
Reputation: 1714
To avoid the error on Eclipse, just do right click on mygame directory, and go to PyDev -> Set as source folder (add to PYTHONPATH)
, as you can see in the image.
Then, you can import your files in the right way in order to test it in Eclipse.
Upvotes: 0