<p>I am trying to execute the <code>kubectl</code> command using python script but keep getting error. I have requirement to execute the <code>kubectl</code> command to create pod and check the <code>pod</code> log for any failure.</p>
<p>What am I doing wrong here?</p>
<pre><code>import subprocess

command = 'kubectl apply -f deployment.yaml'


check_output= subprocess.check_output(command)
print(check_output)


</code></pre>
<p>error</p>
<pre><code>Traceback (most recent call last):
  File &quot;/usr/bin/cma-scripts/kubectl.py&quot;, line 6, in &lt;module&gt;
    check_output= subprocess.check_output(command)
  File &quot;/usr/local/lib/python3.9/subprocess.py&quot;, line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File &quot;/usr/local/lib/python3.9/subprocess.py&quot;, line 505, in run
    with Popen(*popenargs, **kwargs) as process:
  File &quot;/usr/local/lib/python3.9/subprocess.py&quot;, line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File &quot;/usr/local/lib/python3.9/subprocess.py&quot;, line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'kubectl apply -f deployment.yaml'

</code></pre>

<p>You can execute <code>kubectl</code> commands with Python, but you can also use the <a href="https://github.com/kubernetes-client/python" rel="noreferrer">Python client</a> for the Kubernetes API.</p>
<p>Below I will give examples for both options.</p>
<h3>Executing kubectl commands with Python.</h3>
<p>You can use the <a href="https://docs.python.org/3.7/library/subprocess.html" rel="noreferrer">subprocess</a> module:</p>
<pre><code>$ cat script-1.py
#!/usr/bin/python3.7

import subprocess
subprocess.run([&quot;kubectl&quot;, &quot;apply&quot;, &quot;-f&quot;, &quot;deployment.yaml&quot;])


$ ./script-1.py
deployment.apps/web-app created
</code></pre>
<p>You can also use the <a href="https://docs.python.org/3.7/library/os.html" rel="noreferrer">os</a> module:</p>
<pre><code>$ cat script-1.py
#!/usr/bin/python3.7

import os
os.system(&quot;kubectl  apply -f deployment.yaml&quot;)

$ ./script-1.py
deployment.apps/web-app created
</code></pre>
<h3>Using the Python client for the kubernetes API.</h3>
<p>As previously mentioned, you can also use a Python client to create a Deployment.</p>
<p>Based on the <a href="https://github.com/kubernetes-client/python/blob/master/examples/deployment_create.py" rel="noreferrer">deployment_create.py</a> example, I've created a script to deploy <code>deployment.yaml</code> in the <code>default</code> Namespace:</p>
<pre><code>$ cat script-2.py
#!/usr/bin/python3.7

from os import path

import yaml

from kubernetes import client, config


def main():
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), &quot;deployment-1.yaml&quot;)) as f:
        dep = yaml.safe_load(f)
        k8s_apps_v1 = client.AppsV1Api()
        resp = k8s_apps_v1.create_namespaced_deployment(
            body=dep, namespace=&quot;default&quot;)
        print(&quot;Deployment created. status='%s'&quot; % resp.metadata.name)


if __name__ == '__main__':
    main()

$ ./script-2.py
Deployment created. status='web-app'

$ kubectl get deployment
NAME      READY   UP-TO-DATE   AVAILABLE   
web-app   1/1     1            1   

    
</code></pre>
