Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

bash says Permission denied when you run a Python script directly

Errorbash: ./script.py: Permission denied

Short answer

Running ./script.py asks the kernel to execute the file, which needs the execute bit, a shebang line, a path you may enter, and a filesystem not mounted noexec. Check with ls -l and findmnt. Add the bit with chmod +x, or skip the problem by running python3 script.py, which only needs read access.

  • Applies to Ubuntu 24.04, Bash 5, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the file's mode, the mount point it sits on, and the mount options. Together they show which cause applies.

    ls -l script.py
    df script.py
    findmnt -o TARGET,VFS-OPTIONS
  2. 2Match what you see, then jump to its fix

    1. 1ls -l shows -rw-r--r-- with no x in the modeThe file has no execute permissionFix
    2. 2The file has x, but findmnt shows noexec in VFS-OPTIONSThe filesystem is mounted noexecFix
    3. 3The path is a folder, or one folder above the script lacks the search (x) permission for youYou are executing something that is not a regular file, or cannot enter its folderFix
    4. 4The first line points at a Python that does not exist or is not executable (the kernel reports a missing interpreter differently from a permission error)The shebang names an interpreter you cannot runFix

What does the error look like?

  • bash: ./script.py: Permission denied

Cause 1 of 4

The file has no execute permission

1Check

The kernel refuses to run a file that lacks the x bit. The mode column from the first step tells you.

ls -l script.py

2Fix

Add the execute bit. The + operator in chmod adds to the existing bits. The first line of the script must also name the interpreter.

chmod +x script.py
head -n 1 script.py   # should be: #!/usr/bin/env python3

3Verify

Run it by path.

./script.py

Confirmed by man7.org

Did this fix it?

Cause 2 of 4

The filesystem is mounted noexec

1Check

The mount manual describes noexec as not permitting direct execution of any binaries on that filesystem, and execve returns EACCES for it.

findmnt -o TARGET,VFS-OPTIONS

2Fix

Run the script through the interpreter, which only needs to read the file. For a permanent fix, move it to a filesystem without noexec, or remove noexec from that mount's options.

python3 script.py

3Verify

The script runs.

python3 script.py; echo "exit code: $?"

Confirmed by man7.org

Did this fix it?

Cause 3 of 4

You are executing something that is not a regular file, or cannot enter its folder

1Check

execve fails with EACCES when search permission is denied on a path component, or when the target is not an ordinary file. A trailing command like ./scripts instead of ./scripts/run.py is a common typo.

ls -ld "$(dirname "$(realpath script.py)")"
file script.py

2Fix

Give the folder execute (search) permission for the user, or call the right file path.

chmod u+x /path/to/folder

3Verify

The folder shows x for your user, and the script starts.

ls -ld "$(dirname "$(realpath script.py)")" && python3 script.py

Confirmed by man7.org

Did this fix it?

Cause 4 of 4

The shebang names an interpreter you cannot run

1Check

For scripts, execve needs the shebang form #!interpreter, and the interpreter must be a valid executable path. A missing interpreter gives ENOENT, a non-executable one gives EACCES.

head -n 1 script.py
ls -l "$(head -n 1 script.py | sed 's/^#! *//' | cut -d' ' -f1)"

2Fix

Point the shebang at a real interpreter. /usr/bin/env python3 finds python3 on PATH.

sed -i '1s|.*|#!/usr/bin/env python3|' script.py

3Verify

Run the script.

./script.py

Confirmed by man7.org

Did this fix it?

How do I stop it coming back?

  • Add #!/usr/bin/env python3 as line one of every script you run directly.
  • Keep scripts on a normal filesystem, not on a /tmp or removable mount with noexec.
  • Call scripts as python3 script.py in automation, so the execute bit and mount options do not matter.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.