Running bash script with python on local machine - python

I'm trying to run a bash script on my python script with
subprocess and also tried with os.system but failed to do so
the command that I tried to execute is:
totp <<(echo "<some_code>")
I created 'totp' bash file with the following commands:
cd /usr/local/bin
touch totp
vim totp
copy and paste the bash script content
and then
chmod +x totp
Via terminal of the PyCharm I run the bash command fine but via python code, I get the error:
FileNotFoundError: [Errno 2] No such file or directory: 'totp <<(echo "")'

Related

Run python script from Docker

Trying to run test Python script from Docker using Bash. Win10 OS.
I have a file print.py in the directory C:/Py/test
run this $ cd C:/Py/test
run this $ docker run --volume $(pwd):/home/${USER} python:3.7 python /home/${USER}/print.py
Got this error: python: can't open file 'C:/Program Files/Git/home/print.py': [Errno 2] No such file or directory
Why is this file trying to be found in the C:/Program Files/Git/home/ directory instead of C:\Py\test?
$ python 'print.py' is working
Git bash prepended its installation path, try these two methods :
Use: //home/${USER}/print.py
Run with: MSYS_NO_PATHCONV=1 docker run ...

Permission denied error when executing .py file with Geany IDE (Raspberry Pi Raspbian)

I configured a python3 virtual env and added the path to the execute Build Command of Geany.
Now if I try to execute the .py file with Geany I get the following error:
Wi-Fi is currently blocked by rfkill.
Use raspi-config to set the country before use.
/tmp/geany_run_script_5J3UG0.sh: 7: /tmp/geany_run_script_5J3UG0.sh: /home/pi/PyProjekte/pytorchenv1/bin: Permission denied
------------------
(program exited with code: 126)
I have already tried the command:
chmod +x ~/PyProjekte/pytorchenv1/bin
and:
chmod +x /tmp/geany_run_script_5J3UG0.sh
but if I try to chmod the .sh file it tells me that the .sh file is not available
I hope you can help me with that.
If I execute the .py file manually with the command line within the virtual env it executes normally but I would like to be able to execute it from Geany.
This is what I have added to the Execute Command in Geany:
/home/pi/PyProjekte/pytorchenv1/bin "%f"

Python script not running : -bash: ./btltest.py: /usr/bin/python3: bad interpreter: No such file or directory

I have a python script that establishes connection to postgres db and writes results in csv.
When I run it from terminal on Mac, using command
administrators-MacBook-Pro:ml bob$ ./btltest.py -d dev -u temp_viewer -p "xxxxxxx" -l debug -s "2019-08-10" -e "2019-08-20"
it shows error
-bash: ./btltest.py: /usr/bin/python3: bad interpreter: No such file or directory
Using same credentials i.s. pass, user, db name as in the code to connect through pgadmin4 has no problem.
How can I remove this error ?

Python scripts executes Kubernetes command: kubectl apply -f config_file.yaml

I have generated a python script that opens a deployment config_file.yaml, modifies some parameters and saves it again, using pyyaml. This python script will be executed in the master node of a Kubernetes cluster.
Once is generated the new file, my intention is to execute
kubectl apply -f config_file.yaml
in the python script to apply the modifications to the deployment.
I have been reading how to do it using kubernetes python client, but it seems it is not prepare to execute kubectl apply.
So the other option is to create a bash script and execute it from python script.
Bash scripts:
#!/bin/bash
sudo kubectl apply -f config_file.yaml
I give it permissions
chmod +x shell_scipt.sh
Python script:
import subprocess
subprocess.call(['./shell_script.sh'])
But an error appears:
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
I don't know how to resolve this error, I have tested givin permissions to the bash script, but nothing worked.
I do not know anything about Kubernetes but I think I might help.
I am basically suggesting that you run the command directly from Python script, not having Python running a bash script which runs a command.
import os
command = 'kubectl apply -f config_file.yaml'
password = 'yourpassword'
p = os.system('echo %s|sudo -S %s' % (passs, command))
If I understand correctly you are using python to dynamically modify static yaml files. If this is the case I would recommend using helm which if perfect for making static yaml file dynamic :-)
How are you running python script ?
I think you are running python script with a non sudo user. Try to run python script as sudo user this way your subprocess will have access to that file.
if this fixes your issue let me know.

Can't open sh file

I'm trying to execute a sh file from script python.
My script python
os.system('sh run.sh')
My sh file
echo 'The house is blue' | /opt/palavras/por.pl > output.txt
Error:
sh: 0: Cant' open run.sh
How can I fix it?
Make sure that your bash script has the right permissions (i.e it is executable). In a terminal run:
chmod +x run.sh
and then try (assuming that run.sh is in the same directory as your python script)
import os
os.system('./run.sh')
I do not believe this would run from the terminal either because you must run the file in order. Try:
os.system('sh chmod +x run.sh|./run.sh')
instead.
See: https://askubuntu.com/questions/38661/how-do-i-run-sh-files for details on running sh files and how to use os.system() in python for running an shell order for the use of | running in an order in a shell.

Categories