sammiwei
sammiwei

Reputation: 3200

2 ways of running python program

I am learning Python now. There are 2 ways of running python in the terminal.

one is python xx.py
another ./xx.py

The first way works for me, but when I am trying to run using the second option, I get

-bash: ./hello.py: Permission denied

I can run the python program one way or another, but I would really like to know why, and what command should use to grant permission to run this using ./

Thanks!

Upvotes: 2

Views: 241

Answers (4)

pycoder112358
pycoder112358

Reputation: 875

First grant 'execute' permission to the file

$ chmod +x filename.py

Then you will be able to run the script:

$ ./filename.py

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

In order to be executable, the script must be granted execute permissions via chmod: chmod +x filename.py or the like.

Upvotes: 6

Wilduck
Wilduck

Reputation: 14116

You'll need to change permissions on the file, to allow it to be executable. In bash:

chmod +755 ./xx.py

Then ./xx.py will work. If it doesn't, you'll need to make sure that you're using a shebang correctly.

Upvotes: 1

synthesizerpatel
synthesizerpatel

Reputation: 28036

This should probably be migrated to unix.stackexchange.com

You need to make sure it has permissions set correctly,chmod 755 hello.py

For interpreted scripts, you need not only to be executable, but readable so that the script interpreter can read the program.

Upvotes: 2

Related Questions