Syntax highlighting and other questions about PTVS

May 3, 2012 at 1:54 AM
Edited May 3, 2012 at 1:56 AM

Hello.

I'm a beginning programmer working from John Zelle's Python Programming textbook. I've used PyScripter for several months, but its constant crashing has made me switch to Visual Studio 2010 Professional with Python Tools.

I noticed that many functions like print, int, etc. do not have the proper syntax highlighting. I watched videos of PTVS where such functions are highlighted in blue (using the default theme), but my functions are highlighted in black. Python Tools seems to be installed properly, and is listed under Tools->Options as "Python Tools." I am using the 64-bit Python 3.2 interpreter in the 32-bit version of Visual Studio 2010 Professional (I think only 32-bit versions are available, though). Import, def, for, etc., are highlighted in blue. Also, unlike in PyScripter, numbers retain their black color. I know that it is possible to change the color of numbers, for example, but not of specific functions, and I think that these problems with syntax highlighting may be symptomatic of larger issues with my install.

When I run a program to test it within the IDE (F5 or Ctrl+F5), a command window pops up, and another window pops up, and then quickly close. It's different from the clean output that I would get in PyScripter. Is there any way to simply have the output appear in the bottom window without these windows always popping up? Also, after the output appears ("Hello World", for example), it is followed by "The thread 'MainThread' (0xcd0) has exited with code 0 (0x0)." (These numbers change depending on the program, however.) Is this normal?

I know that there are many downloadable VS themes, but I'm wondering if it's also possible to change the window colors. I like dark themes, but they clash with the noticeably brighter VS IDE windows (different shades of blue, white background in the "Solution Explorer" window, etc.). I know that Word 2007 has three default colors to choose from, so I'm wondering if the same applies to VS 2010.

Coordinator
May 3, 2012 at 7:42 PM

On vacation so keeping this short… the reason print isn’t highlighted is that it’s not a keyword in Python 3.2, it’s now just a normal function. Likewise int is also just a function so it doesn’t get highlighted. You could switch back to Python 2.x where print is a keyword and as long as the project (or default interpreter) is set correctly it’ll get highlighted correctly.

You should also be able to change the colors of operators in Tools->Options->Fonts and Colors. You can try changing either the normal operator color or I think there might also be a Python operator color registered as well.

For the output you can do Debug->Execute in Interactive which will cause the program to run in the REPL window instead of another process. There are also options in Tools->Options->Python Tools->[forget where] which control whether we wait for input when the program exits abnormally or normally. This will at least let you see what’s flashing by.

No clue on themes in 2010, but Dev11 (the codename for the next VS version) does have new theme support including a dark and light theme. We’ll probably start putting out Dev11 builds of PTVS soonish.

From: booseek [email removed]
Sent: Wednesday, May 02, 2012 5:54 PM
To: Dino Viehland
Subject: Syntax highlighting and other questions about PTVS [pytools:354336]

From: booseek

Hello.

I'm a beginning programmer working from John Zelle's Python Programming textbook. I've used PyScripter for several months, but its constant crashing has made me switch to Visual Studio 2010 Professional with Python Tools.

I noticed that many functions like print, int, etc. do not have the proper syntax highlighting. I watched videos of PTVS where such functions are highlighted in blue (using the default theme), but my functions are highlighted in black. Python Tools seems to be installed properly, and is listed under Tools->Options as "Python Tools." I am using the 64-bit Python 3.2 interpreter in the 32-bit version of Visual Studio 2010 Professional (I think only 32-bit versions are available, though). Import, def, for, etc., are highlighted in blue. Also, unlike in PyScripter, operands, operators, and numbers retain their black color. I know that it is possible to change the color of numbers, for example, but not of specific functions, and I think that these problems with syntax highlighting may be symptomatic of larger issues with my install.

When I run a program to test it within the IDE (F5 or Ctrl+F5), a command window pops up, and another window pops up, and then quickly close. It's different from the clean output that I would get in PyScripter. Is there any way to simply have the output appear in the bottom window without these windows always popping up? Also, after the output appears ("Hello World", for example), it is followed by "The thread 'MainThread' (0xcd0) has exited with code 0 (0x0)." Is this normal?

I know that there are many downloadable VS themes, but I'm wondering if it's also possible to change the window colors. I like dark themes, but they clash with the noticeably brighter VS IDE windows (different shades of blue, white background in the "Solution Explorer" window, etc.). I know that Word 2007 has three default colors to choose from, so I'm wondering if the same applies to VS 2010.

May 4, 2012 at 2:48 AM

