为了账号安全,请及时绑定邮箱和手机立即绑定

Flask, ImportError: No module named XXX

Flask, ImportError: No module named XXX

眼眸繁星 2019-02-26 05:42:33
第一个flask项目,做一个简单的网址导航。部署项目后,浏览器访问报500错误。查看apache日志后,报错如下: [Tue Jan 06 09:58:22 2015] [error] hello world [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] mod_wsgi (pid=31615): Target WSGI script '/var/www/qianshan/qianshan.wsgi' cannot be loaded as Python module. [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] mod_wsgi (pid=31615): Exception occurred processing WSGI script '/var/www/qianshan/qianshan.wsgi'. [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] Traceback (most recent call last): [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] File "/var/www/qianshan/qianshan.wsgi", line 12, in <module> [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] from qianshan import app as application [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] File "/var/www/qianshan/__init__.py", line 4, in <module> [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] import extras [Tue Jan 06 09:58:22 2015] [error] [client 112.64.71.131] ImportError: No module named extras 项目结构 qianshan ├── config.ini ├── extraModules.py ├── extras.py ├── __init__.py ├── qianshan.wsgi ├── static ├── templates ├── test.py └── venv init.py代码 from flask import Flask from flask import render_template import extraModules import extras#如果没有这句无报错正常运行,只有一些静态资源没拉到,可能是其他问题 import ConfigParser import codecs import logging logging.basicConfig(filename='qianshan.log', level=logging.INFO) logging.info('Started') app = Flask(__name__) logging.info('App established') @app.route("/") def index(): return render_template('index.html') extras.py是实际我想要导入的模块,里面有两个我需要的类;extraModules是测试模块,import extraModules是成功的,apache日志的第一行也打出来了;他们的代码分别如下: extras.py # Filename : extras.py class Block: def setNo(self, no): self.no = int(no) def getNo(self): return self.no def setName(self, name): self.name = name def getName(self): return self.name def setPriority(self, priority): self.priority = int(priority) def getPriority(self): return self.priority def setHotKeyAsc(self, hotKeyAsc): self.hotKeyAsc = hotKeyAsc def getHotKeyAsc(self): return self.hotKeyAsc def setElement(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]) class Website: def setNo(self, no): self.no = int(no) def getNo(self): return self.no def setName(self, name): self.name = name def getName(self): return self.name def setUrl(self, url): self.url = url def getUrl(self): return self.url def setIcon(self, icon): self.icon = icon def getIcon(self): return self.icon def setBlockNo(self, blockNo): self.blockNo = int(blockNo) def setPriority(self, priority): self.priority = int(priority) def getPriority(self): return self.priority def setHotKeyAsc(self, hotKeyAsc): self.hotKeyAsc = hotKeyAsc def getHotKeyAsc(self): return self.hotKeyAsc def setElement(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 'hello world' 其他背景信息: 项目部署在digital ocean的ubuntu12.x主机上,python版本2.7.3. 还请指导下,是不是extras.py有什么地方大意了,小弟新学python不久,请多敲打点拨 谢谢1L和2L前辈的指点啊!白天一直在上课,以下为1月7日晚更新,我把wsgi以及virtual host的配置也发一下,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)) import sys import logging logging.basicConfig(stream=sys.stderr) sys.path.insert(0,'/var/www/qianshan/') sys.path.insert(1,'/var/www/') from qianshan import app as application application.secret_key = 'Add your secret key' Virtual Host配置 <VirtualHost *:80> ServerName qianshan.co ServerAdmin spark@qianshan.co WSGIScriptAlias / /var/www/qianshan/qianshan.wsgi <Directory /var/www/qianshan/> Order allow,deny Allow from all </Directory> Alias /static /var/www/qianshan/static <Directory /var/www/qianshan/static/> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 我会按照1L和2L的方法试试,如果不行再在这里说
查看完整描述

3 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

将第四行修改为:
from qianshan import extras

查看完整回答
反对 回复 2019-03-01
  • 3 回答
  • 0 关注
  • 1416 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信