LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2009, 06:27 AM   #1
slideh
LQ Newbie
 
Registered: Jul 2008
Posts: 29

Rep: Reputation: 15
very simple python script question


I am completely new to programming and Linux.

I want to know how to run a script (.py) I've written

At the top of the script I have entered
#!/usr/bin/python

In a shell I have

cd'd to the directory the script is sitting in.
Typed in "python" to invoke the interpretor
then
typed "python linux_python_test.py"
tried "./ linux_python_test.py"
and "linux_python.py"

HAve also tried removing the .py extention and tried those three again.

All the while the same error message crops up....

File "<stdin>", line 1
python linux_python_test.py
^


What am I doing wrong?
 
Old 01-30-2009, 06:33 AM   #2
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
I think you should be able to run the script by simple cd'ing to the directory where the program is and giving command

python script.py

Just to be safe make it executable.
 
Old 01-30-2009, 08:25 AM   #3
slideh
LQ Newbie
 
Registered: Jul 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks LL,
I'm afraid that didn't work.
by the way I changed directory before going into the Python Interpreter.

S

Quote:
Originally Posted by linuxlover.chaitanya View Post
I think you should be able to run the script by simple cd'ing to the directory where the program is and giving command

python script.py

Just to be safe make it executable.
 
Old 01-30-2009, 09:45 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
#!<path> on the first line tells the shell to use a specific interpreter when executing the script. The file must then have the execute bit set: "chmod 775 script.py"

Not specifying it requires you to call the interpreter with the file as an argument (i.e. python script.py)

Try removing the #!... on the first line and simply executing the script as an option to the interpreter: "python script.py"

If you really want to keep the #!, then make sure the path you're specifying is the true path to the interpreter: "which python" will help you with that.
 
Old 01-30-2009, 10:18 AM   #5
tkedwards
Senior Member
 
Registered: Aug 2004
Location: Munich, Germany
Distribution: Opensuse 11.2
Posts: 1,549

Rep: Reputation: 52
Quote:
by the way I changed directory before going into the Python Interpreter.
If you want to execute a python script as an executable file you need to be at the normal Linux command line (bash), not in the python interpreter.
(Of course you could do it from the interpreter as well but you'd need to write code).
So simply go back to your normal Linux command line, cd into the directory where the script is and type 'python scriptname' where script name is the name of the file.
 
Old 01-31-2009, 12:50 AM   #6
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
Have you done that has been suggested to you in earlier posts?
chmod +x script.py
 
Old 02-02-2009, 12:00 PM   #7
slideh
LQ Newbie
 
Registered: Jul 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tkedwards View Post
If you want to execute a python script as an executable file you need to be at the normal Linux command line (bash), not in the python interpreter.
(Of course you could do it from the interpreter as well but you'd need to write code).
So simply go back to your normal Linux command line, cd into the directory where the script is and type 'python scriptname' where script name is the name of the file.

Thank you for all the replies.
This is what I was doing wrong. I was trying to invoke the script though the Python Interpreter
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extremely simple Python question Travis86 Programming 3 01-02-2007 01:50 PM
Simple Python Question peter_89 Programming 3 05-10-2006 12:13 PM
newbie, getting simple python script to work Monjawi Programming 6 10-03-2005 12:51 PM
how do I do this in Python? (simple sh script) johnMG Programming 6 12-22-2004 11:05 PM
simple python function question Tyir Programming 2 03-28-2004 11:53 PM


All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration