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?
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?
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>