0

Hi sorry to bother again, I'm working on my first flask project, very simple one.

I'm deploying my site on Digital Ocean, ubuntu server. And I complied with its instructions.

Problem: When I tried to open the site in browser, it came with 500 error. Then I looked up the apache error log to find the following sentences.

Error log:

[Wed Dec 31 07:45:49 2014] [error] [client 112.64.71.131] mod_wsgi (pid=27835): Target WSGI script '/var/www/qianshan/qianshan.wsgi' cannot be loaded as Python module.

[Wed Dec 31 07:45:49 2014] [error] [client 112.64.71.131] mod_wsgi (pid=27835): Exception occurred processing WSGI script '/var/www/qianshan/qianshan.wsgi'.

[Wed Dec 31 07:45:49 2014] [error] [client 112.64.71.131] Traceback (most recent call last):

[Wed Dec 31 07:45:49 2014] [error] [client 112.64.71.131] File "/var/www/qianshan/qianshan.wsgi", line 7, in

[Wed Dec 31 07:45:49 2014] [error] [client 112.64.71.131] from qianshan import app as application

[Wed Dec 31 07:45:49 2014] [error] [client 112.64.71.131] ImportError: cannot import name app

Tree structure of the project

spark@Qianshan:/var/www/qianshan$ tree -L 2
.
├── qianshan
│   ├── config.ini
│   ├── qianshan.py
│   ├── static
│   ├── templates
│   └── venv
└── qianshan.wsgi

Virtual Host configuration

<VirtualHost *:80>
            ServerName qianshan.co
            ServerAdmin spark@qianshan.co
            WSGIScriptAlias / /var/www/qianshan/qianshan.wsgi
            <Directory /var/www/qianshan/qianshan/>
                    Order allow,deny
                    Allow from all
            </Directory>
            Alias /static /var/www/qianshan/qianshan/static
            <Directory /var/www/qianshan/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>

wsgi

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/qianshan/")

from qianshan import app as application
application.secret_key = 'Add your secret key'

.py file

# Filename: qianshan.py
# encoding: utf-8

import ConfigParser
import codecs
from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
    block_list = getBlockList()
    website_dict = getWebsiteDict()
    return render_template('index.html', block_list=block_list, website_dict=website_dict)
...
...
if __name__ == '__main__':
    app.run()

Happy new year to all if you are lucky enough to see this bottom line ^_^

Community
  • 1
  • 1
姜嘉维
  • 181
  • 2
  • 12

1 Answers1

0

create a /var/www/qianshan/init.py file as show below:

from flask import Flask
app = Flask(__name__)
import quianshan.quianshan #This is your view file. 

Then remove this line from quianshan.py

app = Flask(__name__)
Prakash Kuma
  • 722
  • 1
  • 6
  • 11
  • thanks so much! Do you mean there should be a file named __init__.py? if so, what if I changed the name of current .py file from qianshan.py to __init__.py, will this solve the problem? – 姜嘉维 Dec 31 '14 at 17:15
  • I've changed the .py file name to __init__.py, reload the virtual host configuration, restart apache service, and still not working. Errors the same – 姜嘉维 Jan 01 '15 at 09:34