Python Run help

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
girmofdoom

Python Run help

Post by girmofdoom »

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).
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Python Run help

Post by xenopeek »

"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:
Image
User avatar
dritzominous
Level 3
Level 3
Posts: 161
Joined: Sat Nov 14, 2009 12:49 am

Re: Python Run help

Post by dritzominous »

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
Locked

Return to “Scripts & Bash”