bash says Permission denied when you run a Python script directly
bash: ./script.py: Permission deniedShort 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.
Fix it fast
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-OPTIONS2Match what you see, then jump to its fix
- 1
ls -l shows -rw-r--r-- with no x in the modeThe file has no execute permissionFix - 2
The file has x, but findmnt shows noexec in VFS-OPTIONSThe filesystem is mounted noexecFix - 3
The 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
The 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
- 1
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.py2Fix
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 python33Verify
Run it by path.
./script.pyConfirmed by man7.org
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-OPTIONS2Fix
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.py3Verify
The script runs.
python3 script.py; echo "exit code: $?"Confirmed by man7.org
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.py2Fix
Give the folder execute (search) permission for the user, or call the right file path.
chmod u+x /path/to/folder3Verify
The folder shows x for your user, and the script starts.
ls -ld "$(dirname "$(realpath script.py)")" && python3 script.pyConfirmed by man7.org
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.py3Verify
Run the script.
./script.pyConfirmed by man7.org
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Servers
pip install worked but the jupyter command is not found
bash: jupyter: command not found
The shell can only run programs that sit in a folder on PATH.
2 causes, each with a fixServers
apt-get does not exist on this EC2 instance
sudo: apt-get: command not found
apt-get belongs to Debian and Ubuntu.
2 causes, each with a fixServers
bash warns that it cannot change the locale after you log in to a server
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
The shell was told to use a locale that is not built on the server.
3 causes, each with a fixServers
Syntax error: redirection unexpected when a Bash script runs under sh
script.sh: 12: Syntax error: redirection unexpected
The operators <<< (here-string) and &> are Bash features.
3 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
cv2.imshow raises 'not implemented' because the wrong OpenCV package is installed
cv2.imshow() works in one environment and fails in another
The headless OpenCV packages are built without any GUI code, so cv2.imshow cannot open a window.
2 causes, each with a fix