16

I'm doing my VERY first project using python/django/eclipse/pydev following this guide

http://docs.djangoproject.com/en/dev/intro/tutorial01/

My only addition is the use of Eclipse/pydev.

I'm getting many errors related to "Unresolved imports". I can remove the errors using "remove error markers" and my site runs perfect (I can browse it) but I want to get rid definitively of this problem since errors pop up again after I removed them.

Any ideas?

EDIT

Using Ubuntu 9.1

StackOverflower
  • 5,463
  • 13
  • 58
  • 89

9 Answers9

16

Check your pythonpath. You need to include the parent directory of django, usually Lib/site-packages.

Adam
  • 43,763
  • 16
  • 104
  • 144
  • 1
    Thanks Adam, I've added to PYTHONPATH /usr/lib/pymodules/python2.6/django/ (libs seems to be there) but no luck :-( – StackOverflower Jun 06 '10 at 15:21
  • @Timmy Is /usr/lib/modules/python2.6/ in PYTHONPATH? – Adam Jun 06 '10 at 15:41
  • I don't have that path. I have /usr/lib/python2.6 – StackOverflower Jun 06 '10 at 15:50
  • 7
    add /usr/lib/pymodules/python2.6 into your PYTHONPATH, you want the directory that contains django to be in there – Adam Jun 06 '10 at 17:01
  • The latest comment from Adam solved my problem. You must have both site-packages and site-packages/django in your PYTHONPATH. (I have django in site-packages) – simao Oct 25 '10 at 21:37
  • 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:24
  • @Adam is correct. In my particular case, I have configured PyDev to use a Python interpreter pointed to the bin/python of a virtualenv I set up specifically for this purpose. I then have a completely separate directory for my workspace. I was encountering similar problems until I went into my intepreter options (Window > Preferences > Interpreter - Python) and under the bottom pane (System PYTHONPATH) for my selected Python Interpreter configuration I added the (Django) project root (i.e. inside the Eclipse project root) and refreshed my project. This worked nicely. – Aaron Newton Jan 28 '12 at 06:39
  • On top of all the above, I had to close and re-open my project for the errors to go away! – arun Jul 22 '13 at 17:51
6

I'm on Mac OS X, but all I had to do was to add:

/Library/Python/2.6/site-packages

to my System PYTHONPATH (Found in: Preferences > Pydev > Interpreter - Python)

The equivalent for Ubuntu 10.04 would be:

/usr/lib/pymodules/python2.6

I'm guessing that the equivalent for Ubuntu 9.x could be:

/usr/lib/python2.6/site-packages

If it isn't and you're tired of looking, just upgrade to Ubuntu 10.04 and you should be fine.

Erik B
  • 40,889
  • 25
  • 119
  • 135
  • 3
    Awesome! I just helped myself! I was just about to upvote this answer and realized that I couldn't because it was my own. :) – Erik B Feb 12 '11 at 20:19
5

I was able to get rid of the import errors executing this cmd

sudo ln -s /usr/lib/pymodules/ /usr/lib/python2.6/pymodules

I was lucky, testing different things I could resolve it but I'm not sure why I have to do this and how I could avoid doing it.

mwolfe02
  • 23,787
  • 9
  • 91
  • 161
StackOverflower
  • 5,463
  • 13
  • 58
  • 89
3

Even if Lib/site-packages is added to the PYTHONPATH, this problem may happen when using modules from egg packages installed using easy_install. The problem with those, and that might actually depend on the easy_install version, is that by default they don't get installed directly inside Lib/site-packages but rather under a folder containing the full package name and having the .egg suffix. For example: Lib/site-packages/django_celery-3.0.23-py2.7.egg

Each module coming from packages installed as above needs an individual entry in PYTHONPATH. If the packages are installed after PyDev has been installed on the system, the system PYTHONPATH needs to be updated in PyDev. That is done automatically by going to Window -> Preferences -> PyDev -> Select your intepreter -> in the python interpreters panel remove and re-add your current python interpreter (usually, this should be the python executable from the system). Then a PyDev restart (File -> Restart) should solve the "Unresolved import" errors.

Florin Dumitrescu
  • 8,182
  • 4
  • 33
  • 29
3

"Unresolved imports" occur when Eclipse/Pydev does not know what you want to import. Check your Pydev settings in the Eclipse preferences > Interpreter Python. Your site-packages folder and things you want to import should be there in the Pythonpath.

Also see http://pydev.org/manual_101_interpreter.html

webjunkie
  • 6,891
  • 7
  • 46
  • 43
2

In my case of the modules were dependent on setuptools-14.3.1 , which was causing all these problems. After installation of setuptools-14.3.1 rest of the modules automatically resolved.

Shakti
  • 2,013
  • 8
  • 27
  • 40
  • 1
    _Thanks @Shakti, after 1 week of unfortunate research this resolved the problem_ This solve the issue with `unresolved reference` for modules in PyCharm CE and PyCharm Pro . Updating `setuptools` proved to be succesful. Hope this will help further users that have this issue. – Pierre Chevallier Sep 26 '17 at 11:24
2

If you are facing problem of unused imports then I must say use Eclipse as a IDE as it is providing functionality to remove unused imports by pressing keys ctr+shift+O. Or In Eclipse there is plugin also available which is doing the same thing automatically when you save your code.You can get that plugin from eclipse plugin site easily and free of cost.

Rupeshit
  • 1,476
  • 1
  • 14
  • 23
1

I was having a single import error when working in PyDEV in eclipse.

I was importing it like

from xyz_module import abc

So I clicked "Ctl + 1" and "select Unresolved import error" in eclipse and it created a class file in a python file.

It turned out that I had created an application called "xyz_module" and a "xyz_module.py" file (with the same name that is) and this was causing an import error. I changed the python file to a new name and this resolved the error.

ankitk
  • 458
  • 3
  • 12
  • Yes. I named my file nltk.py which caused the conflict. Good answer. –  Sep 28 '12 at 23:18
1

I had the same error and none of the answers worked for me as there was no PYTHONPATH options under Window -> Preferences -> PyDev -> Python.

Instead I added PYTHONPATH setting in the menu:

Project -> PyDev-PYTHONPATH -> External Libraries -> Add Source folder

I added the path of site-packages like

/home/Documents/hcx/venv/lib/python3.5/site-packages

Now PyDev stopped complaining.

bhaskarc
  • 9,269
  • 10
  • 65
  • 86