-1

I got this error -bash: ./a3.py: Permission denied when i try to execute my python code in linux.

Can anyone have any idea why I get this error?

OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101
jojo
  • 29
  • 1
  • 3

2 Answers2

0

check if you have permission for execution

ls -la a3.py

If not change the permissions

chmod +x a3.py

Obviously you need to add at the file start (within the Shebang Line) where is the python interpreter to run the script directly:

#!/usr/bin/env python3
J. Murray
  • 1,460
  • 11
  • 19
Nikaido
  • 4,443
  • 5
  • 30
  • 47
0

One way to deal with the problem is simply by using the python command to execute the script instead of entering the script's name as a command directly:

python <script name>

There is another, more advanced solution that allows you to directly execute the script (without the python command as a prefix). In Linux, before a file, such as a Python script, can be directly executed, one has to add execute permission to the file. This can be done with the following command:

chmod +x <script name>

In addition, you need to insert a Shebang line at the beginning of your script, so Linux will know that the script is written with and should be executed using Python (not some other language). To do that, first run the command:

which python

which will give the path to the Python interpreter on your machine. Then add the following as the first line of your script:

#!<output of previous command>

After doing these steps, you should be able to execute the Python script normally, using the command:

./<script name>
  • 1
    you dont need the execute permission to call `python ` you just need read permission – Albin Paul Oct 05 '19 at 04:46
  • Thank you, even though it's been 3 years! I've added information about how the `python` command can be ran directly while also describing the other option (adding the execute permission). –  Oct 05 '22 at 22:21