LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   very simple python script question (https://www.linuxquestions.org/questions/linux-newbie-8/very-simple-python-script-question-700991/)

slideh 01-30-2009 05:27 AM

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?

linuxlover.chaitanya 01-30-2009 05:33 AM

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.

slideh 01-30-2009 07:25 AM

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 (Post 3426162)
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.


JulianTosh 01-30-2009 08:45 AM

#!<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.

tkedwards 01-30-2009 09:18 AM

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.

linuxlover.chaitanya 01-30-2009 11:50 PM

Have you done that has been suggested to you in earlier posts?
chmod +x script.py

slideh 02-02-2009 11:00 AM

Quote:

Originally Posted by tkedwards (Post 3426338)
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


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