这个问题已经在这里有了答案: > ImportError: No Module named simplejson 5个
有两个实例在运行uwgsi和nginx服务器.每个托管一个Flask应用程序.两者都在Python 2.7.3路径上运行.其中一台服务器为“ import simplejson”语句引发ImportError.两台服务器上的解释器都将接受此导入语句,而不会产生投诉.
这是应用程序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
以及应用程序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
这是服务器A的sys.version和sys.path日志输出:
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
这是服务器B的sys.version和sys.path日志输出:
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)
任何建设性的想法将不胜感激.
解决方法:
simplejson是一个外部库;它与Python 2.6和json
module捆绑在一起.
只需使用import json而不是import simplejson,或在服务器B上也安装simplejson.
如果将您的simplejson导入替换为json,请记住要修复文件中的所有引用.另外,也可以这样导入:
try:
import simplejson
except ImportError:
import json as simplejson
并且它将使用重命名的stdlib json模块,并且您对simplejson的所有引用将继续起作用.