Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute python3 program in shell script

I have a python program which I need to run at a particular day of a month, so I am using crontab for this task and create a shell script to run this python program.

This is part of my shell script:

#!/bin/bash
filepath='file2018'
cd ${filepath}
python3 file.py

When I run the crontab which executes the shell script, the log file shows the following error:

line 9: python3: command not found 

I'm really confused about why this error occurs because I have already install python3 and I can run python3 directly from the command line.

Besides, if I replace python3 with python, the shell script works! My python version is python2, but I have to use python3 for this program, so I have to use python3 instead of python.

My operating system is Linux CentOS.

Hope someone can give me some tips!

like image 808
Xiaodan Tang Avatar asked May 12 '26 12:05

Xiaodan Tang


2 Answers

You can give the full path to the python3 executable. You can get it using the which python3 command. Try it out.

like image 149
Parthapratim Neog Avatar answered May 14 '26 03:05

Parthapratim Neog


in file.py add first line like below and add +x permission to file.py file

#!/usr/bin/python3

it will automatically execute, no need to mention python3 in the script use "which python3" command to know exact path of python3 in your machine

like image 41
ShivYaragatti Avatar answered May 14 '26 02:05

ShivYaragatti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!