110

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.

jchanger
  • 739
  • 10
  • 29
ely
  • 74,674
  • 34
  • 147
  • 228
  • Make sure the case (capitalisation) is correct for all the necessary directories in your PYTHONPATH. – Cam Jackson Nov 18 '11 at 01:51
  • I know this has already been answered, but I was getting unresolved import for the `grp` module. I just added it to the `builtins` list in Preferences>PyDev>Interpreters>Python>Forced Builtins and still get autocompletion for that module – user1040495 Feb 08 '14 at 19:47

12 Answers12

137

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
David German
  • 1,854
  • 1
  • 12
  • 6
  • 5
    If you set the pydev properties correctly, you don't need to mess with `sys.path` – Velociraptors Jan 08 '11 at 05:14
  • 6
    For other searchers, I wanted to add my experience -Windows 7, Python 2.7.1, Eclipse 3.6.2, PyDev, Pyschopg (For Python 2.7 amd64). The install of Psychopg went to C:\Python27\Lib\site-packages\psycopg2 ( under the Python 2.7 installation ). I first referenced that folder and all child folders with __init__.py, but found I had to reference the site_packages folder as well. ( http://stackoverflow.com/questions/2983088/unresolved-import-models ) ( http://stackoverflow.com/questions/4631377/unresolved-import-issues-with-pydev-and-eclipse ) – Jeff Maass Apr 22 '11 at 13:25
  • 91
    One other point to note - Eclipse may need to be restarted for this to work. This only seems to work via "File -> Restart" and not by closing and reopening manually. – soulBit May 11 '11 at 14:51
  • 19
    **Important note:** I've found that the interpreter Auto Config adds `C:\Python27\lib\site-packages`, but on my computer, the lib folder is capitalised: `C:\Python27\Lib\site-packages`. Replacing the lower-case entry with an upper case one, and then File->Restart fixed the problem for me. Hope that helps someone else out there :) – Cam Jackson Nov 18 '11 at 01:50
  • 2
    **Important note #2:** I have a capitalized lib folder but it worked for me just by removing the current python interpreter then creating a new one (button 'Auto Config') and a File->Restart. – Cédric Guillemette Jan 11 '12 at 13:55
  • 10
    @soulBit I wish I could thumbs that comment up 20 times, I've spent a good hour trying to figure out why my import was still unresolved. Thank you – Matt Dodge Feb 22 '12 at 03:48
  • 1
    soulBit's solution also fixes internal or custom packages that are changed, i.e. restructuring the folder structure of the packages. – James Mertz Jul 09 '13 at 21:21
  • @CamJackson you saved me a lot of trouble! +1 – wagnerpeer Sep 26 '13 at 11:20
  • If things still aren't working: remove your python interpreter and just recreate it then File->Restart. That worked for me. – kmosley Oct 14 '13 at 20:42
  • Great thread. I tried all this multiple times but kept getting the same error. In my case, the error was that I had created a module with the same name at two different places, both in the PYTHONPATH. I realized soon and deleted the _.py_ but not the _.pyc_ . Hours later and all the steps here behind, I removed the **.pyc** to fix the problem. Hope it helps someone later. – Spade Jan 22 '14 at 18:45
  • 3
    Not sure if this will help others, but make sure that if you're code is doing something like `import api.models`, you want to add the folder that _contains_ the `api` directory, not the `api` directory itself. The system will look in the path for a subfolder called `api` when importing. Got here from: http://stackoverflow.com/questions/14201504/unresolved-import-in-eclipse-pydev – Sammaron Apr 09 '15 at 17:30
  • For Windows 8: Even after File->Restart PyDev (3.5) would not import an external library. But after a system reboot, on restarting Eclipse 4.3.2, it asked if I wanted to import the libraries for my pythonpath. Success! – intotecho May 25 '15 at 00:54
  • 1
    This didn't work for me with Google Protocol Buffers. I added "google" to forced built-ins, performed a File > Restart and everything started working. – Dragos Aug 01 '16 at 07:14
  • Remember to keep an `__init__.py` in your root folder, your `/src`, and `/lib`. – noumenal Aug 08 '16 at 13:42
  • @soulBit: please add that as an answer! Also reference Martin's answer (*project->Pydev->"Remove error markers"*). Eclipse cannot be relied on to trigger rebuilding automatically. – smci Nov 01 '17 at 05:33
  • My code would run fine but eclipse kept saying it could not resolve some relative imports. Well, there was a SPACE in the folder name of the project. Replaced it with an underscore and the problem's gone. – Jerther Nov 22 '17 at 19:54
  • The solution given by @BitByte_Bake is much easier and should be used first. – SPRBRN Dec 20 '18 at 15:05
56

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.

BitByte_Bake
  • 939
  • 1
  • 9
  • 12
42

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!

StackXchangeT
  • 43
  • 1
  • 10
Paul
  • 421
  • 4
  • 2
18

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.

Martin
  • 321
  • 1
  • 3
  • 7
12

project-->properties-->pydev-pythonpath-->external libraries --> add source folder, add the PARENT FOLDER of the project. Then restart eclipse.

Rashad
  • 11,057
  • 4
  • 45
  • 73
zhaokongsheng
  • 121
  • 1
  • 3
5

Here is what worked for me (sugested by soulBit):

1) Restart using restart from the file menu
2) Once it started again, manually close and open it.

