LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2010, 07:15 PM   #1
dexter.gdv
Member
 
Registered: Jul 2010
Location: Vadodara, Gujarat, India
Distribution: ubuntu
Posts: 33

Rep: Reputation: 0
Python on terminal


hello guys,
i am here to ask about the very basic hello world prog, after i create a file with .py extension and run it then on windows it opens up directly in command prompt, but wat to do if i wish to do the same in ubuntu, i.e. i want the .py file to run in terminal as a console program, which does not happen as such.

thank you.
 
Old 09-03-2010, 07:27 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dexter.gdv View Post
hello guys,
i am here to ask about the very basic hello world prog, after i create a file with .py extension and run it then on windows it opens up directly in command prompt, but wat to do if i wish to do the same in ubuntu, i.e. i want the .py file to run in terminal as a console program, which does not happen as such.

thank you.
I don't understand - you want a program that would open a terminal emulator/console ? How are you going to start the program under Ubuntu ?

I.e. if you are already in a terminal emulator and have entered the needed command line to run your Python progran, why do you need another terminal emulator ?

Or you want to click on something and that something should first invoke a terminal emulator and then run in it your Python program ?
 
Old 09-03-2010, 07:34 PM   #3
dexter.gdv
Member
 
Registered: Jul 2010
Location: Vadodara, Gujarat, India
Distribution: ubuntu
Posts: 33

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
I don't understand - you want a program that would open a terminal emulator/console ? How are you going to start the program under Ubuntu ?

I.e. if you are already in a terminal emulator and have entered the needed command line to run your Python progran, why do you need another terminal emulator ?

Or you want to click on something and that something should first invoke a terminal emulator and then run in it your Python program ?
no sir, i wish to just double click on my .py file and want that to run in the terminal for e.g. consider the following codes,

x = raw_input("Enter your name : ")
print x
raw_input("press <enter>")

here, i want the system to ask the 'ENter your name' in terminal, accept the answer from user in terminal and then wait for the user to press the enter key.
the current problem is : when i double click on the.py file i am asked what to do with it ? i have tried all the options, but none allows it to run on terminal, similar to that in xp.
 
Old 09-03-2010, 08:17 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dexter.gdv View Post
... i wish to just double click on my .py file and want that to run in the terminal ...
Double clicking means doing so in a certain file manager, so you have to configure that file manager's file type associations to invoke a terminal and run the needed program in it.

Invoking a terminal which would executed the needed command is easy, for example, if you want '/bin/ls' to be executed in an 'xterm', try this:

Code:
xterm -C -e /bin/ls
or, if you want in the case the terminal to stay,

Code:
xterm -C -e '/bin/ls; /bin/sh'
.

So, now your task is to teach your file manager upon click on Python file to invoke

Code:
xterm -C -e /path/to/your/file.py
.
 
Old 09-04-2010, 07:01 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Does your Python file have executable permissions?

I didn't use GNOME for a long time, but AFAIR it asked you if you want to run it, run it in a terminal, or open it in a text editor if it's executable.
 
Old 09-04-2010, 07:52 PM   #6
dexter.gdv
Member
 
Registered: Jul 2010
Location: Vadodara, Gujarat, India
Distribution: ubuntu
Posts: 33

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Double clicking means doing so in a certain file manager, so you have to configure that file manager's file type associations to invoke a terminal and run the needed program in it.

Invoking a terminal which would executed the needed command is easy, for example, if you want '/bin/ls' to be executed in an 'xterm', try this:

Code:
xterm -C -e /bin/ls
or, if you want in the case the terminal to stay,

Code:
xterm -C -e '/bin/ls; /bin/sh'
.

So, now your task is to teach your file manager upon click on Python file to invoke

Code:
xterm -C -e /path/to/your/file.py
.

thank you for your co-operation sir.
hello, sir, i tried the above with my filemanager and the .py file, but still i can't do it, instead to run the program now in the terminal i use the following command

python filename.py

and the program starts its execution from the next line in the terminal.

