Flask问题求助 No module named XXX怎么解决,flasknamed,第一个flask项目,做


第一个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 Flaskfrom flask import render_templateimport extraModulesimport extras#如果没有这句无报错正常运行,只有一些静态资源没拉到,可能是其他问题import ConfigParserimport codecsimport logginglogging.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.pyclass 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.pyprint '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/pythonactivate_this = '/var/www/qianshan/venv/bin/activate_this.py'execfile(activate_this, dict(__file__=activate_this))import sysimport logginglogging.basicConfig(stream=sys.stderr)sys.path.insert(0,'/var/www/qianshan/')sys.path.insert(1,'/var/www/')from qianshan import app as applicationapplication.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的方法试试,如果不行再在这里说

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

问题已解决,是文件的权限问题。和import的语法无关。

非常感谢大家对我的帮助和指导!虽然这个问题搞了一周时间,但是我学习了很多。。。

下图中可以看到目前项目根目录下各个文件的权限,otherModules(就是之前的extras)对root所在的用户组没有读权限。

-rwx------ 1 root root 9091 Jan  3 09:27 config.ini-rwxr--r-- 1 root root  404 Jan  7 22:58 __init__.py-rwx------ 1 root root 2316 Jan  7 23:07 otherModules.py-rwxr--r-- 1 root root  360 Jan  5 23:34 qianshan.wsgidrwxr-xr-x 2 root root 4096 Jan  3 09:08 staticdrwxr-xr-x 2 root root 4096 Jan  3 08:51 templates-rw-r--r-- 1 root root  262 Jan  7 23:05 testModule.py-rwx------ 1 root root 1716 Jan  3 09:36 test.pydrwxr-xr-x 6 root root 4096 Jan  3 08:05 venv

chmod调整权限后,权限如下,项目可以正常访问,不再报错

-rwxr--r-- 1 root root 9091 Jan  3 09:27 config.ini-rwxr--r-- 1 root root  404 Jan  7 22:58 __init__.py-rwxr--r-- 1 root root 2316 Jan  7 23:07 otherModules.py-rwxr--r-- 1 root root  360 Jan  5 23:34 qianshan.wsgidrwxrwxr-x 2 root root 4096 Jan  3 09:08 staticdrwxrwxr-x 2 root root 4096 Jan  3 08:51 templates-rw-r--r-- 1 root root  262 Jan  7 23:05 testModule.py-rwxr--r-- 1 root root 1716 Jan  3 09:36 test.pydrwxr-xr-x 6 root root 4096 Jan  3 08:05 venv

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

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

...balabala

编橙之家文章,

评论关闭