Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get linux to automatically run my python script in the Python interpreter?

Tags:

python

linux

I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.

I have a basic script on the filesystem called Standalone.py:

#!/usr/bin/env python3.2

import sys

print("this is a standalone script.")

When I'm at my bash prompt I type $ python3.2 Standalone.py. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.

How do I run such scripts?

Thanks for any help.

update

I changed the permissions of Standalone.py to 755. Then I ran the command:

$ ./Standalone.py

and received the message:

: No such file or directory

I then switched the permissions of Standalone.py back to 644. Then when I ran

$ ./Standalone.py

I received the message

-bash: ./Standalone.py: Permission denied

Is there something I'm missing?

like image 935
quakkels Avatar asked Jun 16 '12 18:06

quakkels


2 Answers

  1. You need to make the script executable using

    chmod +x Standalone.py
    
  2. Usually, the current directory is not searched for executable files, so you need to use

    ./Standalone.py
    

    to tell the shell that the script is in the current directory.

like image 154
Sven Marnach Avatar answered Sep 29 '22 00:09

Sven Marnach


Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.

like image 42
emnoor Avatar answered Sep 29 '22 01:09

emnoor