Closed Thread
Results 1 to 5 of 5

Thread: Double-clicking .py files?

  1. #1
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Thumbs down Double-clicking .py files?

    Hi guys. I just started fooling around with Python - just for fun.

    Under Linux it's all good. Under Windows, however, running scripts by double-clicking them does not seem to work (can't expect Windows users to touch a command line, so this is a problem). I can work around this by creating a batch script like this (simplified):

    Code:
    python C:\path\to\my\script.py
    or similar, but I'm curious why the double-clicking functionality of .py and .pyw files does not work...

    The registry entry for opening .py files is

    Code:
    "C:\Python31\python.exe" "%1" %*
    which seems correct to me.. So - basically - wtf?
    ________________________

    EDIT:

    Seems like this depends on the script... Some scripts will run, others not. Don't get me wrong - they will all run fine from the command line.. It's not like I'm making errors or something

    Code:
    print "Hello World"
    
    raw_input("Press ENTER to terminate...")
    The above script does not seem to run. A black screen is flashing (so something obviously happens...), but no hello world. To make sure the script didn't execute (in case the raw_input failed), I expanded it to this:

    Code:
    filename = "C:\\Python\\Scripts\\testfile.txt"
    
    # Create a file
    fileHandle = open(filename, 'w')
    fileHandle.close()
    
    print "Hello World"
    
    raw_input("Press ENTER to terminate...")
    No file created, so it did not execute. Now to the funny part...

    Code:
    filename = "C:\\Python\\Scripts\\testfile.txt"
    
    # Create a file
    fileHandle = open(filename, 'w')
    fileHandle.close()
    
    raw_input("Press ENTER to terminate...")
    This _does_ create a file, but does not wait for the user to press enter. I get the same result as if I did this:

    Code:
    filename = "C:\\Python\\Scripts\\testfile.txt"
    
    # Create a file
    fileHandle = open(filename, 'w')
    fileHandle.close()
    Weird. As I said before, all scripts run as expected from command line (or when wrapped in a batch script).
    Last edited by marwex89; 08-09-2009 at 04:15 PM.
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    psam is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    34
    Rep Power
    10

    Re: Double-clicking .py files?

    Since you're using python 3.1 the problem is with your code.
    The newer versions of python use print as a function and not as a keyword.

    And the raw_input() was renamed to input().

    //from the docs.python.org

    Print Is A Function¶

    The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105). Examples:

    Old: print "The answer is", 2*2
    New: print("The answer is", 2*2)

    Old: print x, # Trailing comma suppresses newline
    New: print(x, end=" ") # Appends a space instead of a newline

    Old: print # Prints a newline
    New: print() # You must call the function!

    Old: print >>sys.stderr, "fatal error"
    New: print("fatal error", file=sys.stderr)

    Old: print (x, y) # prints repr((x, y))
    New: print((x, y)) # Not the same as print(x, y)!

    You can also customize the separator between items, e.g.:

    print("There are <", 2**32, "> possibilities!", sep="")

    which produces:

    There are <4294967296> possibilities!

    Note:

    * The print() function doesn’t support the “softspace” feature of the old print statement. For example, in Python 2.x, print "A\n", "B" would write "A\nB\n"; but in Python 3.0, print("A\n", "B") writes "A\n B\n".
    * Initially, you’ll be finding yourself typing the old print x a lot in interactive mode. Time to retrain your fingers to type print(x) instead!
    * When using the 2to3 source-to-source conversion tool, all print statements are automatically converted to print() function calls, so this is mostly a non-issue for larger projects.
    PEP 3111: raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).
    //more info on What’s New In Python 3.0 &mdash; Python v3.0.1 documentation

    What's happening is that's trying to show you the errors with your code and then closes.

    You must have two installations of python otherwise your scripts wouldn't work under the prompt line. So you should really uninstall one of them .

  4. #3
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Double-clicking .py files?

    Thank you very much! Seems like I must get a newer book, eh? Or just stick with tutorials.. +rep anyway

    The only problem now is that I get the EOFError when I just press ENTER to terminate a script like this:

    Code:
    print ("Hello world")
    
    input("Press ENTER to terminate...")
    How is that usually done? Do you normally just do input() - adding some exception handling, or is there an other common way of doing this? All scripts I've seen have been using raw_input.. :S
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  5. #4
    psam is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    34
    Rep Power
    10

    Re: Double-clicking .py files?

    I actually use python 2.6 so i don't know much about that error.
    You can try using good ol' system("pause") in the os module.

  6. #5
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Double-clicking .py files?

    I guess.. Anyway, thank you. Now I can keep fooling around
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 09-17-2011, 10:34 AM
  2. getting multiple coordinates by clicking an picturebox
    By Tiemer in forum C# Programming
    Replies: 2
    Last Post: 08-11-2011, 09:52 AM
  3. Not Clicking
    By PGP_Protector in forum C# Programming
    Replies: 16
    Last Post: 11-17-2009, 10:58 AM
  4. Clicking an affiliate link?
    By Jordan in forum Technology Ramble
    Replies: 19
    Last Post: 03-26-2009, 09:36 PM
  5. Automatically Clicking URL Buttons
    By Hund34 in forum General Programming
    Replies: 6
    Last Post: 04-22-2008, 09:39 AM

Tags for this Thread

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