Search code examples
pythoncpanelpassengerwsgi

cPanel App runs on Python2 instead of Python3


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?


Solution

  • 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 :

    import os
    INTERP = "/usr/bin/python3"
    if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
    

    so the final result would be :

    passenger_wsgi.py

    
    #!/usr/bin/env python3
    import sys
    import os
    
    # Solution
    INTERP = "/usr/bin/python3"
    if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
    
    from flask import Flask
    
    application = Flask(__name__)
    
    application.route("/")
    def index():
        return sys.version
    
    

    and the respond is correctly as I intended in the first place:

    3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]