Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Running Python Script in Terminal

  1. #1
    Join Date
    Apr 2013
    Location
    Michigan
    Beans
    63
    Distro
    Ubuntu 12.04 Precise Pangolin

    Running Python Script in Terminal

    Hello I made a little python script (I'm learning it right now) that I named SecondConverter. I put the
    Code:
    #!/usr/bin/python
    in and everything, I made it executable. But still when I click on it and click run nothing at all happens. What do I do to run it? I can't run it in terminal because it doesn't show the output which I need to see.
    Thanks,
    Bresser

  2. #2
    Join Date
    Sep 2005
    Beans
    1,606
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Running Python Script in Terminal

    Does the program output anything via the print command? In terminal it should show that, otherwise, it'll just run the program when you double click on it.
    Where's [slooksterpsv] been? - I had Catastrophic partition alignment failure. I lost 90%+ of my data, and just now got it back fixed and working.

  3. #3
    Join Date
    Apr 2013
    Location
    Michigan
    Beans
    63
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Running Python Script in Terminal

    Yes it asks the user how many secs to convert into hrs, minutes, and secs. So when I run in terminal it asks the first question I enter and then terminal closes
    Thanks,
    Bresser

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,915
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Running Python Script in Terminal

    Of course... if you start it by click on it, then that's the only process and the windows closes when the program exits, which happens milliseconds after it printed its output. So either you add another read operation after your output so that the program doesn't exit immediately, or you run your python from a terminal, or you beef up your code to use a windowing interface..

  5. #5
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    977
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Running Python Script in Terminal


    You should also consider changing your shebang line to use the standard python environment variable.
    This will ensure that you're scripts still work if the python executable location is changed
    Code:
    #!/usr/bin/env python
    The terminal only closes because it was opened just to run your script, when that finishes it then closes. If you launch your terminal by hand and then execute your script, you will see the output
    Code:
    python ~/path/to/your/script.py
    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  6. #6
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Running Python Script in Terminal

    Quote Originally Posted by Bresser View Post
    Yes it asks the user how many secs to convert into hrs, minutes, and secs. So when I run in terminal it asks the first question I enter and then terminal closes
    If you are running the script from the command line then the terminal should not close under Linux. The application may close but the terminal shouldn't - it sounds like you have an interesting bug - or you are running the command in a strange way.

    When you double click an application icon in linux, it has no idea if that program needs to open a terminal to display output, so it doesn't open one. The actual process is likely to just fail silently as it has nowhere to send it's output.

    This is different to windows which (I think) opens a terminal if the program needs it.

    Can you show us :

    1) Exactly what command you are using to run you application in the terminal

    2) The code of your application.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Running Python Script in Terminal

    Or you can specify
    Code:
    Terminal=true
    in the .desktop file for your application. See Desktop Entry Specification.

  8. #8
    Join Date
    Apr 2013
    Location
    Michigan
    Beans
    63
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Running Python Script in Terminal

    To execute it I'm clicking on it and running from terminal because it says it doesnt exist as for the code inside is:

    Code:
    first_seconds = input("Please Enter Amount of Seconds to Convert")
    hours = first_seconds // 3600
    seconds_after_hours = first_seconds % 3600
    minutes = seconds_after_hours // 60
    seconds = seconds_after_hours % 60
    
    print(hours, "Hours", minutes, "minutes", seconds, "seconds")
    Thanks,
    Bresser

  9. #9
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Running Python Script in Terminal

    Quote Originally Posted by Bresser View Post
    To execute it I'm clicking on it and running from terminal because it says it doesnt exist as for the code inside is:

    Code:
    first_seconds = input("Please Enter Amount of Seconds to Convert")
    hours = first_seconds // 3600
    seconds_after_hours = first_seconds % 3600
    minutes = seconds_after_hours // 60
    seconds = seconds_after_hours % 60
    
    print(hours, "Hours", minutes, "minutes", seconds, "seconds")
    What command are you running to execute this from the terminal ? Is the above the full contents of the file ?
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  10. #10
    Join Date
    Apr 2013
    Location
    Michigan
    Beans
    63
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Running Python Script in Terminal

    im not using a terminal command I am double-clicking on the file and clicking run from terminal
    and no at the top is
    Code:
    #!/usr/bin/env python
    and i already checked to make sure that was the right hash bang
    Thanks,
    Bresser

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •