开发者

Unresolved import: models

开发者 https://www.devze.com 2023-01-02 05:22 出处:网络
I\'m doing my VERY first project using python/django/eclipse/pydev following this guide http://docs.djangoproject.com/en/dev/intro/tutorial01/

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 sinc开发者_运维百科e errors pop up again after I removed them.

Any ideas?

EDIT

Using Ubuntu 9.1


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


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.


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.


"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


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号