猿问

Flask, ImportError: No module named XXX

第一个flask项目,做一个简单的网址导航。部署项目后,浏览器访问报500错误。查看apache日志后,报错如下:
[TueJan0609:58:222015][error]helloworld
[TueJan0609:58:222015][error][client112.64.71.131]mod_wsgi(pid=31615):TargetWSGIscript'/var/www/qianshan/qianshan.wsgi'cannotbeloadedasPythonmodule.
[TueJan0609:58:222015][error][client112.64.71.131]mod_wsgi(pid=31615):ExceptionoccurredprocessingWSGIscript'/var/www/qianshan/qianshan.wsgi'.
[TueJan0609:58:222015][error][client112.64.71.131]Traceback(mostrecentcalllast):
[TueJan0609:58:222015][error][client112.64.71.131]File"/var/www/qianshan/qianshan.wsgi",line12,in
[TueJan0609:58:222015][error][client112.64.71.131]fromqianshanimportappasapplication
[TueJan0609:58:222015][error][client112.64.71.131]File"/var/www/qianshan/__init__.py",line4,in
[TueJan0609:58:222015][error][client112.64.71.131]importextras
[TueJan0609:58:222015][error][client112.64.71.131]ImportError:Nomodulenamedextras
项目结构
qianshan
├──config.ini
├──extraModules.py
├──extras.py
├──__init__.py
├──qianshan.wsgi
├──static
├──templates
├──test.py
└──venv
init.py代码
fromflaskimportFlask
fromflaskimportrender_template
importextraModules
importextras#如果没有这句无报错正常运行,只有一些静态资源没拉到,可能是其他问题
importConfigParser
importcodecs
importlogging
logging.basicConfig(filename='qianshan.log',level=logging.INFO)
logging.info('Started')
app=Flask(__name__)
logging.info('Appestablished')
@app.route("/")
defindex():
returnrender_template('index.html')
extras.py是实际我想要导入的模块,里面有两个我需要的类;extraModules是测试模块,importextraModules是成功的,apache日志的第一行也打出来了;他们的代码分别如下:
extras.py
#Filename:extras.py
classBlock:
defsetNo(self,no):
self.no=int(no)
defgetNo(self):
returnself.no
defsetName(self,name):
self.name=name
defgetName(self):
returnself.name
defsetPriority(self,priority):
self.priority=int(priority)
defgetPriority(self):
returnself.priority
defsetHotKeyAsc(self,hotKeyAsc):
self.hotKeyAsc=hotKeyAsc
defgetHotKeyAsc(self):
returnself.hotKeyAsc
defsetElement(self,equation):
s=equation.split(':')
if(s[0]=='no'):
self.setNo(s[1])
elif(s[0]=='name'):
self.setName(s[1])
elif(s[0]=='priority'):
self.setPriority(s[1])
elif(s[0]=='hot_key_asc'):
self.setHotKeyAsc(s[1])
classWebsite:
defsetNo(self,no):
self.no=int(no)
defgetNo(self):
returnself.no
defsetName(self,name):
self.name=name
defgetName(self):
returnself.name
defsetUrl(self,url):
self.url=url
defgetUrl(self):
returnself.url
defsetIcon(self,icon):
self.icon=icon
defgetIcon(self):
returnself.icon
defsetBlockNo(self,blockNo):
self.blockNo=int(blockNo)
defsetPriority(self,priority):
self.priority=int(priority)
defgetPriority(self):
returnself.priority
defsetHotKeyAsc(self,hotKeyAsc):
self.hotKeyAsc=hotKeyAsc
defgetHotKeyAsc(self):
returnself.hotKeyAsc
defsetElement(self,equation):
s=equation.split(':')
if(s[0]=='no'):
self.setNo(s[1])
elif(s[0]=='name'):
self.setName(s[1])
elif(s[0]=='url'):
self.setUrl(s[1])
elif(s[0]=='icon'):
self.setIcon(s[1])
elif(s[0]=='priority'):
self.setPriority(s[1])
elif(s[0]=='hot_key_asc'):
self.setHotKeyAsc(s[1])
if__name__=='__main__':
block=Block()
website=Website()
extraModules.py
#Filename:extraModules.py
print'helloworld'
其他背景信息:
项目部署在digitalocean的ubuntu12.x主机上,python版本2.7.3.
还请指导下,是不是extras.py有什么地方大意了,小弟新学python不久,请多敲打点拨
谢谢1L和2L前辈的指点啊!白天一直在上课,以下为1月7日晚更新,我把wsgi以及virtualhost的配置也发一下,wsgi中是有配置/var/www/qianshan/为sys.path的。
qianshan.wsgi
#!/usr/bin/python
activate_this='/var/www/qianshan/venv/bin/activate_this.py'
execfile(activate_this,dict(__file__=activate_this))
importsys
importlogging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,'/var/www/qianshan/')
sys.path.insert(1,'/var/www/')
fromqianshanimportappasapplication
application.secret_key='Addyoursecretkey'
VirtualHost配置
ServerNameqianshan.co
ServerAdminspark@qianshan.co
WSGIScriptAlias//var/www/qianshan/qianshan.wsgi
Orderallow,deny
Allowfromall
Alias/static/var/www/qianshan/static
Orderallow,deny
Allowfromall
ErrorLog${APACHE_LOG_DIR}/error.log
LogLevelwarn
CustomLog${APACHE_LOG_DIR}/access.logcombined
我会按照1L和2L的方法试试,如果不行再在这里说
临摹微笑
浏览 514回答 2
2回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答