Peaceful
Peaceful

Reputation: 5450

No module named 'json' after installing simplejson

I am working in Ubuntu 14.04 and I have multiple versions of Python on my machine (they include python2.7 and python3.4). Few days back, I installed simplejson on my system. I don't remember how I did that but I guess it must be similar to pip install simplejson. However, now a strange problem has started appearing when I try installing any python package. For example, just now I tried installing Tkinter using sudo pip3.4 install Tkinter and it throws the following error:

Traceback (most recent call last):
  File "/usr/local/bin/pip3.4", line 9, in <module>
    load_entry_point('pip==1.5.4', 'console_scripts', 'pip3.4')()
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 351, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2363, in load_entry_point
    return ep.load()
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2088, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 61, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/lib/python3/dist-packages/pip/vcs/subversion.py", line 4, in <module>
    from pip.index import Link
  File "/usr/lib/python3/dist-packages/pip/index.py", line 15, in <module>
    from pip.wheel import Wheel, wheel_ext
  File "/usr/lib/python3/dist-packages/pip/wheel.py", line 25, in <module>
    from distlib.scripts import ScriptMaker
  File "/usr/share/python-wheels/distlib-0.1.8-py2.py3-none-any.whl/distlib/scripts.py", line 15, in <module>
  File "/usr/share/python-wheels/distlib-0.1.8-py2.py3-none-any.whl/distlib/resources.py", line 20, in <module>
  File "/usr/share/python-wheels/distlib-0.1.8-py2.py3-none-any.whl/distlib/util.py", line 11, in <module>
ImportError: No module named 'json'

Sometimes I can fix this if the error tells me that in one of the files I have:

import json

which I simply convert to

import simplejson as json

I tried uninstalling simplejson:

sudo pip uninstall simplejson

but it gives me the same error: json not found.

Can anybody please help me fix this so that I would happily be able to install python packages? Thanks in advance.

Upvotes: 5

Views: 24461

Answers (2)

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21453

Note: I do not have a definitive answer but will offer a series of steps you can try:

The first thing is see if you can import json from the usual python interpreter:

import json
print(json.__file__) #this would be important to know if it works

If that does work (as well as commenting what json.__file__ is) you would then want to try to use pip from the interpreter.

If you can't import json normally:

This is not surprising, I did not expect pip to be looking in a non-standard place for modules. You will want to figure out where the json package should be located on your computer, you can do this by importing another module from the standard library and looking at it's __file__:

>>> import fractions
>>> fractions.__file__
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/fractions.py'

This will obviously be different for you but I'd expect there to be a json folder in the same folder as fractions.py

if you can't import fractions or queue or datetime etc.

If you can't import anything from the standard library you will probably want to just reinstall python.

If the json folder is there and contains an __init__.py

Use the rename function of your file browser to make sure there are no weird special characters, but other then that I'm not sure, if you can import fractions.py but not a package from the same folder that would imply there is something very wrong with the import mechanics of your python version.

If the json folder is not with the rest of the standard library

It is possible that your python distribution has a different structure then I'd expect, it at least can't hurt to take a look for it.

You can search for the json folder amongst your various python files using the find command, not really sure how it works but just another thing to try. If you do find it with the __init__.py, encode.py, decode.py, scanner.py, and tool.py (at least those are the ones in my version) you'll probably want to figure out how it got there, but maybe just move it to the same folder as the rest of the standard library.

If you can't find the json package or you find it and it is corrupted

Well then you will need to replace it! Don't worry, this isn't too hard, just grab a source release of python from the site and extract the json package from it, once it is uncompressed the json folder should be in the Lib folder. Simply copy/move it to the rest of the standard library and you should be good to go!


I hope this helps you debug what is going on, This covers all the scenarios I could imagine happening and I would be interested in which one fixed your issue (or what you were able to figure out so I can come up with more options)

Upvotes: 6

maxking
maxking

Reputation: 1

I am guessing that you install it using either pip install simplejson to download from PyPI or using apt-get install python-simplejson to download from ubuntu repositories.

It is possible that you downlaoded the library for the Python2 if you used any of the commands above and it won't be available for Python3 (which pip3.4 will use). Can you try these commands and help debug yourself?

$ python -c "import simplejson"

$ python3.4 -c "import simplejson"

This would tell you which version of the python did you install simplejson for last time (my guess in python2). If the 2nd command errors out with ImportError try:

$ pip3.4 install simplejson

and then install your libraries.

Upvotes: -1

Related Questions