Execute python program with bash script

430 Views Asked by At

I have following bash script:

export MYCONFIG_CONFIG=TEST
/home/bmwant/projects/test/venv/bin/python3 /home/bmwant/projects/test/script.py

My python script needs environment variable which I have set, but running this script as

bash run_python.sh

shows an error

/home/bmwant/projects/test/venv/bin/python3: can't open file 'home/bmwant/projects/tes': [Errno 2] No such file or directory

What is wrong? I have set script as executable with chmod u+x run_python.sh

2

There are 2 best solutions below

1
On BEST ANSWER

So, the problem was in file format. I have created it in Windows and then copy via FileZilla to remote server where I was trying to run it. So just run

apt-get install dos2unix
dos2unix run_python.sh
bash run_python.sh

and all will work well. Thanks @Horst for the hint.

3
On

Have made script.py executable?

chmod u+x /home/bmwant/projects/test/script.py

Try this script:

#!/bin/bash
export MYCONFIG_CONFIG=TEST
pushd /home/bmwant/projects/test/venv/bin/
./python3 /home/bmwant/projects/test/script.py
popd

You can run it via:

./run_python.sh