简体   繁体   中英

Execute python program with bash script

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM