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):
or similar, but I'm curious why the double-clicking functionality of .py and .pyw files does not work...Code:python C:\path\to\my\script.py
The registry entry for opening .py files is
which seems correct to me.. So - basically - wtf?Code:"C:\Python31\python.exe" "%1" %*
________________________
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
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: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() print "Hello World" 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() raw_input("Press ENTER to terminate...")
Weird. As I said before, all scripts run as expected from command line (or when wrapped in a batch script).Code:filename = "C:\\Python\\Scripts\\testfile.txt" # Create a file fileHandle = open(filename, 'w') fileHandle.close()![]()
Last edited by marwex89; 08-09-2009 at 04:15 PM.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
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.//more info on What’s New In Python 3.0 — Python v3.0.1 documentationPEP 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()).
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.
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:
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.. :SCode:print ("Hello world") input("Press ENTER to terminate...")
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
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.
I guess.. Anyway, thank you. Now I can keep fooling around![]()
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks