Permission denied – Linux bash

Question:

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?

Asked By: jojo

||

Answers:

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
Answered By: Nikaido

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>
Answered By: Sam Zhang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.