#! Python Scripts

In Linux, executable files need to identify how a script should be run also known as the interpreter directive. This is done int the first line of the script with the symbols #!. For bash, something like:

defines that the script is a bash script and uses /bin/bash to execute. For python, the deliminator can either point to the python interpreter directly:

However, Python may exist in more than one location or perhaps a specific version of python needs to be run. In such cases, the exact version can be defined:

A universal way that allows the version of python as defined by the $PATH variable can be used as well:

This will find the Python interpreter installed on the machine and attempt to use that run the script.

I recently tried to setup cron to run a recurrent python script. Cronjobs under Crontab works fine for individual users, but failed to run correctly within /etc/cron.daily. Cron didn’t seem to know how to run python. Sure enough after some digging, I found out that cron doesn’t have much in the way of environment variables. For instance, knows nothing about shell startup files or know much about PATHS.

Comments are closed.



  • slide
  • slide
  • slide
  • slide