Flask, ImportError: No module named XXX


第一个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不久,请多敲打点拨

flask python python2.7

孤月盈雪映霜清 11 years, 1 month ago

很简单,看
qianshan【 你这个文件夹并没有加在sys.path中
├── config.ini
├── extraModules.py
├── extras.py

另外,你在代码中也不能直接 import extras,至少也要带上包名:
import qianshan.extras as extras

...balabala

姐控團D社長 answered 11 years, 1 month ago

Your Answer