My Python environment

Hello, this blog has moved to here : http://blog.ericgazoni.me, the blog on WordPress will be kept not to break links, but will not be updated anymore, see you there !

The early days

When I first heard about Python, it was just after the 2.5 release. I heard that one of my customer was using it but I had never seen a line of Python yet. At some point in a project, I needed a Bash script equivalent on Windows, and decided to give Python a try, instead of using Windows BAT files.

I installed it on my workstation, and started reading the (excellent) documentation about the tasks I needed to do.

I’ve used IDLE at first, because, well, it’s shipped with the Windows Python distribution. It has been a very unpleasant experience I must say (although I have learned to appreciate some features IDLE has that are missing in other editors). TKinter is a summary toolkit, the look and feel makes it look like it was written in the 80’s (in fact, it probably was). The main concept of editor/runner mix-in felt also a bit weird at first. I finally returned to my all-time-favorite editor, Notepad++, and ran my scripts from the command line.

Eclipse and Pydev

Later, I (luckily) landed on a new project, and it included a lot of Python. The team in place was using an editor they were not yet familiar with, but hopefully, I already knew quite well: Eclipse. I must say the Pydev extension for Eclipse is one of the greatest blessing you can get when working with Python. It features a lot of interesting features from IDLE:

  • syntax highlighting
  • code completion
  • real-time code inspection (very useful when using a dynamic language)
  • PyLint integration (once you’ve tasted it, you can never work without anymore)
  • smart indentations (you almost don’t have to worry about your indents)
  • unittest integration (although I’ve stopped using it)

Eclipse is an excellent product on its own, I already used it for PHP development for several years and I was happy it was also my customer choice for Python.

However, there was still some occasions Eclipse was not the right tool to use:

  • the integrated console implements the basic shell only
  • executing scripts outside the project path is hard, as well as changing the current directory
  • for 10 line scripts, creating a project is a bit overkill

The revelation: IPython

After a couple of months struggling with Eclipse and Notepad++ & python.exe, I discovered IPython and at last found a way to work on small scripts without the overhead of Eclipse, but with all its interesting features:

  • serves as well as a shell replacement as Python interpreter
  • excellent autocompletion (both for paths and Python code)
  • “magic” functions such as bookmarks, list of currently defined variables (“whos” command)
  • PDB (Python Debugger) integration with IPDB, providing code completion and history to PDB
  • post-mortem debugger (“debug” command after a traceback)
  • quick access to docstrings and source code of almost every library

IPython is packaged inside the Python(x,y) distribution with the Console application, which is a kind of command line emulator for Windows. Once configured with a readable setup, it’s probably the best development environment you can find of on Windows.

My current setup

Applications

I’m pretty sure the Interweb is full of other blogs displaying their own personal setup. Like in everything, it’s pretty much a matter of taste, however I think this setup is one of the most productive you can get on Windows (notice: I don’t do web apps in Python for the moment, perhaps it’s not the best suited for this task, but for desktop and console applications, it’s my favorite).

  • For prototyping small scripts (~10 lines), as well as browsing code, tinkering with new libraries, doing a little math, I use IPython alone
  • When I need to develop a slightly longer script (~50 lines), I usually use Notepad++, and then run the script with IPython, using the “run” command (equivalent to -i option in python CLI)
  • If the script grows to more than one module, then Eclipse comes handy

Windows

I usually write my unit tests under Eclipse, but I always run them through Nose, using the “–pdb” and “–pdb-failure” options, so I can debug directly from the command line, without having to re-run my checks through the Eclipse debug mode, and I find the PDB debugger more efficient than the one in Eclipse. Being able to play with your data without having to use the mouse is a great plus for productivity.
When writing code that always dies at a certain point, doing post-mortem analysis is really useful, and saves a lot of time, mainly if your script takes some time to run. To use this, you just run the script under IPython, then type the “debug” command, and it gives you the latest status of the application just before the crash. You can browse the call stack backward and forward, play with the variables, just like when using PDB (except it’s IPDB then, even better !).

I’m really fond of the dual screen setup: on one screen I keep multiple IPython and cmd tabs in Console, on the other screen I have Eclipse in fullscreen.

Python (x,y)

If you want to get a full working Python environment with Eclipse, Pydev, IPython and Console, all-in-one, I recommend the Python(x,y) distribution.

I also use the Monaco font for Windows (MacOS default font) or the Consolas font (it seems it’s shipped with MS Office 2007) when available.

The only issue I have with Python(x,y) is the awkward default configuration of Console. I have modified it and here is my configuration file:

