Cannot use "from X import Y"
Issue #87 created 2010-09-07
You need two files, file_1.py and file_2.py, if one file imports the other and the other uses the import method of "from X import Y" then there will be an unexpected import error, an error that as you can imagine is absent from running the program normally.
First file:
#!/usr/bin/env python3
import file_2
Second file:
#!/usr/bin/env python3
from collections import OrderedDict
Here is the error I receive:
Traceback (most recent call last):
File "file_1.py", line 2, in
import file_2
File "file_2.py", line 2, in
from collections import OrderedDict
ImportError: cannot import name OrderedDict
但是也有人指出更新的版本如3.1不会出现这个问题,而且OrderedDict这个东西本来是2.7版本的python才有的特性。
后来改了import方式,但是部署的时候不会出现导入error,却在执行post,走到OrderedDict时error了!哎,最后才想到云上的python和django版本到底是多少?!查一下!
try:
from collections import OrderedDict as ordict
except ImportError:
try:
import ordereddict as ordict
except ImportError:
raise
def iniMonitorItemsByServerType(serverType):
try:
conn = connectToDataBase()
cur = conn.cursor(db.cursors.DictCursor)
selectSQL = """select * from config_server_type where server_type = %s """%(str(serverType))
count = cur.execute(selectSQL)
row = cur.fetchone()
for key in row.keys():
if row.get(key)==0:
row.pop(key)
# use OrderedDict to set index of iframes in the monitoring
monitorItems = ordict()
alist = ['Uptime','RPS', 'Resp_time','CPU','Mem','Disk','Network','Queue_length','Pool_size']
for key in alist:
if row.has_key(key):
monitorItems[key] = row.get(key)
return monitorItems
except Exception:
print "error in iniMonitorItemsByServerType"