Home › Python

python: can't open file '...': [Errno 2] No such file or directory — how to fix

python: can't open file 'C:\\Users\\me\\main.py': [Errno 2] No such file or directory
python3: can't open file '/home/me/app.py': [Errno 2] No such file or directory

Quick fix: Check where you are and what's there: pwd / cd and ls / dir. Then cd into the script's folder or pass the full path: python /full/path/to/app.py. On Windows check the real file name isn't app.py.txt.

What it means

python something.py looks for something.py relative to the current working directory of your terminal, not relative to where Python is installed and not relative to your project. If the file isn't there, Python stops before running any code.

This is different from FileNotFoundError raised inside a running script (see FileNotFoundError). Here the script itself is missing.

Cause 1: you're in the wrong directory

pwd          # Linux/macOS (PowerShell also has pwd)
ls           # or "dir" in cmd.exe

Fix by moving into the folder, or by giving the full path:

cd ~/projects/myapp
python app.py

python ~/projects/myapp/app.py

New terminals usually open in your home folder, not your project folder.

Cause 2: the file name is not what you think

Common mismatches:

Cause 3: spaces in the path

Without quotes the path is split at the space and Python receives only the first part:

python "C:\Users\me\My Projects\main.py"
python "/home/me/My Projects/main.py"

Cause 4: VS Code / IDE runs from another folder

If the "Run" button or a launch config gives this error, the terminal's working directory is not the script's folder. Open the project folder itself (File → Open Folder), or in launch.json set:

"cwd": "${fileDirname}"

Cause 5: you meant to run a module

For packages or tools, use -m with the module name, not a file path:

python -m http.server
python -m pip install requests
python -m mypackage.main      # runs mypackage/main.py from the project root

python mypackage.main would look for a file literally called mypackage.main.

Cause 6: inside Docker

The path in CMD is relative to WORKDIR, and the file must have been copied into the image:

WORKDIR /app
COPY . .
CMD ["python", "app.py"]

Check what's actually in the image:

docker run --rm -it myimage ls -la /app

A .dockerignore entry or a wrong COPY source is a frequent reason the file is missing. A bind mount (-v $(pwd):/app) also hides whatever was copied into /app.

Cause 7: cron, systemd or scheduled tasks

These don't start in your project folder. Always use absolute paths for both the interpreter and the script:

*/5 * * * * /usr/bin/python3 /home/me/myapp/app.py

Last updated 2026-12-05.