<?xml version="1.0"?>
<settings>
	<console change_refresh="10" refresh="100" rows="40" columns="120" buffer_rows="500" buffer_columns="0" shell="" init_dir="" save_size="0" start_hidden="0">
		<colors>
			<color id="0" r="0" g="0" b="0"/>
			<color id="1" r="0" g="0" b="128"/>
			<color id="2" r="0" g="150" b="0"/>
			<color id="3" r="0" g="150" b="150"/>
			<color id="4" r="170" g="25" b="25"/>
			<color id="5" r="128" g="0" b="128"/>
			<color id="6" r="128" g="128" b="0"/>
			<color id="7" r="192" g="192" b="192"/>
			<color id="8" r="128" g="128" b="128"/>
			<color id="9" r="0" g="100" b="255"/>
			<color id="10" r="0" g="255" b="0"/>
			<color id="11" r="0" g="255" b="255"/>
			<color id="12" r="255" g="50" b="50"/>
			<color id="13" r="255" g="0" b="255"/>
			<color id="14" r="255" g="255" b="0"/>
			<color id="15" r="255" g="255" b="255"/>
		</colors>
	</console>
	<appearance>
		<font name="Monaco" size="10" bold="0" italic="0" smoothing="2">
			<color use="0" r="0" g="0" b="0"/>
		</font>
		<window title="Console" icon="" use_tab_icon="1" use_console_title="0" show_cmd="0" show_cmd_tabs="0" use_tab_title="1" trim_tab_titles="20"/>
		<controls show_menu="0" show_toolbar="1" show_statusbar="0" show_tabs="1" hide_single_tab="1" show_scrollbars="1"/>
		<styles caption="1" resizable="1" taskbar_button="1" border="1" inside_border="2" tray_icon="0">
			<selection_color r="255" g="255" b="0"/>
		</styles>
		<position x="0" y="0" dock="-1" snap="-1" z_order="0" save_position="0"/>
		<transparency type="0" active_alpha="255" inactive_alpha="255" r="0" g="0" b="0"/>
	</appearance>
	<behavior>
		<copy_paste copy_on_select="1" clear_on_copy="1" no_wrap="1" trim_spaces="1" copy_newline_char="0"/>
		<scroll page_scroll_rows="0"/>
		<tab_highlight flashes="3" stay_highligted="1"/>
	</behavior>
	<hotkeys use_scroll_lock="1">
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="83" command="settings"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="112" command="help"/>
		<hotkey ctrl="0" shift="0" alt="1" extended="0" code="115" command="exit"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="112" command="newtab1"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="113" command="newtab2"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="114" command="newtab3"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="115" command="newtab4"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="116" command="newtab5"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="117" command="newtab6"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="118" command="newtab7"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="119" command="newtab8"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="120" command="newtab9"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="121" command="newtab10"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="49" command="switchtab1"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="50" command="switchtab2"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="51" command="switchtab3"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="52" command="switchtab4"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="53" command="switchtab5"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="54" command="switchtab6"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="55" command="switchtab7"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="56" command="switchtab8"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="57" command="switchtab9"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="48" command="switchtab10"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="9" command="nexttab"/>
		<hotkey ctrl="1" shift="1" alt="0" extended="0" code="9" command="prevtab"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="87" command="closetab"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="82" command="renametab"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="1" code="45" command="copy"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="1" code="46" command="clear_selection"/>
		<hotkey ctrl="1" shift="0" alt="0" extended="0" code="86" command="paste"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="stopscroll"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollrowup"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollrowdown"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollpageup"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollpagedown"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollcolleft"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollcolright"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollpageleft"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="scrollpageright"/>
		<hotkey ctrl="1" shift="1" alt="0" extended="0" code="112" command="dumpbuffer"/>
		<hotkey ctrl="0" shift="0" alt="0" extended="0" code="0" command="activate"/>
	</hotkeys>
	<mouse>
		<actions>
			<action ctrl="1" shift="0" alt="0" button="3" name="copy"/>
			<action ctrl="0" shift="0" alt="0" button="1" name="select"/>
			<action ctrl="0" shift="0" alt="0" button="3" name="paste"/>
			<action ctrl="1" shift="0" alt="0" button="1" name="drag"/>
			<action ctrl="0" shift="0" alt="0" button="2" name="menu"/>
		</actions>
	</mouse>
	<tabs>
		<tab title="Command Window" icon="C:\Program Files\pythonxy\console\cmd.ico">
			<console shell="cmd.exe" init_dir="c:\workspace"/>
			<cursor style="1" r="128" g="128" b="128"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
        <tab title="IPython (sh)" icon="C:\Program Files\pythonxy\console\ipythonsh.ico">
			ipython -q4thread -p sh -editor SciTE.exe" init_dir="c:\workspace"/>
			<cursor style="1" r="1" g="111" b="229"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
        <tab title="IronPython" icon="C:\Program Files\pythonxy\console\ipythonsh.ico">
			ipy -X:AutoIndent -X:ColorfulConsole -X:TabCompletion -X:ShowClrExceptions -X:Frames" init_dir="c:\workspace"/>
			<cursor style="1" r="128" g="128" b="128"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
	</tabs>
</settings>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s