sir, i would like you to tell me that is the 'xterm' and the 'terminal' the same or different programs.
I know its a noobs questions, but i am not so experienced in linux so i expect ur co-operation.

thanking you.
 
Old 09-04-2010, 08:02 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dexter.gdv View Post
...
python filename.py

and the program starts its execution from the next line in the terminal.

sir, i would like you to tell me that is the 'xterm' and the 'terminal' the same or different programs.
I know its a noobs questions, but i am not so experienced in linux so i expect ur co-operation.

thanking you.
Did you make your Python file executable ? I.e. did you run

chmod +x filename.py

?

What is the very first line in your 'filename.py' file ?

...

'xterm' is a terminal emulator - one of many existing under UNIXish systems. Install it if you don't have it yet. Hard to believe Ubuntu doesn't have.
 
Old 09-04-2010, 08:10 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Ubuntu uses GNOME Terminal.
 
Old 09-04-2010, 08:14 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Ubuntu uses GNOME Terminal.
I don't know how to start Gnome terminal with a command - like in my example with 'xterm'. I don't use Gnome terminal.

I suggested the OP the sure way I know. If/when he succeeds, and if Gnome terminal allows such execution (most likely it does), the OP then will switch to Gnome terminal.
 
Old 09-04-2010, 08:15 PM   #10
dexter.gdv
Member
 
Registered: Jul 2010
Location: Vadodara, Gujarat, India
Distribution: ubuntu
Posts: 33

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Did you make your Python file executable ? I.e. did you run

chmod +x filename.py

?

What is the very first line in your 'filename.py' file ?

...

'xterm' is a terminal emulator - one of many existing under UNIXish systems. Install it if you don't have it yet. Hard to believe Ubuntu doesn't have.


yes sir, i got the xterm, ubuntu has an xterm in it.
and yes my file has executable permissions.
and it runs only in python shell when i select to display it,but not in xterm or terminal.

the codes of the file are as follows :

first line : x = raw_input("Enter your name : ")
second line : print x
third line : raw_input("press <enter>")
 
Old 09-04-2010, 08:26 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dexter.gdv View Post
...
first line : x = raw_input("Enter your name : ")
...
OK, read http://en.wikipedia.org/wiki/Shebang_(Unix) and make sure your first line has that "shebang notation" with the correct path to Python interpreter.

Then try to run your script as

/full/path/to/your/file.py

- of course, use the real full path, not what I wrote. If/when it work, we'll get back to your file manager.
 
Old 09-04-2010, 08:42 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Probably this: http://www.linuxquestions.org/questi...etting-464729/ will help you to configure file associations. I am not a Gnome guy, so I need to perform WEB search to the same extent as you.
 
Old 09-04-2010, 08:43 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
http://docs.sun.com/app/docs/doc/817...dl?l=zh&a=view
http://developers.sun.com/solaris/ar...ing_gnome.html

Last edited by Sergei Steshenko; 09-04-2010 at 08:45 PM.
 
1 members found this post helpful.
Old 09-04-2010, 08:54 PM   #14
dexter.gdv
Member
 
Registered: Jul 2010
Location: Vadodara, Gujarat, India
Distribution: ubuntu
Posts: 33

Original Poster
Rep: Reputation: 0
thank you sir, thanks a lot for all the support, i ll read,search and try to figure it out.
same to MTK358.
thanks again.
 
Old 09-04-2010, 08:56 PM   #15
dexter.gdv
Member
 
Registered: Jul 2010
Location: Vadodara, Gujarat, India
Distribution: ubuntu
Posts: 33

Original Poster
Rep: Reputation: 0
sir,can u please tell me wat is the unless construct, as m still a beginner in programming.
is it a constructor as in c++, if yes then wat does it do ?
 
  


Reply



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
mysterious discrepancy b/w script results at terminal and in python script cedardoc Linux - Newbie 1 08-03-2010 03:59 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
Python Terminal Gui Kedelfor Programming 2 10-24-2005 03:50 PM
Prevent Terminal Echo Using Python (for Passwords) taylor_venable Programming 4 07-05-2005 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 PM.

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
Open Source Consulting | Domain Registration