unresolved import in eclipse pydev

2019-01-25 13:46发布

问题:

I just properly installed the google gdata library for python (the script actually runs fine). I'm working with pydev in eclipse. I imported a module but the import command remains curly red underlined as you can see on the screenshot.

I added the following und ProjectName->Properties, but it does not work (although the path is definitly correct):

What do I have to do such that the import is resovled correctly by eclipse?

回答1:

If you have import statement like this: import gdata.spreadsheet.service then you need to make sure that on PYTHONPATH there is a directory that contains gdata subdirectory (and gdata should have spreadsheet subdirectory with service.py module file).

In your case: if your gdata directory is in site-packages directory on python distribution, then you need to make sure that site-packages is on PYTHONPATH (not site-packages/gdata/spreadsheet that you were trying to put there).

Look at http://docs.python.org/2/tutorial/modules.html in "6.4 Packages" section for example and reference.

In PyDev you can modify the contents of PYTHONPATH in two places:

  • Window -> PyDev -> Interpreter - Python
  • In the window you used

I would suggest you to first check that site-packages directory is in your Window -> PyDev -> Interpreter - Python settings -> System PYTHONPATH. It should be there by default, so if you didn't change it, it should be present there (and probably that's why your application works!).

Now, from some reasons, PyDev often has problems with "refreshing" info about available libraries after adding a new library to site-packages (and it uses that info for example to check if it should put error marker on import statement).

There are a few ways to force the refresh of this info. The most reliable for me is just removing python interpreter by means of: Window -> PyDev -> Interpreter - Python -> Remove and then adding it back in the same view. Then the site-packages directory is rescanned and PyDev sees the updated set of libraries, so the error markers should disappear.

Side note: To be honest, so far I haven't ever need to use this External Libraries view. Having site-packages in Interpreter settings is fine enought for 99% of cases because there is where Python external libraries should be put.