Python: OSError: [Errno 2] No such file or directory on subprocess.Popen

16,676

Add this print to your file:

import os
print os.environ['PATH']

Compare the outputs after running the script from IDE and terminal.
You should observe that the IDE's PATH does not include the mytool's directory.

Share:
16,676
Nemo
Author by

Nemo

Updated on June 28, 2022

Comments

  • Nemo
    Nemo almost 2 years

    I have added the path to mytool in .bashrc and I could run mytool --help from any path in bash shell. However when when I run the following snippet, I get :

    File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory

    import subprocess
    
    command_array = ['mytool', '--help']
    
    p = subprocess.Popen(command_array,
                         stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
                        )
    
    for line in iter(p.stdout.readline, b''):
            print(line)
    p.stdout.close()
    

    How can I resolve this?

    EDIT: When I run the python file from terminal (bash), it works fine. But when I run from PyCharm (debugger) or other shells it is giving the above error.

    How do I change my script so that it runs 'mytool' in bash when I run the script from other shells? I need the environment added in .bashrc

  • Nemo
    Nemo almost 9 years
    The path that is printed doesn't have the paths added to .bashrc. How do I change my script so that it runs 'mytool' in bash when I run the script from other shells?
  • Eugeniu Rosca
    Eugeniu Rosca almost 9 years
    Try one of two (or both): 1) command_array = ['/path/to/mytool', '--help'] 2) Update the PATH environment variable in your IDE (howTo)