The stack trace suggests you're using Windows as the operating system. ls not something that you will typically find on a Windows machine unless using something like CygWin.
Instead, try one of these options:
# use python's standard library function instead of invoking a subprocess
import os
os.listdir()
# invoke cmd and call the `dir` command
import subprocess
subprocess.run(["cmd", "/c", "dir"])
# invoke PowerShell and call the `ls` command, which is actually an alias for `Get-ChildItem`
import subprocess
subprocess.run(["powershell", "-c", "ls"])
Answer from Czaporka on Stack OverflowPython
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
1 day ago - Stderr output of the child process if it was captured by run(). Otherwise, None. This is always bytes when stderr output was captured regardless of the text=True setting. It may remain None instead of b'' when no stderr output was observed. Added in version 3.3. Changed in version 3.5: stdout and stderr attributes added ... Subclass of SubprocessError, raised when a process run by check_call(), check_output(), or run() (with check=True) returns a non-zero exit status.
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - Let's take a look at when to use the Python subprocess module. The subprocess module can be used to automate various system tasks, such as running backups, starting and stopping services, and scheduling cron jobs. For example, you can use the subprocess module to run the cp command to create ...
How to use subprocess.run method in python? - Stack Overflow
I wanted to run external programs using python but I receive an error saying I don't have the file the code I wrote: import subprocess subprocess.run(["ls", "-l"]) Output: Tra... More on stackoverflow.com
How subprocess run() works?
Running python sys.executable in pycharm shows it uses virtual environment interpreter (venv) PS C:\Users\SJ\Desktop\Programs\Python\PyTest> python Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" ... More on discuss.python.org
How do you keep a subprocess running after the python script ends on Windows?
This isn't really possible, because any subprocesses must be terminated when the parent process terminates. However, you can use pythonw to run a script in the background, without opening a window. The easiest way to use it is to change the extension from .py to .pyw. More on reddit.com
subprocess.run: simple but powerful lib to execute external processes
What does subprocess run do?
Subprocess runs the command, waits for the procedure to complete, and then returns a "Completed process" artifact.
simplilearn.com
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
Is subprocess a standard Python library?
With the help of the subprocess library, we can run and control subprocesses right from Python.
simplilearn.com
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
Videos
19:01
Python Tutorial: Calling External Commands Using the Subprocess ...
13:34
Python Standard Library: Subprocess - YouTube
09:57
Using the Python subprocess Module: Gettting Started & Using ...
02:03
Using subprocess to Run Python (Video) – Real Python
02:59
python subprocess run command - YouTube
r/Python on Reddit: Calling a System/Shell command with Python ...
IONOS
ionos.com › digital guide › websites › web development › python subprocess
How to use Python subprocess to execute external commands and programs
June 27, 2025 - We will now use this function for our first small example to illustrate how Python subprocess works. To do this, we first import the subprocess and sys modules and then execute a simple request. The corresponding code looks like this: import subprocess import sys result = subprocess.run([sys.executable, "-c", "print('hello')"])python
Top answer 1 of 2
8
The stack trace suggests you're using Windows as the operating system. ls not something that you will typically find on a Windows machine unless using something like CygWin.
Instead, try one of these options:
# use python's standard library function instead of invoking a subprocess
import os
os.listdir()
# invoke cmd and call the `dir` command
import subprocess
subprocess.run(["cmd", "/c", "dir"])
# invoke PowerShell and call the `ls` command, which is actually an alias for `Get-ChildItem`
import subprocess
subprocess.run(["powershell", "-c", "ls"])
2 of 2
3
ls is not a Windows command. The windows analogue is dir, so you could do something like
import subprocess
subprocess.run(['cmd', '/c', 'dir'])
However, if you're really just trying to list a directory it would be much better (and portable) to use something like os.listdir()
import os
os.listdir()
or pathlib
from pathlib import Path
list(Path().iterdir())
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-subprocess-to-run-external-programs-in-python-3
How To Use subprocess to Run External Programs in Python 3 | DigitalOcean
July 30, 2020 - For example, sys.executable might be a path like /usr/local/bin/python. subprocess.run is given a list of strings consisting of the components of the command we are trying to run.
Simplilearn
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
December 15, 2025 - Learn about Python's subprocess module for executing external commands. Discover how to manage processes and handle inputs/outputs efficiently.
Address 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Codecademy
codecademy.com › article › python-subprocess-tutorial-master-run-and-popen-commands-with-examples
Python Subprocess Tutorial: Master run() and Popen() Commands (with Examples) | Codecademy
Learn how to use Python’s `subprocess` module, including `run()` and `Popen()` to execute shell commands, capture output, and control processes with real-world examples.
Top answer 1 of 7
1
I need to stress that subprocess.run() itself does not search for “python” on Windows, unlike how it’s implemented on POSIX. It just lets the system API find “python”, which is where the peculiar behavior with the application directory comes into play. If, for example, the virtual environment gets c…
2 of 7
0
I am using PyCharm and I don’t get that result.
[pycharm_executable_location]
Maybe it was how you had set up PyCharm during installation?
Python
docs.python.org › 3 › library › asyncio-subprocess.html
Subprocesses — Python 3.14.3 documentation
February 23, 2026 - For processes created with create_subprocess_shell(), the return code reflects the exit status of the shell itself (e.g. /bin/sh), which may map signals to codes such as 128+N. See the documentation of the shell (for example, the Bash manual’s Exit Status) for details. Standard asyncio event loop supports running subprocesses from different threads by default.
W3Schools
w3schools.com › python › ref_module_subprocess.asp
Python subprocess Module
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain return codes. Use it to execute external commands, run shell scripts, or interact with other programs from your Python code.
Python Module of the Week
pymotw.com › 2 › subprocess
subprocess – Work with additional processes - Python Module of the Week
Instead, the function is passed to Popen as the preexec_fn argument so it is run after the fork() inside the new process, before it uses exec() to run the shell. import os import signal import subprocess import tempfile import time import sys script = '''#!/bin/sh echo "Shell script in process ...