简体   繁体   中英

Shebang line #!/usr/bin/python3 preventing server run

Here's the code I have. Basically I have the Shebang line in there because the psycopg2 wasn't working without it.

But now when I have this line in there it doesn't allow me to run the database, it just says "no module named 'flask'"

 #!/usr/bin/python3.4
    #
    # Small script to show PostgreSQL and Pyscopg together
    #

    from flask import Flask, render_template
    from flask import request
    from flask import *
    from datetime import datetime
    from functools import wraps
    import time
    import csv
    import psycopg2
    app = Flask(__name__)
    app.secret_key ='lukey'
    def getConn():
        connStr=("dbname='test' user='lukey' password='lukey'")
        conn=psycopg2.connect(connStr)
        return conn

    @app.route('/')
    def home():
        return render_template(index.html)

    @app.route('/displayStudent', methods =['GET'])
    def displayStudent():
        residence = request.args['residence']
        try:
            conn = None
            conn = getConn()
            cur = conn.cursor()
            cur.execute('SET search_path to public')

            cur.execute('SELECT stu_id,student.name,course.name,home_town FROM student,\
                        course WHERE course = course_id AND student.residence = %s',[residence])
            rows = cur.fetchall()
            if rows:
                return render_template('stu.html', rows = rows, residence = residence)
            else:
                return render_template('index.html', msg1='no data found')

        except Exception as e:
            return render_template('index.html', msg1='No data found', error1 = e)

        finally:
            if conn:
                conn.close()

    #@app.route('/addStudent, methods =['GET','POST']')
    #def addStudent():

    if __name__ == '__main__':
        app.run(debug = True)

This is an environment problem, not a flask, postgres or shebang problem. A specific version of Python is being called, and it is not being given the correct path to its libraries.

Depending on what platform you are on, changing you shebang to #! /usr/bin/env python3 #! /usr/bin/env python3 can fix the problem, but if not (very likely not, though using env is considered better/portable practice these days), then you may need to add your Python3 libs location manually in your code.

sys.path.append("/path/to/your/python/libs")

If you know where your Python libs are (or maybe flask is installed somewhere peculiar?) then you can add that to the path and imports following the line where you added to the path will include it in their search for modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM