Running Python 3 scripts

#1
What is the shebang for Python 3 scripts, and the Python 3 command line command, on shared hosting?

It's easy to run Python 2 using
Code:
python
and run scripts with any of these shebangs from the command line, cronjobs, and from a web call:

Code:
#!/usr/bin/python
#!/usr/bin/python2
#!/usr/bin/python2.7
And while I can use the Setup Python App feature in cPanel to create a web app using Python 3, I cannot find the path to python 3 on the shared hosting server to run as a cronjob, to pipe an email to, or to run from a web call.

What is the path to Python 3?
 

PH-Marc-André.B.

Conseiller technique
Membre du personnel
#2
Hello,

The python version on the server by default is in fact version 2.7 as you have found.

That being said in the N0C panel you may create a python 3 + app (up to version 3.8)

Once the application has been configured here are the required steps in order to setup the proper binary for python:

You need to invoke the app's environment by running the above command before performing any other commands (such as pip install or python).

Code:
source /home/N0CUSER/virtualenv/python_app/3.7/bin/activate
Where 'N0CUSER' should be replaced by the Actual user name and the correct Python version number indicated to suit your needs.

This command will adjust the environment variables and allow you to configure your application.


Then in the shell the python --version command shall net you something like this:

Code:
(python/app:3.7)[N0CUSER@nodels1-ca ~]$ python --version
Python 3.7.10
(python/app:3.7)[N0CUSER@nodels1-ca ~]$ which python
~/virtualenv/python_app/3.7/bin/python
(python/app:3.7)[N0CUSER@nodels1-ca ~]$
 
#3
Thank you!

I'm happy to say that these steps work on cPanel accounts as well. My primary account was pushed to be a cPanel account because some requirements could not be met with N0C last year.

I see that a shell script can switch to the virtual environment and execute commands in it. That's great, and will work with my cron jobs.

I'll have to test to see if the cPanel email forwarder can pipe an email to the Python script through a shell script that switches the environment, or if will have to add
Code:
&& python script.py
to the `source` command and have that work in cPanel as the pipe command.
 
Haut