Unresolved Import Issues with PyDev and Eclipse
I am very new to PyDev and Python, though I have used Eclipse for Java plenty. I am trying to work through some of the Dive Into Python examples and this feels like an extremely trivial problem that's just becoming exceedingly annoying. I am using Ubuntu Linux 10.04.
I want to be able to use the file odbchelper.py, which is located in the directory /Desktop/Python_Tutorials/diveintopython/py
Here is my example.py file that I'm working on in my PyDev/Eclipse project:
import sys
sys.path.append("~/Desktop/Python_Tutorials/diveintopython/py")
This works fine, but then I want the next line of my code to be:
import odbchelper
and this causes an unresolved import error every time. I have added __init__.py
files to just about every directory possible and it doesn't help anything. I've tried adding __init__.py
files one at a time to the various levels of directories between the project location and the odbchelper.py file, and I've also tried adding the __init__.py
files to all of the directories in between simultaneously. Neither works.
All I want to do is have a project somewhere in some other directory, say /Desktop/MyStuff/Project
, in which I have example.py ... and then from example.py I want to import odbchelper.py from /Desktop/Python_Tutorials/diveintopython/py/
Every message board response I can find just saying to use the sys.path.append()
function to add this directory to my path, and then import it ... but that is precisely what I am doing in my code and it's not working.
I have also tried the Ctrl-1 trick to suppress the error message, but the program is still not functioning correctly. I get an error, ImportError: No module named odbchelper
. So it's clearly not getting the path added, or there is some problem that all of my many permutations of adding __init__.py
files has missed.
It's very frustrating that something this simple... calling things from some file that exists somewhere else on my machine... requires this much effort.
In the properties for your pydev project, there's a pane called "PyDev - PYTHONPATH", with a sub-pane called "External Libraries". You can add source folders (any folder that has an __init__.py
) to the path using that pane. Your project code will then be able to import modules from those source folders.
I am using eclipse kepler 4.3, PyDev 3.9.2 and on my ubuntu 14.04 I encountered with the same problem. I tried and spent hours, with all the above most of the options but in vain. Then I tried the following which was great:
- Select Project-> RightClick-> PyDev-> Remove PyDev Project Config
- file-> restart
And I was using Python 2.7 as an interpreter, although it doesn’t effect, I think.
I just upgraded a WXWindows project to Python 2.7 and had no end of trouble getting Pydev to recognize the new interpreter. Did the same thing as above configuring the interpreter, made a fresh install of Eclipse and Pydev. Thought some part of python must have been corrupt, so I re-installed everything again. Arghh! Closed and reopened the project, and restarted Eclipse between all of these changes.
FINALLY noticed you can 'remove the PyDev project config' by right clicking on project. Then it can be made into a PyDev project again, now it is good as gold!
I fixed my pythonpath and everything was dandy when I imported stuff through the console, but all these previously unresolved imports were still marked as errors in my code, no matter how many times I restarted eclipse or refreshed/cleaned the project.
I right clicked the project->Pydev->Remove error markers and it got rid of that problem. Don't worry, if your code contains actual errors they will be re-marked.
project-->properties-->pydev-pythonpath-->external libraries --> add source folder, add the PARENT FOLDER of the project. Then restart eclipse.