2

The following lines in Eclipse + PyDev with Python 3.4 work, but it is highlighted as Undefined variable from import: dump error:

import json
with open('C:\\test', 'w') as outfile:
    json.dump([1, 2, 3], outfile)

Here is a snapshot of the Eclipse editor:

Error on json.dump

How do I get rid of that error?

EDIT

The output of:

import json
with open('C:\\test', 'w') as outfile:
    json.dump([1, 2, 3], outfile)

print(json.__file__)

Is:

C:\Python34\lib\json\__init__.py

Here is the list of attributes that Eclipse shows:

List of attributes of json according to Eclipse

stenci
  • 8,290
  • 14
  • 64
  • 104
  • It'd be useful to see the import statement too. Even better, a complete but concise standalone program that shows the error. – BobHy May 19 '14 at 01:16
  • @BobHy: I edited the question. Now you can see a working program with the Eclipse error – stenci May 19 '14 at 13:34
  • 1
    Do you have a `json.py` in the same folder as your current module? If so, you're probably importing it instead of the standard library module, and the errors are due to it not having the stuff you expect. – Blckknght May 19 '14 at 14:42
  • @Blckknght: No files are called json or json.py. I just did `pip install peewee`, then i removed the interpreter from the project and added it back. Now Eclipse knows about `peewee`, but it still doesn't know about `json` – stenci May 19 '14 at 16:00
  • @stenci: Hmm, that error doesn't make much sense then. I don't know much about PyDev, but if it gives you an interactive interpreter, try doing `import json; print(json.__file__)` to double check that it's actually importing the module from the standard library rather than something else. – Blckknght May 19 '14 at 19:53
  • @Blckknght: I edited the post adding the info you requested and some. – stenci May 19 '14 at 20:04
  • Well, that path looks fine to me. I have no idea what's going on. Hopefully somebody with more knowledge about PyDev can help you out! – Blckknght May 19 '14 at 22:51
  • Here's an earlier report of a similiar problem: http://stackoverflow.com/questions/2143549/undefined-variable-from-import-when-using-wxpython-in-pydev . It recommends removing / respecifying the interpreter and restarting Eclipse. I'd remove the global interpreter setting as well as project setting, then restart Eclipse, then respecify the interpreter. I guess what's going on here is that the editor ends up with a shorter search path than the actual Python interpreter. – BobHy May 21 '14 at 17:18

4 Answers4

2

problem in line 313 in file ../lib/json/__init__.py

...
if s.startswith(u'\ufeff'):
...

change this line to

if s.startswith('\ufeff'):

or wait for the new version

Aleksey
  • 41
  • 6
0

I was not able to get Eclipse to work, but I was able to get rid of the error (and of similar error/warnings): select the highlighted word, press ctrl+1 and select the line containing @UndefinedVariable to add a comment at the end of the row that will tell Eclipse to ignore that error on that line.

I use the same technique to ignore other errors with other modules, and it helps keeping the project clean.

See here for more details.

stenci
  • 8,290
  • 14
  • 64
  • 104
0

I had the same problem, but I had a file called json.py. Renaming my file to jsonTest.py from Eclipse left behind a json.pyc. Deleting that file did not help. Doing a Project->Clean did not help. Finally, I created a new workspace and things now work as expected. This was with Eclipse Kepler Release 2.

SoloPilot
  • 1,484
  • 20
  • 17
0

Adding json to the list of Forced Builtins of your Python Interpreter in PyDev Preferences also fix this issue.

MestreLion
  • 12,698
  • 8
  • 66
  • 57