Unresolved Import Issues with PyDev and Eclipse
Asked Answered
E

12

111

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.

Especial answered 7/1, 2011 at 23:55 Comment(2)
Make sure the case (capitalisation) is correct for all the necessary directories in your PYTHONPATH.Reikoreilly
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 moduleDerron
S
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.

Seventeen answered 8/1, 2011 at 2:26 Comment(17)
If you set the pydev properties correctly, you don't need to mess with sys.pathEpithelium
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. ( https://mcmap.net/q/196135/-unresolved-import-models ) ( #4631877 )Oehsen
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.Countercheck
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 :)Reikoreilly
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.Senior
@Countercheck 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 youMasochism
soulBit's solution also fixes internal or custom packages that are changed, i.e. restructuring the folder structure of the packages.Homiletics
@CamJackson you saved me a lot of trouble! +1Antinomy
If things still aren't working: remove your python interpreter and just recreate it then File->Restart. That worked for me.Sorbitol
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.Goodnight
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: #14202004Dotti
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!Locale
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.Jeb
Remember to keep an __init__.py in your root folder, your /src, and /lib.Symmetrical
@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.Pathway
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.Dail
The solution given by @BitByte_Bake is much easier and should be used first.Ancestor
T
57

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.

Terminal answered 30/3, 2015 at 16:7 Comment(5)
No need to restart Eclipse, just close and re-open file. It also worked on Windows with Eclipse Luna, Thanks!Tame
I had to restart Eclipse two times to make the warnings disappear. Thank youWofford
Works with Python 3.7 as well. This should be the solution to this question.Ancestor
I tried this and now I can't start my DEV server. How do I replace the file?Necaise
@BitByty-Bake what is the root cause?Girlish
M
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!

Mazuma answered 1/6, 2011 at 11:54 Comment(1)
Yes, closed and reopened the project to get rid of that disturbing ImportError messagesSeena
L
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.

Lubber answered 4/12, 2012 at 18:38 Comment(1)
This works fine using liclipse with odoo 10. Thanks!Implacental
C
13

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

Cordova answered 25/3, 2014 at 6:44 Comment(2)
Well, this was the one that fixed my issue.Complete
No need to restart just close and reopen the file. Fixed it for me!Dail
S
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.

Switcheroo answered 13/6, 2014 at 2:43 Comment(0)
L
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".

Lambdacism answered 5/7, 2013 at 7:4 Comment(0)
S
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.

Selfeducated answered 3/9, 2013 at 14:41 Comment(0)
A
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.

Autarky answered 5/10, 2014 at 2:2 Comment(0)
K
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.

Kernel answered 19/12, 2019 at 22:21 Comment(0)
R
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.

Rictus answered 29/1, 2023 at 1:3 Comment(0)
A
-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")    
Ariadne answered 25/1, 2018 at 19:48 Comment(1)
Please consider to separate file names and codes. And you also need to fix some indents.Reiners

© 2022 - 2024 — McMap. All rights reserved.