Jump to content

[Solved] Import Psycopg2 Library Make My Flask Application No Work Properly


oswako

Recommended Posts

Hi

 

I have a flask application that runs perfectly when passing a json package, and returns the same package to the client with no problems but when importing psycopg2 the json package returned is "None" and client complain about the parsing something that is not a json package.

 

Working Code using json library:

 

#!/usr/bin/env python
import sys
#import psycopg2
import json
from flask import Flask,request,jsonify, __version__
app = Flask(__name__)
application = app
@app.route("/")
def hello():
return """
HelioHost rules!<br><br>
<a href="/flask/python/version/">Python version</a><br>
<a href="/flask/flask/version/">Flask version</a>
"""
@app.route("/python/version/")
def p_version():
return "Python version %s" % sys.version
@app.route("/flask/version/")
def f_version():
return "Flask version %s" % __version__
@app.route("/mother")
def mother():
return "Hello world mother"
@app.route("/jsonp",methods=['POST'])
def jsonp():
webdatajson = request.get_json() # get json package
#datajson = jsonify(webdatajson) #make a json package to send back
datajson = json.dumps(webdatajson) #makes a json package to send back
return(datajson)
if __name__ == "__main__":
app.run()

 

Another working code using jsonify from flask:

 

#!/usr/bin/env python
import sys
#import psycopg2
import json
from flask import Flask,request,jsonify, __version__
app = Flask(__name__)
application = app
@app.route("/")
def hello():
return """
HelioHost rules!<br><br>
<a href="/flask/python/version/">Python version</a><br>
<a href="/flask/flask/version/">Flask version</a>
"""
@app.route("/python/version/")
def p_version():
return "Python version %s" % sys.version
@app.route("/flask/version/")
def f_version():
return "Flask version %s" % __version__
@app.route("/mother")
def mother():
return "Hello world mother"
@app.route("/jsonp",methods=['POST'])
def jsonp():
webdatajson = request.get_json() # get json package
datajson = jsonify(webdatajson) #make a json package to send back
#datajson = json.dumps(webdatajson) #makes a json package to send back
return(datajson)
if __name__ == "__main__":
app.run()

 

 

 

NOT WORKING CODE once importing psycopg2:

 

#!/usr/bin/env python
import sys
import psycopg2
import json
from flask import Flask,request,jsonify, __version__
app = Flask(__name__)
application = app
@app.route("/")
def hello():
return """
HelioHost rules!<br><br>
<a href="/flask/python/version/">Python version</a><br>
<a href="/flask/flask/version/">Flask version</a>
"""
@app.route("/python/version/")
def p_version():
return "Python version %s" % sys.version
@app.route("/flask/version/")
def f_version():
return "Flask version %s" % __version__
@app.route("/mother")
def mother():
return "Hello world mother"
@app.route("/jsonp",methods=['POST'])
def jsonp():
webdatajson = request.get_json() # get json package
#datajson = jsonify(webdatajson) #make a json package to send back
datajson = json.dumps(webdatajson) #makes a json package to send back
return(datajson)
if __name__ == "__main__":
app.run()

 

I'm trying to get the json from the client and then store the data into the postgresql that is why i need the psycopg2 adapter.

 

Is this adapter installed or am I doing something wrong?

 

Also for the future when connecting to the postgresql database from the flask app how should I specify the host? ("http://localhost:5432" or "http://ricky.heliohost.org:5432")

 

Thanks in advanced



Forgot to mention my data

 

username: ioteche
domain: ioteche.heliohost.org
server: Ricky

Link to comment
Share on other sites

You actually don't have to use a shebang line on flask scripts because only one version of python is available to run flask. /usr/bin/env python is actually python 2.7.5 but like I said the flask.wsgi forces it to use python 3.6.1 and it will just ignore that shebang. You don't need it so you may as well remove it.

 

Not all python modules are installed by default. If you need a module installed, or if you want to check if it's already installed just let us know which version of python you're using, and what server you're on, and what module you need. I installed pyscopg2 for you on Python 3.6.1 on Ricky.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...