Thanks for the reply.

I just download Python 2.7 and confirmed what you say is true. I did not think that it could be my version of Python, as PyScripter highlights these functions as if they are keywords, autocompletes them, etc., while using Python 3.2.3.

I know about the Python Interactive Window; it functions closer to how I want the debugger to perform. I will try to find that option to force those windows to stay open to see what's going on. I take it as this is abnormal, then. Am I supposed to simply get some output in the bottom debugging window for simple 'print' functions, for example, without windows flashing by? Also, is the "The thread 'MainThread'..." output at the end of every program output normal?

Good to know about VS 2011 - I'm looking forward to trying it out once it leaves beta mode.

Coordinator
May 4, 2012 at 3:31 AM

PTVS is optimized for big, long running programs rather than short scripts. The command window pops up because we run python.exe with your script, which is a console mode executable (and hence Windows provides it with a console automatically). If you change your project properties to enable the "Windows Application" option then we will use pythonw.exe and no console is displayed. As Dino said, there is an option to add a "Press any key to exit" prompt to keep the console open, and if you add an "input()" call to your code then you will be able to type into the console.

The second window is probably part of your script, but without knowing what you intend to do, it is hard to know exactly what should happen here.

There is an option "Tee program output to Debug Output window" under Tools-Options-Python Tools-Advanced that will copy anything that is printed to the normal VS output window (View-Output, possibly also Ctrl+Alt+O). However, if you're running a script that calculates something, prints it and then exits, using the Run in Interactive Window command may be a better option - programs that print a lot of output may run noticeably slower if the Tee output option is enabled.

The message about the thread ending is part of Visual Studio - the entire environment is optimized for complex applications, so a lot of the things it does don't really make sense until you're trying to debug (for example) Visual Studio itself, when suddenly every little bit of information is critically important.

VS 11 (years have been dropped, we're back to version numbers again) is definitely better for those who like dark themes, though if there are windows in PTVS now that don't change with the settings in Fonts and Colors then we'll probably need to fix them for VS 11 as well. If you find a window that you can't find a way to change in Fonts and Colors, report it and we'll fix it.

May 5, 2012 at 12:43 AM
Edited May 5, 2012 at 1:34 AM

Thank you for your detailed response.

The command window that zips by is indeed the python.exe command window. The other window that flashes by is the Call Stack window, which shows up in the lower right corner of VS. I was able to force them to remain open by checking "Wait for input when process exits normally" in Tools->Options->Python Tools.

Zooba, when I check off "Windows Application" (found by right-clicking on my project within the Solution Explorer window and selecting "Properties"), F5 makes the Call Stack window flash by, but I do not get any output within the debug window. The output window is then changed from "Debug" to "Build." The Python Interactive window still outputs properly, though. I know that pythonw.exe is called instead of python.exe when the "Windows Application" option is enabled; I wonder why this change causes such behavior.

I have several files open at once and run each one at different times. But VS forces me to select a "Startup File" within my project's properties window, which is the file that is run/debugged no whatever tab I'm running it from. Is there a way to work around this "Startup File" and run each program depending on which file/tab I'm in without pointing to a new Startup File each time? The Interactive Window behaves the same way and needs a Startup File to run from.

I will take another look at all of the window and font color options and see if I can change everything to my liking. I did notice that I cannot seem to find/change the Solution Explorer window, though. Is it possible to change its background and foreground colors?

Coordinator
May 5, 2012 at 1:27 AM

Okay, so the Call Stack window is because VS switches into its debug view and then back into editing view. The same thing should happen regardless of which language you are using, though if you use Ctrl+F5 (run without debugging) instead of just F5 (run with debugging) then VS won't switch (but you don't get breakpoints/exception helpers/etc.).

That sounds like the correct behaviour for changing the Windows Application setting, though it does depend on the behavior of your program. If you are using print()/input() then you probably need to have this setting unselected; if you are using PyQT/wxPython/some other windowing toolkit then you can use this option. (I suggested this when I didn't know that the extra window was the call stack.)

Under the Project menu (or the context menu in Solution Explorer) there are commands to run the current file with or without debugging. By default, these aren't as convenient as pressing F5 (again, VS and PTVS are optimised for projects, rather than scripts) but you can add keyboard shortcuts through Tools-Customize-Keyboard - the command names are Project.StartwithDebugging and Project.StartwithoutDebugging.

And thanks for using PTVS and asking these questions - it helps us sort out which features aren't easily discoverable (or are completely missing).

May 6, 2012 at 7:34 PM

Thanks for all of your help.