This is the simplest solution ever and it completely removes the annoying thing.

Brana
  • 1,197
  • 3
  • 17
  • 38
1

There are two ways of solving this issue:

  • Delete the Python interpreter from "Python interpreters" and add it again.
  • Or just add the folder with the libraries in the interpreter you are using in your project, in my case I was using "bottle" and the folder I added was "c:\Python33\Lib\site-packages\bottle-0.11.6-py3.3.egg"

Now I don't see the error anymore, and the code completion feature works as well with "bottle".

PachinSV
  • 3,680
  • 2
  • 29
  • 41
1

I'm running Eclipse 4.2.0 (Juno) and PyDev 2.8.1, and ran into this problem with a lib installed to my site-packages path. According to this SO question:

Pydev and *.pyc Files

...there is an issue with PyDev and pyc files. In the case of the particular lib I tried to reference, all that is delivered is pyc files.

Here's what I did to address this:

  1. Install uncompyle2 from https://github.com/Mysterie/uncompyle2
  2. Run uncompyle2 against the *.pyc files in the site-packages lib. Example:

    uncompyle2 -r -o /tmp /path/to/site-packages/lib

  3. Rename the resulting *.pyc_dis files produced from uncompyle2 to *.py
  4. Move / copy these *.py files to the site-packages path
  5. In Eclipse, select File > Restart

The unresolved import error relating to .pyc files should now disappear.

Community
  • 1
  • 1
0

Following, in my opinion will solve the problem

  1. Adding the init.py to your "~/Desktop/Python_Tutorials/diveintopython/py" folder
  2. Go to Window --> Preferences --> PyDev --> Interpreters --> Python Interpreter to remove your Python Interpreter setting (reason being is because PyDev unable to auto refresh any updates made to any System PythonPath)
  3. Add in the Interpreter with the same details as before (this will refresh your Python Interpreter setting with updates made to your PythonPath)
  4. Finally since your "~/Desktop/Python_Tutorials/diveintopython/py" folder not a standard PythonPath, you will need to add it in. There are two ways to do it

a. As per what David German suggested. However this only applicable for the particular projects you are in b. Add in "~/Desktop/Python_Tutorials/diveintopython/py" into a new PythonPath under Window --> Preferences --> PyDev --> Interpreters --> Python Interpreter --> Libraries subtab --> NewFolder

Hope it helps.

0

I had some issues importing additional libraries, after trying to resolve the problem, by understanding PYTHONPATH, Interpreter, and Grammar I found that I did everything write but the problems continue. After that, I just add a new empty line in the files that had the import errors and saved them and the error was resolved.

0

None of the other answers worked for me. I could see the function in the imported file, pylint could find it, and all tests passed (and yes, the function was exercised). Nothing helped until I happened to run ls on the site.packages directory and saw both the module.py file AND a module.pyi file. The pyi file was empty, and I have no idea where it came from (I'm sure I fat-fingered something but no idea what), but I do know that if you have one it can't be empty.

I removed the file, restarted Eclipse and everything was back to normal.

ivanlan
  • 979
  • 6
  • 8
-1
KD.py

class A:
a=10;

KD2.py 
from com.jbk.KD import A;
class B:
  b=120;

aa=A();
print(aa.a)

THIS works perfectly file for me

Another example is

main.py
=======
from com.jbk.scenarios.objectcreation.settings import _init
from com.jbk.scenarios.objectcreation.subfile import stuff

_init();
stuff();

settings.py
==========
def _init():
print("kiran")


subfile.py
==========
def stuff():
print("asasas")    
Java By Kiran
  • 95
  • 1
  • 2
  • Please consider to separate file names and codes. And you also need to fix some indents. – dkato Jan 26 '18 at 04:57