I have a VPS Hosting in cPanel, and a Flask-App.
I followed the instructions of How to install WSGI Application in cPanel.
Everything is working fine, when I run the application using the terminal, but when I open the App URL, it shows the following error:
Traceback (most recent call last):
File "/opt/cpanel/ea-ruby24/root/usr/share/passenger/helper-scripts/wsgi-loader.py", line 369, in <module>
app_module = load_app()
File "/opt/cpanel/ea-ruby24/root/usr/share/passenger/helper-scripts/wsgi-loader.py", line 76, in load_app
return imp.load_source('passenger_wsgi', startup_file)
File "/home/qsemh/Maidan/passenger_wsgi.py", line 8, in <module>
wsgi = imp.load_source('wsgi', 'wsgi.py')
File "wsgi.py", line 1, in <module>
from flaskr import create_app
File "/home/qsemh/Maidan/flaskr/__init__.py", line 133
print(f"Validate {len(users)} Users At {current_time}")
^
SyntaxError: invalid syntax
So I’ve decided to create a simpler app in order to detect the issue, the app is as following :
passenger_wsgi.py
#!/usr/bin/env python3
import sys
from flask import Flask
application = Flask(__name__)
application.route("/")
def index():
return sys.version
When I run this simple Application Using the URL it shows the following as a respond :
2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
even though I’ve used the shebang #!/usr/bin/env python3
at the start of the file, and when I run it using the terminal, it works as if it using python3.
I’ve tried changing the shebang to the following formats :
#!/usr/bin/python3
#!/usr/bin/env 'python3'
but they gave the same result.
What is the problem here, and How can I solve it?
2
Answers
I've found the Correct approach to solve this issue;
Basically, I only needed to add the following lines at the beginning of my passenger_wsgi.py file :
so the final result would be :
passenger_wsgi.py
and the respond is correctly as I intended in the first place:
This answer might be for some cPanel users. My cPanel is using passenger and wsgi, and even though my cPanel Python application is set to use python 3.9. it was running 2.7.5. Worse, it set all the 2.7 folders in the path.
So I ended up with this helper script called from
passenger_wsgi.py
. I’m moved all the code out of this file into an import because this file may get blown away and re-created in some cases. It’s not safe to rely on code in this file surviving. It’s an auto generated file.Put
set_python_environment.py
file in the same directory aspassenger_wsgi.py
set_python_environment.py
Modified passenger_wsgi.py
In order to get the right path, I typed
whereis python
in the ssh environment after running thesource
command given on the python application page, to find the location of all the python versions, and the one ending inbin
turned out to be the one which actually works.