Python Run help

Forum rules
Before you post please read this

Python Run help

Postby girmofdoom on Fri Jul 27, 2012 11:16 pm

I am not sure if this is the right section or not. I couldnt really find a specific one for it, but could someone help me. I am at the beggining of learning python, I created a file with just (only stuff inside the <>,not them itself) < Print "Hello World!" >, i save the file as test.py, i double click it and it gives me options for opening it. the only 2 for running are "Run" and "Run in terminal". The "Run" does nothing when i try it, then i try "Run in terminal" and it only flashes the terminal for a half second then disappears. Could someone please help me. I want to learn python(ive prevoiusly learned HTML/5 and CSS/3).
girmofdoom
Level 1
Level 1
 
Posts: 2
Joined: Thu Jul 26, 2012 4:43 pm

Linux Mint is funded by ads and donations.
 

Re: Python Run help

Postby xenopeek on Sat Jul 28, 2012 3:01 am

"Run" runs the program, but without showing a terminal. This is mostly useful for GUI applications. "Run in Terminal" runs the program and shows its output in a terminal, but the terminal is closed upon completion of the program. You will not see the output as it is done so quick.

Four alternatives to test your Python console programs:

1. Open a terminal yourself, go to the directory where you have your Python file and run it with:
Code: Select all
./test.py

The output will show, and the terminal remains open and waiting for your next command. BTW this works only if you have given the file execute permissions.

2. Make your program wait for a key press before closing, and then just use "Run in Terminal". For Python 3 you can do so by adding the line at the end:
Code: Select all
input("Press Enter to continue...")

or for Python 2:
Code: Select all
raw_input("Press Enter to continue...")

3. Change the configuration of your terminal to not close once the program that is running on it closes. To do so for GNOME Terminal, go to Edit > "Profile Preferences", "Title and Command" tab, and then change "When command exits" to "Hold the terminal open".

4. Use a IDE to write your Python applications, and those usually have the option to run programs from within the IDE and show terminal output.

I just use the first option :wink:
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14829
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Python Run help

Postby dritzominous on Sun Jul 29, 2012 10:36 am

Putting this as the first line in your script might help:

Code: Select all
#!/usr/bin/env python

^ might be different if you're using python3; and it varies based on what OS you're running.

And you might have to edit the permissions to allow the file to execute:

Code: Select all
chmod +x ./text.py

^
must be in the script's directory to do that exact command, unless you specify the file location.
v

Or, if you get lazy and don't want to do that, just:

Code: Select all
python text.py
User avatar
dritzominous
Level 3
Level 3
 
Posts: 160
Joined: Sat Nov 14, 2009 12:49 am


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests