Why is 'import simplejson' failing in Python 2.7.3 code, but not in the interpreter? [duplicate]

StackOverflow https://stackoverflow.com/questions/14221360

  •  14-01-2022
  •  | 
  •  

سؤال

There are two instances running uwgsi and nginx servers. Each hosts a Flask application. Both are running on a Python 2.7.3 path. One of the servers throws an ImportError for the "import simplejson" statement. The interpreter on both servers will accept this import statement without complaint.

Here's the source of application A:

  1 from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, views                                                                                                   
  2 import sys
  3 print sys.version
  4 print sys.path
  5 
  6 import os
  7 import functools
  8 import urllib,urllib2
  9 import simplejson
 10 from datetime import datetime, timedelta

And the source of application B:

  1 from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, views                                                                                                   
  2 
  3 import sys
  4 print sys.version          
  5 print sys.path             
  6 
  7 import simplejson  
  8 
  9 import functools

Here's the sys.version and sys.path log output of server A:

2.7.3 (default, Aug  1 2012, 05:25:23) 
[GCC 4.6.3]
['/srv/www/A/env/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',     
'/srv/www/A/env/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
'/srv/www/A/env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', 
'/srv/www/A/env/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg', 
'/srv/www/A/env/lib/python2.7', 
'/srv/www/A/20120910/src', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/srv/www/A/env/local/lib/python2.7/site-packages', 
'/srv/www/A/env/lib/python2.7/site-packages']
WSGI app 0 (mountpoint='notimportant.com|') ready in 1 seconds on interpreter 0x1b20420 pid: 11069

Here's the sys.version and sys.path log output of server B:

2.7.3 (default, Aug  1 2012, 05:25:23) 
[GCC 4.6.3]
['/srv/www/B/env/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', 
'/srv/www/B/env/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg', 
'/srv/www/B/env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', 
'/srv/www/B/env/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg', 
'/srv/www/B/env/lib/python2.7', 
'/srv/www/B/20130105/src', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/srv/www/B/env/local/lib/python2.7/site-packages', 
'/srv/www/B/env/lib/python2.7/site-packages']

Traceback (most recent call last):
  File "/srv/www/B/20130105/src/B.py", line 7, in <module>
    import simplejson
ImportError: No module named simplejson
unable to load app 0 (mountpoint='notimportant.com|') (callable not found or import error)

Any constructive ideas will be appreciated.

هل كانت مفيدة؟

المحلول

simplejson is an external library; it is bundled with Python 2.6 and up as the json module.

Just use import json instead of import simplejson, or install simplejson on server B too.

If you replace your simplejson import with json, remember to fix any references in the file. Alternatively, import it like this:

try:
    import simplejson
except ImportError:
    import json as simplejson

and it'll use the stdlib json module, renamed, and all your references to simplejson will continue to work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top