<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Mouse Vs. The Python &#187; Windows</title>
	<atom:link href="http://www.blog.pythonlibrary.org/category/windows/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.pythonlibrary.org</link>
	<description>Python Programming from the Frontlines</description>
	<lastBuildDate>Sun, 05 Sep 2010 16:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A bbfreeze Tutorial &#8211; Build a Binary Series!</title>
		<link>http://www.blog.pythonlibrary.org/2010/08/19/a-bbfreeze-tutorial-build-a-binary-series/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/08/19/a-bbfreeze-tutorial-build-a-binary-series/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 13:23:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Packaging]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[bbfreeze]]></category>
		<category><![CDATA[binaries]]></category>
		<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Distribution]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1137</guid>
		<description><![CDATA[The bbfreeze package also allows us to create binaries, but only on Linux and Windows. It&#8217;s just an easy_install away, so if you plan on following along with the examples in the article, you should go get it. The bbfreeze package includes egg support, so it can include egg dependencies in your binary, unlike py2exe. [...]]]></description>
			<content:encoded><![CDATA[<p>The bbfreeze package also allows us to create binaries, but only on Linux and Windows. It&#8217;s just an easy_install away, so if you plan on following along with the examples in the article, you should go get it. The bbfreeze package includes egg support, so it can include egg dependencies in your binary, unlike py2exe. You can also freeze multiple scripts at once, include the Python interpreter and more. According to bbfreeze&#8217;s PyPI entry, it&#8217;s only been tested with Python 2.4-2.5, so keep that in mind. However, I was able to use it with Python 2.6 with no obvious problems.<span id="more-1137"></span></p>
<h2>Getting Started with bbfreeze</h2>
<p>You can use easy_install to download and install bbfreeze or you can just download its source or the egg file directly from the <a href="http://pypi.python.org/pypi/bbfreeze/">Python Package Index</a> (PyPI). In this article, we&#8217;ll try using it on a simple configuration file generator script and we&#8217;ll also try it against a lame wxPython program. My test machine was a Windows 7 Home Edition 32-bit laptop with bbfreeze 0.96.5, and Python 2.6.4. Let&#8217;s start with the configuration script:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># config_1.py</span>
<span style="color: #ff7700;font-weight:bold;">import</span> configobj
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> createConfig<span style="color: black;">&#40;</span>configFile<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Create the configuration file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    config = configobj.<span style="color: black;">ConfigObj</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    inifile = configFile
    config.<span style="color: black;">filename</span> = inifile
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'server'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;http://www.google.com&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'username'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;mike&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'password'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;dingbat&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'update interval'</span><span style="color: black;">&#93;</span> = <span style="color: #ff4500;">2</span>
    config.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getConfig<span style="color: black;">&#40;</span>configFile<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Open the config file and return a configobj
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> configobj.<span style="color: black;">ConfigObj</span><span style="color: black;">&#40;</span>configFile<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> createConfig2<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Create a config file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    config = configobj.<span style="color: black;">ConfigObj</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    config.<span style="color: black;">filename</span> = path
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;product&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;Sony PS3&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;accessories&quot;</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'controller'</span>, <span style="color: #483d8b;">'eye'</span>, <span style="color: #483d8b;">'memory stick'</span><span style="color: black;">&#93;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;retail price&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;$400&quot;</span>
    config.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    createConfig2<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sampleConfig2.ini&quot;</span><span style="color: black;">&#41;</span></pre>
<p>This script has a couple of functions that are pointless, but we&#8217;ll leave them in for illustrative purposes. According to the bbfreeze documentation, we should be able to create a binary with the following string typed into the command line:</p>
<p><code><br />
bb-freeze config_1.py<br />
</code></p>
<p>This assumes that you have &#8220;C:\Python26\Scripts&#8221; on your path. If you don&#8217;t, you&#8217;ll need to type the complete path out (i.e. &#8220;C:\Python26\Scripts\bb-freeze config_1.py&#8221;). When I ran this, I got an error. Here it is:</p>
<pre class="python">Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\S</span>cripts<span style="color: #000099; font-weight: bold;">\b</span>b-freeze-script.py&quot;</span>, line <span style="color: #ff4500;">8</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
    load_entry_point<span style="color: black;">&#40;</span><span style="color: #483d8b;">'bbfreeze==0.96.5'</span>, <span style="color: #483d8b;">'console_scripts'</span>, <span style="color: #483d8b;">'bb-freeze'</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\l</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages<span style="color: #000099; font-weight: bold;">\b</span>bfreeze-0.96.5-py2.6-win32.egg<span style="color: #000099; font-weight: bold;">\b</span>bfreeze<span style="color: #000099; font-weight: bold;">\_</span>_init__.py&quot;</span>, line <span style="color: #ff4500;">18</span>, <span style="color: #ff7700;font-weight:bold;">in</span> main
    f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\l</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages<span style="color: #000099; font-weight: bold;">\b</span>bfreeze-0.96.5-py2.6-win32.egg<span style="color: #000099; font-weight: bold;">\b</span>bfreeze<span style="color: #000099; font-weight: bold;">\f</span>reezer.py&quot;</span>, line <span style="color: #ff4500;">474</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #0000cd;">__call__</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">addModule</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;encodings.*&quot;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\l</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages<span style="color: #000099; font-weight: bold;">\b</span>bfreeze-0.96.5-py2.6-win32.egg<span style="color: #000099; font-weight: bold;">\b</span>bfreeze<span style="color: #000099; font-weight: bold;">\f</span>reezer.py&quot;</span>, line <span style="color: #ff4500;">411</span>, <span style="color: #ff7700;font-weight:bold;">in</span> addModule
    <span style="color: #008000;">self</span>.<span style="color: black;">mf</span>.<span style="color: black;">import_hook</span><span style="color: black;">&#40;</span>name<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">-2</span><span style="color: black;">&#93;</span>, fromlist=<span style="color: #483d8b;">&quot;*&quot;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\l</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages<span style="color: #000099; font-weight: bold;">\b</span>bfreeze-0.96.5-py2.6-win32.egg<span style="color: #000099; font-weight: bold;">\b</span>bfreeze<span style="color: #000099; font-weight: bold;">\m</span>odulegraph<span style="color: #000099; font-weight: bold;">\m</span>odulegraph.py&quot;</span>, line <span style="color: #ff4500;">256</span>, <span style="color: #ff7700;font-weight:bold;">in</span> import_hook
    modules.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">ensure_fromlist</span><span style="color: black;">&#40;</span>m, fromlist<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\l</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages<span style="color: #000099; font-weight: bold;">\b</span>bfreeze-0.96.5-py2.6-win32.egg<span style="color: #000099; font-weight: bold;">\b</span>bfreeze<span style="color: #000099; font-weight: bold;">\m</span>odulegraph<span style="color: #000099; font-weight: bold;">\m</span>odulegraph.py&quot;</span>, line <span style="color: #ff4500;">345</span>, <span style="color: #ff7700;font-weight:bold;">in</span> ensure_fromlist
    fromlist.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">find_all_submodules</span><span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\l</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages<span style="color: #000099; font-weight: bold;">\b</span>bfreeze-0.96.5-py2.6-win32.egg<span style="color: #000099; font-weight: bold;">\b</span>bfreeze<span style="color: #000099; font-weight: bold;">\m</span>odulegraph<span style="color: #000099; font-weight: bold;">\m</span>odulegraph.py&quot;</span>, line <span style="color: #ff4500;">369</span>, <span style="color: #ff7700;font-weight:bold;">in</span> find_all_submodules
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>path, mode, typ<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> ifilter<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, imap<span style="color: black;">&#40;</span>moduleInfoForPath, names<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
<span style="color: #008000;">NameError</span>: <span style="color: #ff7700;font-weight:bold;">global</span> name <span style="color: #483d8b;">'ifilter'</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> defined</pre>
<p>It would seem that the &#8220;modulegraph.py&#8221; file is missing an import from the <a href="http://docs.python.org/library/itertools.html">itertools</a> library, which is included with Python. I edited my copy of &#8220;modulegraph.py&#8221; to include the following line at the top of the file: &#8220;from itertools import ifilter&#8221;. This got rid of that traceback, but it raised another because &#8220;imap&#8221; wasn&#8217;t defined either. To fix the second error, I changed my import to &#8220;from itertools import ifilter,imap&#8221; and then it ran with no problems and produced a binary in a &#8220;dist&#8221; folder along with nine other files (see screenshot below).</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/bbfreeze_dir.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/bbfreeze_dir.png" alt="bbfreeze_dir.png" title="bbfreeze_dir.png" width="599" height="324" class="aligncenter size-full wp-image-1148" /></a></p>
<h2>Using bbfreeze&#8217;s Advanced Configuration</h2>
<p>The PyPI page for bbfreeze (which is also its home page) has very little documentation. However, the page does say that the preferred way to use bbfreeze is with little scripts. We&#8217;re going to try using creating a binary with the lame wxPython, mentioned earlier. Here&#8217;s the wx code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DemoPanel<span style="color: black;">&#40;</span>wx.<span style="color: black;">Panel</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Panel</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>
&nbsp;
        labels = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Name&quot;</span>, <span style="color: #483d8b;">&quot;Address&quot;</span>, <span style="color: #483d8b;">&quot;City&quot;</span>, <span style="color: #483d8b;">&quot;State&quot;</span>, <span style="color: #483d8b;">&quot;Zip&quot;</span>,
                  <span style="color: #483d8b;">&quot;Phone&quot;</span>, <span style="color: #483d8b;">&quot;Email&quot;</span>, <span style="color: #483d8b;">&quot;Notes&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
        mainSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Please enter your information here:&quot;</span><span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> lbl <span style="color: #ff7700;font-weight:bold;">in</span> labels:
            sizer = <span style="color: #008000;">self</span>.<span style="color: black;">buildControls</span><span style="color: black;">&#40;</span>lbl<span style="color: black;">&#41;</span>
            mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sizer, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">EXPAND</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>mainSizer<span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Layout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> buildControls<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
        size = <span style="color: black;">&#40;</span><span style="color: #ff4500;">80</span>,<span style="color: #ff4500;">40</span><span style="color: black;">&#41;</span>
        font = wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span>
&nbsp;
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=label, size=size<span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> label <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;Notes&quot;</span>:
            txt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name=label<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            txt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, style=wx.<span style="color: black;">TE_MULTILINE</span>, name=label<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>txt, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> sizer
&nbsp;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DemoFrame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Frame that holds all other widgets
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>,
                          <span style="color: #483d8b;">&quot;cxFreeze Tutorial&quot;</span>,
                          size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">600</span>,<span style="color: #ff4500;">400</span><span style="color: black;">&#41;</span>
                          <span style="color: black;">&#41;</span>
        panel = DemoPanel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">App</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    frame = DemoFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Now let&#8217;s create a simple freezing script!</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># bb_setup.py</span>
<span style="color: #ff7700;font-weight:bold;">from</span> bbfreeze <span style="color: #ff7700;font-weight:bold;">import</span> Freezer
&nbsp;
f = Freezer<span style="color: black;">&#40;</span>distdir=<span style="color: #483d8b;">&quot;bb-binary&quot;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">addScript</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sampleApp.py&quot;</span><span style="color: black;">&#41;</span>
f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>First off, we import the <em>Freezer</em> class from the bbfreeze package. Freezer accepts three arguments: a destination folder, an includes iterable and an excludes iterable (i.e. a tuple or list). Just to see how well bbfreeze works with only its defaults, we leave out the includes and excludes tuples/lists. Once you have a Freezer object, you can add your script(s) by calling the Freezer object name&#8217;s <em>addScript</em> method. Then you just need to call the object (i.e.  f() ). If you do all that, you should end up with a 14.5 MB folder holding 18 files. When I ran the sampleApp.exe file, it ran just fine and was properly themed, however it also had a console screen. To figure out the correct syntax, I used <a href="http://code.google.com/p/gui2exe/">GUI2Exe</a>. Here&#8217;s the new code:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># bb_setup2.py</span>
<span style="color: #ff7700;font-weight:bold;">from</span> bbfreeze <span style="color: #ff7700;font-weight:bold;">import</span> Freezer
&nbsp;
includes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
excludes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'_gtkagg'</span>, <span style="color: #483d8b;">'_tkagg'</span>, <span style="color: #483d8b;">'bsddb'</span>, <span style="color: #483d8b;">'curses'</span>, <span style="color: #483d8b;">'email'</span>, <span style="color: #483d8b;">'pywin.debugger'</span>,
            <span style="color: #483d8b;">'pywin.debugger.dbgcon'</span>, <span style="color: #483d8b;">'pywin.dialogs'</span>, <span style="color: #483d8b;">'tcl'</span>,
            <span style="color: #483d8b;">'Tkconstants'</span>, <span style="color: #483d8b;">'Tkinter'</span><span style="color: black;">&#93;</span>
&nbsp;
bbFreeze_Class = Freezer<span style="color: black;">&#40;</span><span style="color: #483d8b;">'dist'</span>, includes=includes, excludes=excludes<span style="color: black;">&#41;</span>
&nbsp;
bbFreeze_Class.<span style="color: black;">addScript</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sampleApp.py&quot;</span>, gui_only=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
bbFreeze_Class.<span style="color: black;">use_compression</span> = <span style="color: #ff4500;">0</span>
bbFreeze_Class.<span style="color: black;">include_py</span> = <span style="color: #008000;">True</span>
bbFreeze_Class<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>If you run this, you should end up with a &#8220;dist&#8221; folder that contains 18 files and is 16.6 MB in size. Notice that we added a second argument to the <em>addScript</em> method: gui_only=True. This makes that annoying console go away. We also set compression to zero (no compression, I think) and include the Python interpreter. Turning on compression only reduced the result down to 14.3 MB though. </p>
<p>The bbfreeze package also handles &#8220;recipes&#8221; and includes several examples, however they are not documented well either and I couldn&#8217;t figure out how to include them with my current examples. Feel free to figure them out on your own!</p>
<h2>Wrapping Up</h2>
<p>Well, now you should know the basics of using bbfreeze to create binaries from your programs. I hope you found this helpful. The last article in this series will be on GUI2Exe. Look for it soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/08/19/a-bbfreeze-tutorial-build-a-binary-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A PyInstaller Tutorial &#8211; Build a Binary Series!</title>
		<link>http://www.blog.pythonlibrary.org/2010/08/10/a-pyinstaller-tutorial-build-a-binary-series/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/08/10/a-pyinstaller-tutorial-build-a-binary-series/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 01:36:13 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Packaging]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[binaries]]></category>
		<category><![CDATA[Distribution]]></category>
		<category><![CDATA[PyInstaller]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1102</guid>
		<description><![CDATA[In our previous article on building binaries, we learned a little about py2exe. This time around, we will be focusing our collective attention on the ins and outs of PyInstaller. We&#8217;ll use the same lame wxPython script from the last article for one of our examples, but we&#8217;ll also try a normal console script to [...]]]></description>
			<content:encoded><![CDATA[<p>In our previous article on building binaries, we learned a little about py2exe. This time around, we will be focusing our collective attention on the ins and outs of PyInstaller. We&#8217;ll use the same lame wxPython script from the last article for one of our examples, but we&#8217;ll also try a normal console script to see what the differences are, if any. In case you didn&#8217;t know, PyInstaller works on Linux, Windows and Mac (experimental) and works with Python 1.5-2.6 (except on Windows, where there&#8217;s a caveat for 2.6 &#8211; see below). PyInstaller supports code-signing (Windows), eggs, hidden imports, single executable, single directory, and lots more!<span id="more-1102"></span></p>
<h2>Getting Started with PyInstaller</h2>
<p><strong>Note on Python 2.6 on Windows: </strong>If you read the PyInstaller <a href="http://www.pyinstaller.org/wiki/Python26Win">website </a>closely, you will see a warning about Python 2.6+ not being fully supported. The note says that you will currently need to have the Microsoft CRT installed for your executable to work. What this is probably referring to is the side-by-side assemblies / manifest issues that were introduced with Python 2.6 vis-a-vis Microsoft Visual Studio 2008. We already mentioned this problem in the first article. If you don&#8217;t know anything about it, please check out the py2exe website, the wxPython wiki or Google for the issue.</p>
<p>Anyway, let&#8217;s get on with the show. After you download PyInstaller, you just unzip the archive somewhere convenient. The follow these three simple steps:</p>
<ol>
<li>Run <strong>Configure.py</strong> to save some basic configuration data to a &#8220;.dat&#8221; file. This saves some time since PyInstaller won&#8217;t have to recalculate the configuration on the fly.</li>
<li>Run the following command on the command line:  <em>python Makespec.py [opts] <scriptname></em> where scriptname is the name of the main Python file you use to run your program.</li>
<li>Finally, run the following command via the command line:  <em>python Build.py specfile</em> to build your executable.</li>
</ol>
<p>Let&#8217;s run through this with a real script now. We&#8217;ll start with a simple console script that creates a faux configuration file. Here&#8217;s the code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> configobj
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> createConfig<span style="color: black;">&#40;</span>configFile<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Create the configuration file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    config = configobj.<span style="color: black;">ConfigObj</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    inifile = configFile
    config.<span style="color: black;">filename</span> = inifile
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'server'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;http://www.google.com&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'username'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;mike&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'password'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;dingbat&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'update interval'</span><span style="color: black;">&#93;</span> = <span style="color: #ff4500;">2</span>
    config.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getConfig<span style="color: black;">&#40;</span>configFile<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Open the config file and return a configobj
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> configobj.<span style="color: black;">ConfigObj</span><span style="color: black;">&#40;</span>configFile<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> createConfig2<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Create a config file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    config = configobj.<span style="color: black;">ConfigObj</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    config.<span style="color: black;">filename</span> = path
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;product&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;Sony PS3&quot;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;accessories&quot;</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'controller'</span>, <span style="color: #483d8b;">'eye'</span>, <span style="color: #483d8b;">'memory stick'</span><span style="color: black;">&#93;</span>
    config<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Sony&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;retail price&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;$400&quot;</span>
    config.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    createConfig2<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sampleConfig.ini&quot;</span><span style="color: black;">&#41;</span></pre>
<p>Now let&#8217;s make a spec file:</p>
<p><code><br />
c:\Python25\python c:\Users\Mike\Desktop\pyinstaller-1.4\Makespec.py config_1.py<br />
</code></p>
<p>On my test machine, I have 3 different Python versions installed, so I had to specify the Python 2.5 path explicitly (or set Python 2.5 as the default). Anyway, this should create a file similar to the following (which was named &#8220;config_1.spec&#8221;):</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># -*- mode: python -*-</span>
a = Analysis<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>HOMEPATH,<span style="color: #483d8b;">'support<span style="color: #000099; font-weight: bold;">\\</span>_mountzlib.py'</span><span style="color: black;">&#41;</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>HOMEPATH,<span style="color: #483d8b;">'support<span style="color: #000099; font-weight: bold;">\\</span>useUnicode.py'</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'config_1.py'</span><span style="color: black;">&#93;</span>,
             pathex=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\\</span>Users<span style="color: #000099; font-weight: bold;">\\</span>Mike<span style="color: #000099; font-weight: bold;">\\</span>Desktop<span style="color: #000099; font-weight: bold;">\\</span>py2exe_ex'</span>, r<span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\P</span>ython26<span style="color: #000099; font-weight: bold;">\L</span>ib<span style="color: #000099; font-weight: bold;">\s</span>ite-packages'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
pyz = PYZ<span style="color: black;">&#40;</span>a.<span style="color: black;">pure</span><span style="color: black;">&#41;</span>
exe = EXE<span style="color: black;">&#40;</span>pyz,
          a.<span style="color: black;">scripts</span>,
          exclude_binaries=<span style="color: #ff4500;">1</span>,
          name=<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'build<span style="color: #000099; font-weight: bold;">\\</span>pyi.win32<span style="color: #000099; font-weight: bold;">\\</span>config_1'</span>, <span style="color: #483d8b;">'config_1.exe'</span><span style="color: black;">&#41;</span>,
          debug=<span style="color: #008000;">False</span>,
          strip=<span style="color: #008000;">False</span>,
          upx=<span style="color: #008000;">True</span>,
          console=<span style="color: #008000;">True</span> <span style="color: black;">&#41;</span>
coll = COLLECT<span style="color: black;">&#40;</span> exe,
               a.<span style="color: black;">binaries</span>,
               a.<span style="color: black;">zipfiles</span>,
               a.<span style="color: black;">datas</span>,
               strip=<span style="color: #008000;">False</span>,
               upx=<span style="color: #008000;">True</span>,
               name=<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'pyInstDist2'</span>, <span style="color: #483d8b;">'config_1'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
<p>For the Python script we&#8217;re using, we need to add an explicit path to the location of <em>configobj.py</em> in the <em>Analysis </em>section of the spec file in the <em>pathex</em> parameter. If you do not do this, when you run the resulting executable, it will open and close a console window really fast and you won&#8217;t be able to tell what it says unless you run the exe from the command line. I did the latter to find out what was wrong and discovered it could not find the configobj module. You can also specify the output path for your exe in the COLLECT function&#8217;s name parameter. In this case, we put PyInstaller&#8217;s output in the &#8220;config_1&#8243; subfolder of &#8220;pyInstDist2&#8243;, which should be a folder alongside your original script. There are a ton of options when configuring your spec file, which you can read about <a href="http://www.pyinstaller.org/export/latest/tags/1.4/doc/Manual.html?format=raw#configuring-your-pyinstaller-setup">here</a>.</p>
<p>To build the executable based on the spec file, do the following on the command line:</p>
<p><code><br />
c:\Python25\python c:\Users\Mike\Desktop\pyinstaller-1.4\Build.py config_1.spec<br />
</code></p>
<p>On my machine, I ended up with a folder that had 25 files in it that totaled 6.7 MB. You should be able to reduce the size using the Analysis section&#8217;s <em>excludes</em> parameter and/or compression.</p>
<h2>PyInstaller and wxPython</h2>
<p>Now let&#8217;s try creating a binary from a simple wxPython script. Here&#8217;s the Python script:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DemoPanel<span style="color: black;">&#40;</span>wx.<span style="color: black;">Panel</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Panel</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>
&nbsp;
        labels = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Name&quot;</span>, <span style="color: #483d8b;">&quot;Address&quot;</span>, <span style="color: #483d8b;">&quot;City&quot;</span>, <span style="color: #483d8b;">&quot;State&quot;</span>, <span style="color: #483d8b;">&quot;Zip&quot;</span>,
                  <span style="color: #483d8b;">&quot;Phone&quot;</span>, <span style="color: #483d8b;">&quot;Email&quot;</span>, <span style="color: #483d8b;">&quot;Notes&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
        mainSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Please enter your information here:&quot;</span><span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> lbl <span style="color: #ff7700;font-weight:bold;">in</span> labels:
            sizer = <span style="color: #008000;">self</span>.<span style="color: black;">buildControls</span><span style="color: black;">&#40;</span>lbl<span style="color: black;">&#41;</span>
            mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sizer, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">EXPAND</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>mainSizer<span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Layout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> buildControls<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
        size = <span style="color: black;">&#40;</span><span style="color: #ff4500;">80</span>,<span style="color: #ff4500;">40</span><span style="color: black;">&#41;</span>
        font = wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span>
&nbsp;
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=label, size=size<span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> label <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;Notes&quot;</span>:
            txt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name=label<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            txt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, style=wx.<span style="color: black;">TE_MULTILINE</span>, name=label<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>txt, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> sizer
&nbsp;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DemoFrame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Frame that holds all other widgets
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>,
                          <span style="color: #483d8b;">&quot;PyInstaller Tutorial&quot;</span>,
                          size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">600</span>,<span style="color: #ff4500;">400</span><span style="color: black;">&#41;</span>
                          <span style="color: black;">&#41;</span>
        panel = DemoPanel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">App</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    frame = DemoFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Since this is a GUI, we&#8217;ll create our spec file slightly differently:</p>
<p><code><br />
c:\Python25\python c:\Users\Mike\Desktop\pyinstaller-1.4\Makespec.py -F -w sampleApp.py<br />
</code></p>
<p>Note the -F and -w parameters. The -F command tells PyInstaller to create just one executable whereas the -w tells PyInstaller to hide the console window. Here&#8217;s the resulting spec file:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># -*- mode: python -*-</span>
a = Analysis<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>HOMEPATH,<span style="color: #483d8b;">'support<span style="color: #000099; font-weight: bold;">\\</span>_mountzlib.py'</span><span style="color: black;">&#41;</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>HOMEPATH,<span style="color: #483d8b;">'support<span style="color: #000099; font-weight: bold;">\\</span>useUnicode.py'</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'sampleApp.py'</span><span style="color: black;">&#93;</span>,
             pathex=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\\</span>Users<span style="color: #000099; font-weight: bold;">\\</span>Mike<span style="color: #000099; font-weight: bold;">\\</span>Desktop<span style="color: #000099; font-weight: bold;">\\</span>py2exe_ex'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
pyz = PYZ<span style="color: black;">&#40;</span>a.<span style="color: black;">pure</span><span style="color: black;">&#41;</span>
exe = EXE<span style="color: black;">&#40;</span> pyz,
          a.<span style="color: black;">scripts</span>,
          a.<span style="color: black;">binaries</span>,
          a.<span style="color: black;">zipfiles</span>,
          a.<span style="color: black;">datas</span>,
          name=<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'pyInstDist'</span>, <span style="color: #483d8b;">'sampleApp.exe'</span><span style="color: black;">&#41;</span>,
          debug=<span style="color: #008000;">False</span>,
          strip=<span style="color: #008000;">False</span>,
          upx=<span style="color: #008000;">True</span>,
          console=<span style="color: #008000;">False</span> <span style="color: black;">&#41;</span></pre>
<p>Note that the last line has the &#8220;console&#8221; parameter set to &#8220;False&#8221;. If you build this like you did with the console script you should end up with one file in the &#8220;pyInstDist&#8221; folder that is approximately 7.1 MB in size. </p>
<h2>Wrapping Up</h2>
<p>This ends our quick tour of PyInstaller. I hope you found this helpful in your Python binary-making endeavors. There is much more information on the PyInstaller website and it&#8217;s pretty well documented, although the website is pretty plain. Be sure to give it a try and see just how much easy PyInstaller is to use!</p>
<p><em>Note: I tested all this using PyInstaller 1.4 and Python 2.5 on Windows 7 Home Premium (32-bit).</em></p>
<h2>Further Reading</h2>
<ul>
<li>PyInstaller Official <a href="http://www.pyinstaller.org">Website</a>, <a href="http://www.pyinstaller.org/export/latest/tags/1.4/doc/Manual.html?format=raw">Manual</a>, <a href="http://groups-beta.google.com/group/PyInstaller?pli=1">Mailing List</a></li>
<li>First <a href="http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/">article </a>in the &#8220;Build a Binary Series&#8221;</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/08/10/a-pyinstaller-tutorial-build-a-binary-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A py2exe tutorial &#8211; Build a Binary Series!</title>
		<link>http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 19:52:09 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Packaging]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[binaries]]></category>
		<category><![CDATA[py2exe]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[wxPython]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1080</guid>
		<description><![CDATA[I received a request to create an article on how to use py2exe and wxPython to create an executable. I d, ecided to do a series on packaging instead. It is my intention to go over the major Windows binary building utilities and show you, dear reader, how to use them to create a binary [...]]]></description>
			<content:encoded><![CDATA[<p>I received a request to create an article on how to use py2exe and wxPython to create an executable. I d, ecided to do a series on packaging instead. It is my intention to go over the major Windows binary building utilities and show you, dear reader, how to use them to create a binary that you can distribute. Once those articles are done, I&#8217;ll show how to use Inno and NSIS. To kick things off, we&#8217;ll go over how to use py2exe, probably the most popular of the Windows executable packages.<span id="more-1080"></span></p>
<h2>Let&#8217;s Get Started</h2>
<p>For this tutorial, we&#8217;re going to use a wxPython script that doesn&#8217;t do anything. This is a contrived example, but we&#8217;re using wx to make it more visually interesting than just doing a console &#8220;Hello World&#8221; program. Note also that I am using py2exe 0.6.9, wxPython 2.8.11.0 and Python 2.6. Here&#8217;s what the end product should look like when run:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/py2exe_wx.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/py2exe_wx.png" alt="" title="py2exe_wx.png" width="496" height="400" class="aligncenter size-full wp-image-1084" /></a></p>
<p>Now that we know what it looks like, here&#8217;s a look at the code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DemoPanel<span style="color: black;">&#40;</span>wx.<span style="color: black;">Panel</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Panel</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>
&nbsp;
        labels = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Name&quot;</span>, <span style="color: #483d8b;">&quot;Address&quot;</span>, <span style="color: #483d8b;">&quot;City&quot;</span>, <span style="color: #483d8b;">&quot;State&quot;</span>, <span style="color: #483d8b;">&quot;Zip&quot;</span>,
                  <span style="color: #483d8b;">&quot;Phone&quot;</span>, <span style="color: #483d8b;">&quot;Email&quot;</span>, <span style="color: #483d8b;">&quot;Notes&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
        mainSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Please enter your information here:&quot;</span><span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> lbl <span style="color: #ff7700;font-weight:bold;">in</span> labels:
            sizer = <span style="color: #008000;">self</span>.<span style="color: black;">buildControls</span><span style="color: black;">&#40;</span>lbl<span style="color: black;">&#41;</span>
            mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sizer, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">EXPAND</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>mainSizer<span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Layout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> buildControls<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
        size = <span style="color: black;">&#40;</span><span style="color: #ff4500;">80</span>,<span style="color: #ff4500;">40</span><span style="color: black;">&#41;</span>
        font = wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span>
&nbsp;
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=label, size=size<span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> label <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;Notes&quot;</span>:
            txt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name=label<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            txt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, style=wx.<span style="color: black;">TE_MULTILINE</span>, name=label<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>txt, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> sizer
&nbsp;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DemoFrame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Frame that holds all other widgets
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>,
                          <span style="color: #483d8b;">&quot;Py2Exe Tutorial&quot;</span>,
                          size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">600</span>,<span style="color: #ff4500;">400</span><span style="color: black;">&#41;</span>
                          <span style="color: black;">&#41;</span>
        panel = DemoPanel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">App</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    frame = DemoFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>This is fairly straightforward, so I&#8217;ll leave it the reader to figure out. This article is about py2exe after all.</p>
<h2>The py2exe setup.py file</h2>
<p>The key to any py2exe script is the <em>setup.py</em> file. This file controls what gets included or excluded, how much we compress and bundle, and much more! Here is the simplest setup that we can use with the wx script above:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup
<span style="color: #ff7700;font-weight:bold;">import</span> py2exe
&nbsp;
setup<span style="color: black;">&#40;</span>windows=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'sampleApp.py'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre>
<p>As you can see, we import the <em>setup</em> method from <em>distutils.core</em> and then we import <em>py2exe</em>. Next we call setup with a <em>windows</em> keyword parameter and pass it the name of the main file inside a python list object. If you were creating a non-GUI project, than you would use the <em>console </em> key instead of <em>windows</em>. To run this, open up a command prompt and navigate to the appropriate location. Then type &#8220;python setup.py py2exe&#8221; to run it. This is what I got when I first ran it:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/py2exe_firstrun.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/py2exe_firstrun.png" alt="" title="py2exe_firstrun.png" width="450" height="325" class="aligncenter size-full wp-image-1085" /></a></p>
<p>It looks like wxPython requires the &#8220;MSVCP90.dll&#8221; and Windows can&#8217;t find it. A quick Google search yielded the consensus that I needed the &#8220;Microsoft Visual C++ 2008 Redistributable Package&#8221;, found <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&#038;displaylang=en">here</a>. I downloaded it, installed it and tried py2exe again. Same error. This would have probably worked had I been using Visual Studio to create an exe of a C# program. Anyway, the trick was to search the hard drive for the file and then copy it to Python&#8217;s DLL folder, which on my machine was found at the following location: C:\Python26\DLLs (adjust as necessary on your machine). Once the DLL was in the proper place, the setup.py file ran just fine. The result was put into a &#8220;dist&#8221; folder which contains 17 files and weighs in at 15.3 MB. I double-clicked the &#8220;sampleApp.exe&#8221; file to see if my shiny new binary would work and it did! In older versions of wxPython, you would have needed to include a manifest file to get the right look and feel (i.e the themes), but that was taken care of in 2.8.10 (I think) as was the side-by-side (SxS) assembly manifest file that used to be required.</p>
<p>Note that for non-wxPython scripts, you will probably still need to mess with the SxS manifests and all the hoops that includes. You can read more about that in the <a href="http://py2exe.org/index.cgi/Tutorial">py2exe tutorial</a>.</p>
<h2>Creating an Advanced setup.py File</h2>
<p>Let&#8217;s see what other options py2exe gives us for creating binaries by creating a more complex setup.py file. </p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup
<span style="color: #ff7700;font-weight:bold;">import</span> py2exe
&nbsp;
includes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
excludes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'_gtkagg'</span>, <span style="color: #483d8b;">'_tkagg'</span>, <span style="color: #483d8b;">'bsddb'</span>, <span style="color: #483d8b;">'curses'</span>, <span style="color: #483d8b;">'email'</span>, <span style="color: #483d8b;">'pywin.debugger'</span>,
            <span style="color: #483d8b;">'pywin.debugger.dbgcon'</span>, <span style="color: #483d8b;">'pywin.dialogs'</span>, <span style="color: #483d8b;">'tcl'</span>,
            <span style="color: #483d8b;">'Tkconstants'</span>, <span style="color: #483d8b;">'Tkinter'</span><span style="color: black;">&#93;</span>
packages = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
dll_excludes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'libgdk-win32-2.0-0.dll'</span>, <span style="color: #483d8b;">'libgobject-2.0-0.dll'</span>, <span style="color: #483d8b;">'tcl84.dll'</span>,
                <span style="color: #483d8b;">'tk84.dll'</span><span style="color: black;">&#93;</span>
&nbsp;
setup<span style="color: black;">&#40;</span>
    options = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;py2exe&quot;</span>: <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;compressed&quot;</span>: <span style="color: #ff4500;">2</span>,
                          <span style="color: #483d8b;">&quot;optimize&quot;</span>: <span style="color: #ff4500;">2</span>,
                          <span style="color: #483d8b;">&quot;includes&quot;</span>: includes,
                          <span style="color: #483d8b;">&quot;excludes&quot;</span>: excludes,
                          <span style="color: #483d8b;">&quot;packages&quot;</span>: packages,
                          <span style="color: #483d8b;">&quot;dll_excludes&quot;</span>: dll_excludes,
                          <span style="color: #483d8b;">&quot;bundle_files&quot;</span>: <span style="color: #ff4500;">3</span>,
                          <span style="color: #483d8b;">&quot;dist_dir&quot;</span>: <span style="color: #483d8b;">&quot;dist&quot;</span>,
                          <span style="color: #483d8b;">&quot;xref&quot;</span>: <span style="color: #008000;">False</span>,
                          <span style="color: #483d8b;">&quot;skip_archive&quot;</span>: <span style="color: #008000;">False</span>,
                          <span style="color: #483d8b;">&quot;ascii&quot;</span>: <span style="color: #008000;">False</span>,
                          <span style="color: #483d8b;">&quot;custom_boot_script&quot;</span>: <span style="color: #483d8b;">''</span>,
                         <span style="color: black;">&#125;</span>
              <span style="color: black;">&#125;</span>,
    windows=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'sampleApp.py'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#41;</span></pre>
<p>This is pretty self-explanatory, but let&#8217;s unpack it anyway. First we set up a few lists that we pass to the options parameter of the setup function. </p>
<ul>
<li>The <em>includes</em> list is for special modules that you need to specifically include. Sometimes py2exe can&#8217;t find certain modules, so you get to manually specify them here. </li>
<li>The <em>excludes</em> list is a list of which modules to exclude from your program. In this case, we don&#8217;t need Tkinter since we&#8217;re using wxPython. This list of excludes is what GUI2Exe will exclude by default.</li>
<li>The <em>packages</em> list is a list of specific packages to include. Again, sometimes py2exe just can&#8217;t find something. I&#8217;ve had to include email, PyCrypto, or lxml here before. Note that if the excludes list contains something you&#8217;re trying to include in the packages or includes lists, py2exe may continue to exclude them.</li>
<li><em>dll_excludes</em> &#8211; excludes dlls that we don&#8217;t need in our project.</li>
</ul>
<p>In the <em>options</em> dictionary, we have a few other options to look at. The <em>compressed</em> key tells py2exe whether or not to compress the zipfile, if it&#8217;s set. The <em>optimize</em> key sets the optimization level. Zero is no optimization and 2 is the highest. The <em>bundle_files</em> key bundles dlls in the zipfile or the exe. Valid values for <em>bundle_files</em> are: 3 = don&#8217;t bundle (default) 2 = bundle everything but the Python interpreter 1 = bundle everything, including the Python interpreter. A couple of years ago, when I was first learning py2exe, I asked on their mailing list what the best option was because I was having issues with bundle option 1. I was told that 3 was probably the most stable. I went with that and stopped having random problems, so that&#8217;s what I currently recommend. If you don&#8217;t like distributing more than one file, zip them up or create an installer. The only other option I use in this list is the <em>dist_dir</em> one. I use it to experiment with different built options or to create custom builds when I don&#8217;t want to overwrite my main good build. You can read about all the other options (including ones not even listed here) on the <a href="http://py2exe.org/index.cgi/ListOfOptions">py2exe website</a>. By setting optimize to 2, we can reduce the size of folder by about one megabyte. </p>
<h2>Reducing a wxPython Script&#8217;s Binary Size</h2>
<p>There&#8217;s a <a href="http://bit.ly/bwIDNd">thread </a>on the wxPython mailing list about reducing the size of the binary. I contacted Steven Sproat, one of the people in the thread, about what he did and here&#8217;s the trick: Set the &#8220;bundle_files&#8221; option to 1 and the the zipfile to None. The result will be three files in your dist folder: MSVCR71.dll, sampleApp.exe and w9xpopen.exe. The size of the folder is 5.94 MB on my machine (Tested on a Windows XP machine). As I mentioned earlier, setting bundle_files to 1 can cause some users to experience issues when running your application, but this may have been fixed with the newer versions of py2exe. Here&#8217;s what the setup.py file looks like with the new options set:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup
<span style="color: #ff7700;font-weight:bold;">import</span> py2exe
&nbsp;
includes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
excludes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'_gtkagg'</span>, <span style="color: #483d8b;">'_tkagg'</span>, <span style="color: #483d8b;">'bsddb'</span>, <span style="color: #483d8b;">'curses'</span>, <span style="color: #483d8b;">'email'</span>, <span style="color: #483d8b;">'pywin.debugger'</span>,
            <span style="color: #483d8b;">'pywin.debugger.dbgcon'</span>, <span style="color: #483d8b;">'pywin.dialogs'</span>, <span style="color: #483d8b;">'tcl'</span>,
            <span style="color: #483d8b;">'Tkconstants'</span>, <span style="color: #483d8b;">'Tkinter'</span><span style="color: black;">&#93;</span>
packages = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
dll_excludes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'libgdk-win32-2.0-0.dll'</span>, <span style="color: #483d8b;">'libgobject-2.0-0.dll'</span>, <span style="color: #483d8b;">'tcl84.dll'</span>,
                <span style="color: #483d8b;">'tk84.dll'</span><span style="color: black;">&#93;</span>
&nbsp;
setup<span style="color: black;">&#40;</span>
    options = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;py2exe&quot;</span>: <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;compressed&quot;</span>: <span style="color: #ff4500;">2</span>,
                          <span style="color: #483d8b;">&quot;optimize&quot;</span>: <span style="color: #ff4500;">2</span>,
                          <span style="color: #483d8b;">&quot;includes&quot;</span>: includes,
                          <span style="color: #483d8b;">&quot;excludes&quot;</span>: excludes,
                          <span style="color: #483d8b;">&quot;packages&quot;</span>: packages,
                          <span style="color: #483d8b;">&quot;dll_excludes&quot;</span>: dll_excludes,
                          <span style="color: #483d8b;">&quot;bundle_files&quot;</span>: <span style="color: #ff4500;">1</span>,
                          <span style="color: #483d8b;">&quot;dist_dir&quot;</span>: <span style="color: #483d8b;">&quot;dist&quot;</span>,
                          <span style="color: #483d8b;">&quot;xref&quot;</span>: <span style="color: #008000;">False</span>,
                          <span style="color: #483d8b;">&quot;skip_archive&quot;</span>: <span style="color: #008000;">False</span>,
                          <span style="color: #483d8b;">&quot;ascii&quot;</span>: <span style="color: #008000;">False</span>,
                          <span style="color: #483d8b;">&quot;custom_boot_script&quot;</span>: <span style="color: #483d8b;">''</span>,
                         <span style="color: black;">&#125;</span>
              <span style="color: black;">&#125;</span>,
    <span style="color: #dc143c;">zipfile</span> = <span style="color: #008000;">None</span>,
    windows=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'sampleApp.py'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#41;</span></pre>
<p>Another way to reduce the size of the folder is to use compression programs as one of my readers, ProgMan, pointed out. Here are his results:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/sampleapps.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/sampleapps.png" alt="" title="sampleapps" width="496" height="315" class="aligncenter size-full wp-image-1096" /></a></p>
<h2>Wrapping Up</h2>
<p>You now know the basics for creating binaries with py2exe. I hope you have found this helpful for your current or future projects. If so, let me know in the comments!</p>
<h2>Further Reading</h2>
<ul>
<li>The py2exe <a href="http://py2exe.org/index.cgi/Tutorial">Tutorial</a></li>
<li><a href="http://www.blog.pythonlibrary.org/2008/08/27/packaging-wxpymail-for-distribution/">Packaging wxPyMail for Distribution</a></li>
<li><a href="http://code.google.com/p/gui2exe/">GUI2Exe</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PyWin32: Getting Windows Event Logs</title>
		<link>http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 12:14:28 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[PyWin32]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1071</guid>
		<description><![CDATA[The other day, there was a post on one of the mailing lists that I follow about accessing the Windows Event Logs. I thought that was an interesting topic, so I went looking for examples and found a pretty nice example on ActiveState. In this article, you&#8217;ll find out what I discovered. It&#8217;s probably easiest [...]]]></description>
			<content:encoded><![CDATA[<p>The  other day, there was a post on one of the mailing lists that I follow about accessing the Windows Event Logs. I thought that was an interesting topic, so I went looking for examples and found a pretty nice example on <a href="http://docs.activestate.com/activepython/2.5/pywin32/Windows_NT_Eventlog.html">ActiveState</a>. In this article, you&#8217;ll find out what I discovered.<span id="more-1071"></span></p>
<p>It&#8217;s probably easiest to just jump right into the code. Note that the only thing other than Python that you will need is the PyWin32 package. Once you&#8217;ve got that, then you can follow along:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">codecs</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">traceback</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32con
<span style="color: #ff7700;font-weight:bold;">import</span> win32evtlog
<span style="color: #ff7700;font-weight:bold;">import</span> win32evtlogutil
<span style="color: #ff7700;font-weight:bold;">import</span> winerror
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getAllEvents<span style="color: black;">&#40;</span>server, logtypes, basePath<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> server:
        serverName = <span style="color: #483d8b;">&quot;localhost&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        serverName = server
    <span style="color: #ff7700;font-weight:bold;">for</span> logtype <span style="color: #ff7700;font-weight:bold;">in</span> logtypes:
        path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>basePath, <span style="color: #483d8b;">&quot;%s_%s_log.log&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>serverName, logtype<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        getEventLogs<span style="color: black;">&#40;</span>server, logtype, path<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getEventLogs<span style="color: black;">&#40;</span>server, logtype, logPath<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Get the event logs from the specified machine according to the
    logtype (Example: Application) and save it to the appropriately
    named log file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Logging %s events&quot;</span> <span style="color: #66cc66;">%</span> logtype
    log = <span style="color: #dc143c;">codecs</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>logPath, encoding=<span style="color: #483d8b;">'utf-8'</span>, mode=<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    line_break = <span style="color: #483d8b;">'-'</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">80</span>
&nbsp;
    log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>%s Log of %s Events<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>server, logtype<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Created: %s<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">time</span>.<span style="color: black;">ctime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + line_break + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    hand = win32evtlog.<span style="color: black;">OpenEventLog</span><span style="color: black;">&#40;</span>server,logtype<span style="color: black;">&#41;</span>
    total = win32evtlog.<span style="color: black;">GetNumberOfEventLogRecords</span><span style="color: black;">&#40;</span>hand<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Total events in %s = %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>logtype, total<span style="color: black;">&#41;</span>
    flags = win32evtlog.<span style="color: black;">EVENTLOG_BACKWARDS_READ</span>|win32evtlog.<span style="color: black;">EVENTLOG_SEQUENTIAL_READ</span>
    events = win32evtlog.<span style="color: black;">ReadEventLog</span><span style="color: black;">&#40;</span>hand,flags,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    evt_dict=<span style="color: black;">&#123;</span>win32con.<span style="color: black;">EVENTLOG_AUDIT_FAILURE</span>:<span style="color: #483d8b;">'EVENTLOG_AUDIT_FAILURE'</span>,
              win32con.<span style="color: black;">EVENTLOG_AUDIT_SUCCESS</span>:<span style="color: #483d8b;">'EVENTLOG_AUDIT_SUCCESS'</span>,
              win32con.<span style="color: black;">EVENTLOG_INFORMATION_TYPE</span>:<span style="color: #483d8b;">'EVENTLOG_INFORMATION_TYPE'</span>,
              win32con.<span style="color: black;">EVENTLOG_WARNING_TYPE</span>:<span style="color: #483d8b;">'EVENTLOG_WARNING_TYPE'</span>,
              win32con.<span style="color: black;">EVENTLOG_ERROR_TYPE</span>:<span style="color: #483d8b;">'EVENTLOG_ERROR_TYPE'</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        events=<span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> events:
            events=win32evtlog.<span style="color: black;">ReadEventLog</span><span style="color: black;">&#40;</span>hand,flags,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">for</span> ev_obj <span style="color: #ff7700;font-weight:bold;">in</span> events:
                the_time = ev_obj.<span style="color: black;">TimeGenerated</span>.<span style="color: black;">Format</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#'12/23/99 15:54:09'</span>
                evt_id = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>winerror.<span style="color: black;">HRESULT_CODE</span><span style="color: black;">&#40;</span>ev_obj.<span style="color: black;">EventID</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                computer = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>ev_obj.<span style="color: black;">ComputerName</span><span style="color: black;">&#41;</span>
                cat = ev_obj.<span style="color: black;">EventCategory</span>
        <span style="color: #808080; font-style: italic;">##        seconds=date2sec(the_time)</span>
                record = ev_obj.<span style="color: black;">RecordNumber</span>
                msg = win32evtlogutil.<span style="color: black;">SafeFormatMessage</span><span style="color: black;">&#40;</span>ev_obj, logtype<span style="color: black;">&#41;</span>
&nbsp;
                source = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>ev_obj.<span style="color: black;">SourceName</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> ev_obj.<span style="color: black;">EventType</span> <span style="color: #ff7700;font-weight:bold;">in</span> evt_dict.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
                    evt_type = <span style="color: #483d8b;">&quot;unknown&quot;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    evt_type = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>evt_dict<span style="color: black;">&#91;</span>ev_obj.<span style="color: black;">EventType</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Event Date/Time: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> the_time<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Event ID / Type: %s / %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>evt_id, evt_type<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Record #%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> record<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Source: %s<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> source<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line_break<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">traceback</span>.<span style="color: black;">print_exc</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Log creation finished. Location of log is %s&quot;</span> <span style="color: #66cc66;">%</span> logPath
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    server = <span style="color: #008000;">None</span>  <span style="color: #808080; font-style: italic;"># None = local machine</span>
    logTypes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;System&quot;</span>, <span style="color: #483d8b;">&quot;Application&quot;</span>, <span style="color: #483d8b;">&quot;Security&quot;</span><span style="color: black;">&#93;</span>
    getAllEvents<span style="color: black;">&#40;</span>server, logTypes, <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\d</span>ownloads&quot;</span><span style="color: black;">&#41;</span></pre>
<p>There are a couple potential caveats to this type of scripting. I tested this code as an Administrator on my PCs and as a Domain Administrator at work. I did not test it as any other type of user. So if you have problems getting this code to run, check your permissions. I tested this from Windows XP  and Windows 7. The UAC doesn&#8217;t appear to block this activity on Windows 7, so that made it just as easy to use as XP did. However, Windows 7&#8242;s events had some unicode in the message portion of the code whereas XP did not. Watch out for that and handle it accordingly.</p>
<p>Anyway, let&#8217;s unpack this script and see how it works. First we have a number of imports. We use the <em>codecs</em> modules to encode the log file in utf-8 just in case there&#8217;s some sneaky unicode in the message. We use PyWin32&#8242;s <em>win32evtlog</em> module to open the event log and pull information out of it. According to the article I mentioned at the beginning, to get all the events from the log, you need to call <em>win32evtlog.ReadEventLog</em> repeatedly until it stops returning events. Thus, we use <em>while </em>loop. Inside the while loop, we use a <em>for</em> loop to iterate over the events and extract the event ID, record number, event message, event source and a few other tidbits. We log it and then we exit the <em>for</em> loop and the <em>while</em> loop calls the <em>win32evtlog.ReadEventLog</em> again.</p>
<p>We use the <em>traceback</em> module to print out any errors that occur during the script&#8217;s run. And that&#8217;s all there is to it!</p>
<h2>Wrapping Up</h2>
<p>As you can see, using the PyWin32 package is easy. If you get stuck, it has some great documentation. If that documentation isn&#8217;t good enough though, you can fall back on MSDN instead. PyWin32 is a light wrapper around Windows&#8217; API, so using MSDN&#8217;s instructions is fairly simple. Anyway, I hope you learned a lot and will find it helpful.</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://docs.python.org/howto/unicode.html">Python and Unicode</a></li>
<li><a href="http://sourceforge.net/projects/pywin32/">PyWin32</a></li>
<li>PyWin32 <a href="http://docs.activestate.com/activepython/2.5/pywin32/PyWin32.HTML">Documentation </a>from ActiveState</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python and Microsoft Office &#8211; Using PyWin32</title>
		<link>http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 14:18:55 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PyWin32]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Word]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1040</guid>
		<description><![CDATA[Most typical users have used Microsoft Office. While Office may be the bane of tech support, we still have to deal with it. Python can be used to script (AKA automate) Office and make it easier for us or our users to use. It may not be as easy as recording a macro, but it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Most typical users have used Microsoft Office. While Office may be the bane of tech support, we still have to deal with it. Python can be used to script (AKA automate) Office and make it easier for us or our users to use. It may not be as easy as recording a macro, but it&#8217;s close. For this article, you will learn how to use the PyWin32 module to access some of the Office programs and manipulate them with Python. Some forums say that you need to run PythonWin&#8217;s makepy utility on Microsoft Word (and Excel) before you can access Office applications. I don&#8217;t think I needed to do that to make it work though (at least, not with the 2007 version). However, PythonWin comes with the PyWin32, so if you do run into trouble, you can try it.<span id="more-1040"></span></p>
<h2>Python and Microsoft Excel</h2>
<p>If you&#8217;ve looked for examples of using Python and Office, you&#8217;ll usually find that the most often hacked component is Excel. In fact, there are several non-PyWin32 modules specifically created to read and write Excel files. They are called xlrd and xlwt, respectively. But that&#8217;s a topic for another article. Here we&#8217;ll see how to mess with Excel using the PyWin32 interface. Note that the following scripts only work on Windows. One advantage of xlrd and xlwt is that you can use them on any platform.</p>
<p>Let&#8217;s take a look at a simple example, shall we?</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32com.<span style="color: black;">client</span> as win32
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> excel<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    xl = win32.<span style="color: black;">gencache</span>.<span style="color: black;">EnsureDispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Excel.Application'</span><span style="color: black;">&#41;</span>
    ss = xl.<span style="color: black;">Workbooks</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    sh = ss.<span style="color: black;">ActiveSheet</span>
&nbsp;
    xl.<span style="color: black;">Visible</span> = <span style="color: #008000;">True</span>
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    sh.<span style="color: black;">Cells</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span> = <span style="color: #483d8b;">'Hacking Excel with Python Demo'</span>
&nbsp;
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>:
        sh.<span style="color: black;">Cells</span><span style="color: black;">&#40;</span>i,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span> = <span style="color: #483d8b;">'Line %i'</span> <span style="color: #66cc66;">%</span> i
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    ss.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    xl.<span style="color: black;">Application</span>.<span style="color: black;">Quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    excel<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>The example above is similar to what you will normally find on the web. It is actually based on an example I saw in Wesley Chun&#8217;s excellent book, <em>Core Python Programming</em>.  Let&#8217;s take some time and unpack the code. To gain access to Excel, we import <em>win32com.client</em> and then call its <em>gencache.EnsureDispatch</em>, passing in the application name that we want to open. In this case, the string to pass is &#8220;Excel.Application&#8221;. All that does is open Excel in the background. At this point, the user won&#8217;t even know Excel is open unless they have Task Manager running. The next line is adds a new workbook to Excel by calling that Excel instance&#8217;s &#8220;Workbooks.Add()&#8221; method. This returns a sheets object (I think). To get the ActiveSheet, we call <em>ss.ActiveSheet</em>. Finally, we make the Excel program itself visible by setting that property to True.</p>
<p>To set a specific cell&#8217;s value, call something like this: <em>sh.Cells(row,col).Value = &#8220;some value&#8221;</em>. Note that our instance is NOT zero-based and will actually put the value in the correct row/col combo. If we want to extract a value, we just remove the equals sign. What if we want the formula? To figure this our, I recorded a macro in Excel and did a Paste Special command that only pasted the Formula. Using the code generated, I figured out that to get the formula in Python, you just do this: </p>
<pre class="python">formula = sh.<span style="color: black;">Cells</span><span style="color: black;">&#40;</span>row, col<span style="color: black;">&#41;</span>.<span style="color: black;">Formula</span></pre>
<p>What if you need to change which sheet you&#8217;re on? Recording a macro also showed me how to accomplish this feat. Here&#8217;s the VBA code from Excel:</p>
<p><code><br />
Sub Macro1()<br />
'<br />
' Macro1 Macro<br />
'<br />
    Sheets(&quot;Sheet2&quot;).Select<br />
End Sub<br />
</code></p>
<p>From this code, I gathered that I needed to call my sheets object&#8217;s &#8220;Sheets&#8221; method and after a little fiddling, I got it to work by doing the following:</p>
<pre class="python">sheet2 = ss.<span style="color: black;">Sheets</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Sheet2&quot;</span><span style="color: black;">&#41;</span></pre>
<p>Now we have a handle on the second sheet in the workbook. If you want to edit or retrieve values, just prepend the same methods you used above with whatever you called the sheet2 instance (i.e. sheet2.Cells(1,1).Value). The last two lines from the original program will close the sheets and then quit the entire Excel instance.</p>
<p>You may be thinking that so far all I&#8217;ve shown is how to create a new document. What if you want to open an existing file? Just do something like this at the beginning of the code:</p>
<pre class="python">xl = win32.<span style="color: black;">gencache</span>.<span style="color: black;">EnsureDispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Excel.Application'</span><span style="color: black;">&#41;</span>
ss = xl.<span style="color: black;">Workbooks</span>.<span style="color: black;">Open</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span></pre>
<p>And there you have it! You now know the basics of hacking Excel with Python using Excel&#8217;s COM object model. If you need to learn more, I recommend trying to record a macro and than translating the result into Python. <em>Note: I was unable to find an example that could save the spreadsheet&#8230;there are several examples that claim they work, but they didn&#8217;t for me.</em></p>
<h2>Python and Microsoft Word</h2>
<p>Accessing Microsoft Word with Python follows the same syntax that we used for Excel. Let&#8217;s take a quick look at how to access Word.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">time</span> <span style="color: #ff7700;font-weight:bold;">import</span> sleep
<span style="color: #ff7700;font-weight:bold;">import</span> win32com.<span style="color: black;">client</span> as win32
&nbsp;
RANGE = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> word<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    word = win32.<span style="color: black;">gencache</span>.<span style="color: black;">EnsureDispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Word.Application'</span><span style="color: black;">&#41;</span>
    doc = word.<span style="color: black;">Documents</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    word.<span style="color: black;">Visible</span> = <span style="color: #008000;">True</span>
    sleep<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    rng = doc.<span style="color: black;">Range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    rng.<span style="color: black;">InsertAfter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Hacking Word with Python<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
    sleep<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> RANGE:
        rng.<span style="color: black;">InsertAfter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Line %d<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #66cc66;">%</span> i<span style="color: black;">&#41;</span>
        sleep<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    rng.<span style="color: black;">InsertAfter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Python rules!<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    doc.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    word.<span style="color: black;">Application</span>.<span style="color: black;">Quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    word<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>This particular example is also based on something from Chun&#8217;s book as well. However, there are lots of other examples on the web that look almost exactly like this too. Let&#8217;s unpack this code now. To get a handle on the Microsoft Word application, we call <em>win32.gencache.EnsureDispatch(&#8216;Word.Application&#8217;)</em>; then we add a new document by calling the word instance&#8217;s <em>Documents.Add()</em>. If you want to show the user what you&#8217;re up to, you can set the visibility of Word to True.</p>
<p>If you want to add text to the document, then you&#8217;ll want to tell Word where you want the text to go. That&#8217;s where the Range method comes in. While you can&#8217;t see it, there is a &#8220;grid&#8221; of sorts that tells Word how to layout the text onscreen. So if we want to insert text at the very top of the document, we tell it to start at (0,0).  To add a new line in Word, we need to append &#8220;\r\n&#8221; to the end of our string. If you don&#8217;t know about the annoyances of line endings on different platforms, you should spend some time with Google and learn about it so you don&#8217;t get bit by weird bugs!</p>
<p>The rest of the code is pretty self-explanatory and will be left to the reader to interpret. We&#8217;ll move on to opening and saving documents now:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># Based on examples from http://code.activestate.com/recipes/279003/</span>
word.<span style="color: black;">Documents</span>.<span style="color: black;">Open</span><span style="color: black;">&#40;</span>doc<span style="color: black;">&#41;</span>
word.<span style="color: black;">ActiveDocument</span>.<span style="color: black;">SaveAs</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>a.txt&quot;</span>, FileFormat=win32com.<span style="color: black;">client</span>.<span style="color: black;">constants</span>.<span style="color: black;">wdFormatTextLineBreaks</span><span style="color: black;">&#41;</span></pre>
<p>Here we show how to open an existing Word document and save it as text. I haven&#8217;t tested this one fully, so your mileage may vary. If you want to read the text in the document, you can do the following:</p>
<pre class="python">docText = word.<span style="color: black;">Documents</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">Content</span></pre>
<p>And that ends the Python hacking lesson on Word documents. Since a lot of the information I was finding on Microsoft Word and Python was old and crusty and didn&#8217;t seem to work half the time, I don&#8217;t add to the mess of bad information. Hopefully this will get you started on your own journey into the wild wonders of Word manipulation.</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://win32com.goermezer.de/content/view/94/192/">Python for Windows Examples</a></li>
<li>Opening MS Word files via Python <a href="http://www.velocityreviews.com/forums/t330073-opening-ms-word-files-via-python.html">thread</a></li>
<li><a href="http://code.activestate.com/recipes/279003/">Recipe 279003</a>: Converting Word documents to text</li>
<li>Dzone: <a href="http://snippets.dzone.com/posts/show/2036">script excel from Python</a></li>
<li>Python-Excel <a href="http://www.python-excel.org/">website</a>, <a href="http://groups.google.com/group/python-excel">mailing list</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python: How to Tell How Long Windows Has Been Idle</title>
		<link>http://www.blog.pythonlibrary.org/2010/05/05/python-how-to-tell-how-long-windows-has-been-idle/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/05/05/python-how-to-tell-how-long-windows-has-been-idle/#comments</comments>
		<pubDate>Wed, 05 May 2010 13:08:09 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[PyWin32]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=836</guid>
		<description><![CDATA[The other day, I received a request to create a script that could tell how long a Windows XP machine had been idle and to alert the user if it had been idle for a certain amount of time. I did a little research with Google and found a couple of ways to accomplish this [...]]]></description>
			<content:encoded><![CDATA[<p>The other day, I received a request to create a script that could tell how long a Windows XP machine had been idle and to alert the user if it had been idle for a certain amount of time. I did a little research with Google and found a couple of ways to accomplish this feat. The only one I was able to get working was a ctypes example, so without further ado, let&#8217;s check it out!<span id="more-836"></span></p>
<p>The following ctypes-related code was taken from a <a href="http://stackoverflow.com/questions/911856/detecting-idle-time-in-python">stackoverflow forum</a>:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> Structure, windll, c_uint, sizeof, byref
&nbsp;
<span style="color: #808080; font-style: italic;"># http://stackoverflow.com/questions/911856/detecting-idle-time-in-python</span>
<span style="color: #ff7700;font-weight:bold;">class</span> LASTINPUTINFO<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
    _fields_ = <span style="color: black;">&#91;</span>
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'cbSize'</span>, c_uint<span style="color: black;">&#41;</span>,
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'dwTime'</span>, c_uint<span style="color: black;">&#41;</span>,
    <span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_idle_duration<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    lastInputInfo = LASTINPUTINFO<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    lastInputInfo.<span style="color: black;">cbSize</span> = sizeof<span style="color: black;">&#40;</span>lastInputInfo<span style="color: black;">&#41;</span>
    windll.<span style="color: black;">user32</span>.<span style="color: black;">GetLastInputInfo</span><span style="color: black;">&#40;</span>byref<span style="color: black;">&#40;</span>lastInputInfo<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    millis = windll.<span style="color: black;">kernel32</span>.<span style="color: black;">GetTickCount</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - lastInputInfo.<span style="color: black;">dwTime</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> millis / <span style="color: #ff4500;">1000.0</span></pre>
<p>If you don&#8217;t understand the code above, please ask for help on the ctypes mailing list. I don&#8217;t really understand it all either. I get the basic gist, but that&#8217;s it. Here&#8217;s the snippet I use to make the code above do its thing:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
    GetLastInputInfo = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>get_idle_duration<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> GetLastInputInfo
    <span style="color: #ff7700;font-weight:bold;">if</span> GetLastInputInfo == <span style="color: #ff4500;">480</span>:
        <span style="color: #808080; font-style: italic;"># if GetLastInputInfo is 8 minutes, play a sound</span>
        sound = r<span style="color: #483d8b;">&quot;c:<span style="color: #000099; font-weight: bold;">\w</span>indows<span style="color: #000099; font-weight: bold;">\m</span>edia<span style="color: #000099; font-weight: bold;">\n</span>otify.wav&quot;</span>
        <span style="color: #dc143c;">winsound</span>.<span style="color: black;">PlaySound</span><span style="color: black;">&#40;</span>sound, <span style="color: #dc143c;">winsound</span>.<span style="color: black;">SND_FILENAME</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> GetLastInputInfo == <span style="color: #ff4500;">560</span>:
        <span style="color: #808080; font-style: italic;"># if GetLastInputInfo is 9 minutes, play a more annoying sound</span>
        sound = r<span style="color: #483d8b;">&quot;c:<span style="color: #000099; font-weight: bold;">\w</span>indows<span style="color: #000099; font-weight: bold;">\m</span>edia<span style="color: #000099; font-weight: bold;">\r</span>ingout.wav&quot;</span>
        <span style="color: #dc143c;">winsound</span>.<span style="color: black;">PlaySound</span><span style="color: black;">&#40;</span>sound, <span style="color: #dc143c;">winsound</span>.<span style="color: black;">SND_FILENAME</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">winsound</span>.<span style="color: black;">PlaySound</span><span style="color: black;">&#40;</span>sound, <span style="color: #dc143c;">winsound</span>.<span style="color: black;">SND_FILENAME</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">winsound</span>.<span style="color: black;">PlaySound</span><span style="color: black;">&#40;</span>sound, <span style="color: #dc143c;">winsound</span>.<span style="color: black;">SND_FILENAME</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre>
<p>In my code, I check if the machine has been idle for 8 minutes and 9 minutes. Depending on how long it&#8217;s been idle, the code plays a specific wav file with Python winsound module. In our shop, we have certain machines lock themselves if they&#8217;ve been idle for 10 minutes. Our users don&#8217;t like that too much, so they requested that we warn them with a sound when the machine was about to lock. That is what this script accomplishes. Hopefully you can find a better use for this knowledge.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/05/05/python-how-to-tell-how-long-windows-has-been-idle/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python Text-to-Speech: Making Your PC Talk</title>
		<link>http://www.blog.pythonlibrary.org/2010/04/02/python-test-to-speech-making-your-pc-talk/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/04/02/python-test-to-speech-making-your-pc-talk/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 22:57:13 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=751</guid>
		<description><![CDATA[Soon after getting hired at my current job, my boss sent me a script (which I think was based on this article) about Python and a certain text-to-speech module called pyTTS. This was right after Python 2.5 had released. Anyway, it&#8217;s basically a nice wrapper over the win32com module which can communicate with the Microsoft [...]]]></description>
			<content:encoded><![CDATA[<p>Soon after getting hired at my current job, my boss sent me a script (which I think was based on this <a href="http://mindtrove.info/articles/synthesizing-speech-with-pytts/">article</a>) about Python and a certain text-to-speech module called <a href="http://www.cs.unc.edu/Research/assist/doc/pytts/">pyTTS</a>. This was right after Python 2.5 had released. Anyway, it&#8217;s basically a nice wrapper over the win32com module which can communicate with the Microsoft Speech API (SAPI).<span id="more-751"></span></p>
<p>I won&#8217;t go over everything about pyTTS since that article I linked to above already does that, but I will give you a quick introduction to it. If you go to the author&#8217;s <a href="http://mindtrove.info/clique/">website</a> you&#8217;ll find that he&#8217;s moved on to Screen Reading technology and has created a program called Clique. I&#8217;m not sure if that is written in Python or not though. I went digging on the site to find out where you could download the software and finally found this: <a href="http://sourceforge.net/projects/uncassist/files/ ">http://sourceforge.net/projects/uncassist/files/ </a></p>
<p>From what I can tell, he only officially support Python 2.3-2.5. However, since pyTTS basically just wraps the win32com calls to SAPI and the PyWin32 module supports Python 2.x-3.x, I think it would be fairly easy to make pyTTS work with the newer versions. </p>
<p><em>Note: You will need the <a href="http://www.cs.unc.edu/Research/assist/packages/SAPI5SpeechInstaller.msi">Microsoft SAPI 5.1 redistributable</a>, <a href="http://www.cs.unc.edu/Research/assist/packages/SAPI5VoiceInstaller.msi">extra MS voices</a> and <a href="http://sourceforge.net/projects/pywin32/files/">PyWin32</a>.</em></p>
<p>Let&#8217;s take a quick look at how we use this module:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> pyTTS
tts = pyTTS.<span style="color: black;">Create</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
tts.<span style="color: black;">SetVoiceByName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'MSSam'</span><span style="color: black;">&#41;</span>
tts.<span style="color: black;">Speak</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, fellow Python programmer&quot;</span><span style="color: black;">&#41;</span></pre>
<p>You don&#8217;t have to set the voice as I think the default it MSMary, but it&#8217;s fun to do. The Speak method accepts various flags for its second argument. One example is pyTTS.tts_async, which puts the speaking in asynchronous mode. You can also mess with the volume by doing this: tts.Volume = 50. You can choose anywhere from 0-100%.</p>
<p>If you look at this <a href="http://mindtrove.info/articles/synthesizing-speech-with-pytts/">article</a>, it will teach you how to make pyTTS pronounce the words you feed it.</p>
<p>Here is how you would do most of the example above using PyWin32:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> win32com.<span style="color: black;">client</span> <span style="color: #ff7700;font-weight:bold;">import</span> constants
<span style="color: #ff7700;font-weight:bold;">import</span> win32com.<span style="color: black;">client</span>
speaker = win32com.<span style="color: black;">client</span>.<span style="color: black;">Dispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SAPI.SpVoice&quot;</span>, constants.<span style="color: black;">SVSFlagsAsync</span><span style="color: black;">&#41;</span>
speaker.<span style="color: black;">Speak</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, fellow Python programmer&quot;</span><span style="color: black;">&#41;</span></pre>
<p>While researching this article, I noticed that the developer(s) behind pyTTS had also put together a cross-platform text-to-speech module called <a href="http://pypi.python.org/pypi/pyttsx">pyttsx</a>. I haven&#8217;t used it, but I encourage you to give it a try. Other modules worth a look are <a href="http://code.google.com/p/pyspeech/">pySpeech</a> and this cool recipe that allows Python to recognize speech: <a href="http://www.surguy.net/articles/speechrecognition.xml">http://www.surguy.net/articles/speechrecognition.xml</a></p>
<p>Well, this ended up being more of a survey of the cool speech-related modules in Python than about code. Still, I hope this will prove helpful to you in your endeavors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/04/02/python-test-to-speech-making-your-pc-talk/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Restarting PCs with Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 15:47:25 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=738</guid>
		<description><![CDATA[Have you ever wanted to restart your Windows PC with out pressing Start, Shutdown or CTRL+ALT+DEL? What about restarting your annoying co-worker&#8217;s PC&#8230;the one who just doesn&#8217;t know when to shut up? Well, Python has the answer and this blog will tell you how to do it! Note: I don&#8217;t actually recommend rebooting your neighbor&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to restart your Windows PC with out pressing Start, Shutdown or CTRL+ALT+DEL? What about restarting your annoying co-worker&#8217;s PC&#8230;the one who just doesn&#8217;t know when to shut up? Well, Python has the answer and this blog will tell you how to do it!<span id="more-738"></span> Note: I don&#8217;t actually recommend rebooting your neighbor&#8217;s PC at random&#8230;</p>
<h2>Rebooting with PyWin32</h2>
<p>Anyway, back when I was first learning about Python, I stumbled upon an ActiveState recipe on how to do this. Of course, now I can&#8217;t seem to find that recipe, but I found a similar one here: http://code.activestate.com/recipes/360649/. Thus, we&#8217;ll start with this method first and then look at a slightly different approach. If you&#8217;d like to follow along, then you&#8217;ll need to make sure you have the <a href="http://sourceforge.net/projects/pywin32/files/">PyWin32 package</a>.</p>
<p>Here&#8217;s my version of the recipe from my foggy past:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># rebootServer.py</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> win32security
<span style="color: #ff7700;font-weight:bold;">import</span> win32api
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> ntsecuritycon <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AdjustPrivilege<span style="color: black;">&#40;</span>priv, enable=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Get the process token</span>
    flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
    htoken = win32security.<span style="color: black;">OpenProcessToken</span><span style="color: black;">&#40;</span>win32api.<span style="color: black;">GetCurrentProcess</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, flags<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># Get the ID for the system shutdown privilege.</span>
    idd = win32security.<span style="color: black;">LookupPrivilegeValue</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, priv<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># Now obtain the privilege for this process.</span>
    <span style="color: #808080; font-style: italic;"># Create a list of the privileges to be added.</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> enable:
        newPrivileges = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>idd, SE_PRIVILEGE_ENABLED<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        newPrivileges = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>idd, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #808080; font-style: italic;"># and make the adjustment</span>
    win32security.<span style="color: black;">AdjustTokenPrivileges</span><span style="color: black;">&#40;</span>htoken, <span style="color: #ff4500;">0</span>, newPrivileges<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> RebootServer<span style="color: black;">&#40;</span>message=<span style="color: #483d8b;">'Rebooting'</span>, timeout=<span style="color: #ff4500;">30</span>, bForce=<span style="color: #ff4500;">0</span>, bReboot=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        win32api.<span style="color: black;">InitiateSystemShutdown</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, message, timeout, bForce, bReboot<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        <span style="color: #808080; font-style: italic;"># Now we remove the privilege we just added.</span>
        AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AbortReboot<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        win32api.<span style="color: black;">AbortSystemShotdown</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    RebootServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Aborting shutdown'</span>
    AbortReboot<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>I actually took out the ability to reboot remote machines in this snippet just because I didn&#8217;t want to be tempted to do so. If you want to reboot a machine on your network, change the following line:</p>
<pre class="python">win32api.<span style="color: black;">InitiateSystemShutdown</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, message, timeout, bForce, bReboot<span style="color: black;">&#41;</span></pre>
<p>to</p>
<pre class="python">win32api.<span style="color: black;">InitiateSystemShutdown</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;someMachineName&quot;</span>, message, timeout, bForce, bReboot<span style="color: black;">&#41;</span></pre>
<p>or just add a host parameter to the <em>RebootServer</em> function and modify the InitiateSystemShutdown as necessary. If you just pass <em>None </em>in, then you&#8217;ll just reboot your own machine. The <em>AdjustPrivilege</em> method is used to change the privilege of the process so that it can actually shut the PC down. I think that only applies if you run the script as a user with limited privileges, but I almost never run that way so I can&#8217;t be sure. I do remember the recipe stating that you needed to remove the privileges that you added, which is what the <em>finally</em> statement is for. However, I would assume that once the reboot is complete, those privileges would have been revoked anyway.</p>
<h2>Rebooting with just Plain Ol&#8217; Python</h2>
<p>The other way to initiate a shutdown is to learn the magic of your Windows command line. There is a shutdown command that you can put into your Run dialog (go to Start &#8211;> Run) that will reboot your PC too. Here&#8217;s the one I&#8217;m talking about:  shutdown -r -t 1   <em>Note that this will only restart the local machine!</em></p>
<p>Now we just need to figure out how to make Python call that for us. Probably the simplest way to do so is to import the <em>os</em> module and call its <em>system</em> method. Let&#8217;s take a look:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;shutdown -r -t 1&quot;</span><span style="color: black;">&#41;</span></pre>
<p>This is much shorter than the PyWin32 method, but I don&#8217;t know that it&#8217;s as robust. I used this method in a quick and dirty wxPython application I created for our Sun Ray virtual machines at work. One of the biggest problems with the Sun Ray system is that the user is connected to their virtual machine using Remote Desktop. A part of Remote Desktop is to hide the Shutdown button, so there is no simple way for the user to restart their PC unless they&#8217;re installed some kind of hotfix that does it for them. There are times when rebooting a machine is a good thing, so I took the script above and put it into the following application:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/03/restarter.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/03/restarter.png" alt="" title="restarter" width="273" height="169" class="aligncenter size-full wp-image-739" /></a></p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> wx
<span style="color: #ff7700;font-weight:bold;">from</span> wx.<span style="color: black;">lib</span>.<span style="color: black;">buttons</span> <span style="color: #ff7700;font-weight:bold;">import</span> GenBitmapButton
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> RestarterPanel<span style="color: black;">&#40;</span>wx.<span style="color: black;">Panel</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Panel</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent=parent<span style="color: black;">&#41;</span>
&nbsp;
        img = wx.<span style="color: black;">Bitmap</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cute-logoff.png&quot;</span><span style="color: black;">&#41;</span>
        logoutBtn = GenBitmapButton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span>, img, style=wx.<span style="color: black;">BORDER_NONE</span><span style="color: black;">&#41;</span>
        logoutBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onLogout</span><span style="color: black;">&#41;</span>
&nbsp;
        img = wx.<span style="color: black;">Bitmap</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;quick_restart.png&quot;</span><span style="color: black;">&#41;</span>
        restartBtn = GenBitmapButton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span>, img, style=wx.<span style="color: black;">BORDER_NONE</span><span style="color: black;">&#41;</span>
        restartBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onRestart</span><span style="color: black;">&#41;</span>
&nbsp;
        cancelBtn = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Cancel&quot;</span><span style="color: black;">&#41;</span>
        cancelBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onCancel</span><span style="color: black;">&#41;</span>
&nbsp;
        vSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">buttonBuilder</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Logout&quot;</span>, logoutBtn<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">buttonBuilder</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Restart&quot;</span>, restartBtn<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        vSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        vSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>cancelBtn, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALIGN_RIGHT</span>|wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>vSizer<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> buttonBuilder<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label, button<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Creates a button with a label underneath it and puts them into
        a vertical BoxSizer, which is then returned
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        font = wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>button, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
&nbsp;
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=label<span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span>|wx.<span style="color: black;">BOTTOM</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> sizer
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onCancel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Close the dialog
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">GetParent</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onLogout<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Logs the current user out
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;shutdown -t 0 -l&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onRestart<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Restarts the PC
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;shutdown -r -t 1&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> RestarterFrame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">273</span>, <span style="color: #ff4500;">169</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        panel = RestarterPanel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Center</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Main<span style="color: black;">&#40;</span>wx.<span style="color: black;">App</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, redirect=<span style="color: #008000;">False</span>, filename=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">App</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, redirect, filename<span style="color: black;">&#41;</span>
        dlg = RestarterFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        dlg.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = Main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>I looked at a lot of icons for this project and I think I ended up just doing searches for <a href="http://commons.wikimedia.org/wiki/File:Quick_restart.png">shutdown </a>and <a href="http://www.iconfinder.net/icondetails/32136/128/?q=cute">logout </a>icons and downloaded the ones I liked the best. Both of these free or had Creative Commons licenses.</p>
<p>Now you know the tricks to restart local and remote machines. Use this knowledge wisely!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Python&#8217;s _winreg: Editing the Windows Registry</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 12:26:30 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=698</guid>
		<description><![CDATA[Python&#8217;s standard library is known for including lots of handy modules and packages that can be used without installing anything else. That&#8217;s one of the primary reasons that its standard library is called &#8220;batteries included&#8221; so often. So it should come as no surprise that Python includes a Windows only module for editing the Windows [...]]]></description>
			<content:encoded><![CDATA[<p>Python&#8217;s standard library is known for including lots of handy modules and packages that can be used without installing anything else. That&#8217;s one of the primary reasons that its standard library is called &#8220;batteries included&#8221; so often. So it should come as no surprise that Python includes a Windows only module for editing the Windows Registry. This particular module goes by the odd name of <em>_winreg</em> (odd because it starts with an underscore). In this article, we&#8217;ll learn the basics of working with the Registry using this &#8220;battery&#8221;. <span id="more-698"></span></p>
<h2>Reading from the Registry</h2>
<p>Using Python to read data from the registry is very easy. In the following example, we&#8217;ll find out where Outlook Express is installed:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
key = OpenKey<span style="color: black;">&#40;</span>HKEY_LOCAL_MACHINE, r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\O</span>utlook Express'</span>, <span style="color: #ff4500;">0</span>, KEY_ALL_ACCESS<span style="color: black;">&#41;</span>
QueryValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;InstallRoot&quot;</span><span style="color: black;">&#41;</span></pre>
<p>On my machine, this will return the following tuple: (u&#8217;%ProgramFiles%\\Outlook Express&#8217;, 2). The tuple is made up of the value and the registry type of said value. There are two other query methods that one can use called QueryInfoKey and QueryValue. The former gives you information about the key itself in form of three integers while the latter retrieves only the data for a key’s first value that has a NULL name. The <a href="http://docs.python.org/library/_winreg.html">documentation</a> recommends that you use QueryValueEx whenever possible. </p>
<p>We should probably quickly explain what&#8217;s going on in the code above as well. The OpenKey function takes an HKEY* constant, a subkey path string, a reserved integer (which must be zero) and the security mask. In this case, we passed in KEY_ALL_ACCESS which gives us complete control of that key. Since all we were doing was reading it, we probably should have just used KEY_READ though. As for what QueryValueEx does, it just accepts a key object and the field name that we want to query against.</p>
<h2>Writing to the Registry</h2>
<p>If you&#8217;ve been reading this blog lately, then you&#8217;ve probably already seen the _winreg module used for writing to the registry. This will just be review for you, so feel free to skip this section. We&#8217;ll start out with a practical example. In the following code snippet, we will set Internet Explorer&#8217;s home page. As always, please note that editing Registry entries can be dangerous. Be sure to back up your registry before attempting to edit it. Now, on with the show!</p>
<pre class="python">keyVal = r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\I</span>nternet Explorer<span style="color: #000099; font-weight: bold;">\M</span>ain'</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    key = OpenKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, keyVal, <span style="color: #ff4500;">0</span>, KEY_ALL_ACCESS<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span>:
    key = CreateKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, keyVal<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;Start Page&quot;</span>, <span style="color: #ff4500;">0</span>, REG_SZ, <span style="color: #483d8b;">&quot;http://www.blog.pythonlibrary.org/&quot;</span><span style="color: black;">&#41;</span>
CloseKey<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span></pre>
<p>In the code above, we attempt to open the following key: <em>HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main</em> and set the &#8220;Start Page&#8221; value to this blog. If the open fails, it&#8217;s usually because the key doesn&#8217;t exist, so we try to create the key in our exception handler. Then we use SetValueEx to actually set the value and like all good programmers, we clean up when we&#8217;re done and close the key. If you skipped the CloseKey command, you&#8217;d be fine in this case as the script is done and Python will do it for you. However, if you continued working on this key, you might have an access violation since it&#8217;s already open. Thus, the lesson is to always close a key when you finish editing it.</p>
<h2>Other _winreg Methods</h2>
<p>There are several other methods in the _winreg library worth pointing out. The DeleteKey method is handy when you need to remove a key. Unfortunately, I&#8217;ve had occasion when I need to remove keys recursively, such as when an uninstall goes bad and _winreg has no built-in way of doing that. You can write your own, of course, or you can download a wrapper like <a href="http://code.activestate.com/recipes/476229-yarw-yet-another-registry-wrapper/">YARW</a> (Yet Another Registry Wrapper) that can do it for you.</p>
<p>DeleteValue is similar to DeleteKey, except that you delete just a value. Yes, it&#8217;s pretty obvious. If you&#8217;d wanted to write your own recursive key deleting code, then you&#8217;d probably want to take a look at EnumKey and EnumValue as they enumerate keys and values respectively. Let&#8217;s take a quick look at how to use EnumKey:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> EnumKey, HKEY_USERS
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    i = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
        subkey = EnumKey<span style="color: black;">&#40;</span>HKEY_USERS, i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> subkey
        i += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
    <span style="color: #808080; font-style: italic;"># WindowsError: [Errno 259] No more data is available    </span>
    <span style="color: #ff7700;font-weight:bold;">pass</span></pre>
<p>The code above will loop through the HKEY_USERS hive printing the subkey to stdout until we reach the end of the hive and a WindowsError is raised. Of course, this does not descend into the subkeys, but I&#8217;ll leave that as an exercise for the reader to figure out.</p>
<p>The last method that we&#8217;ll talk about here is ConnectRegistry. This is helpful if we need to edit the Registry of a remote machine. It only accepts two arguments: the computer name and the key to connect to (i.e. HKEY_LOCAL_MACHINE or some such). Note that when connecting to remote machines, you can only edit certain keys while the others are unavailable. </p>
<h2>Wrapping Up</h2>
<p>I hope this was helpful for you and that it gave you lots of good ideas for your future projects. I have many login scripts that use this wonderful library and a couple that use YARW. It&#8217;s been quite useful so far and I hope that it will be the same for you.</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://docs.python.org/library/_winreg.html">Official documentation for _winreg</a></li>
<li><a href="http://effbot.org/librarybook/winreg.htm">Effbot tutorial on _winreg</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: Finding the Commit Charge Values in Windows</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 14:24:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=656</guid>
		<description><![CDATA[This week I was tasked with trying to find a way to find out what the Peak Commit value was on our virtual workstations. The reason being that we are trying to save money and were wondering if we were allocating too much memory or not. We didn&#8217;t need the Total Commit Charge or the [...]]]></description>
			<content:encoded><![CDATA[<p>This week I was tasked with trying to find a way to find out what the Peak Commit value was on our virtual workstations. The reason being that we are trying to save money and were wondering if we were allocating too much memory or not. We didn&#8217;t need the Total Commit Charge or the Limit Commit Charge values, but since I figured out how to get those during my research, I&#8217;ll show how to get those as well.<span id="more-656"></span></p>
<p>When I first started searching on this topic, I tried such search terms as &#8220;python peak commit value&#8221; and variations thereof. That got me nowhere, so I replaced &#8220;oython&#8221; with &#8220;wmi&#8221; and found the <a href="http://msdn.microsoft.com/en-us/library/aa394268%28VS.85%29.aspx">Win32_PerfFormattedData_PerfOS_Memory</a> Class on MSDN. I thought this was it, but it only gave me the Commit Charge Limit and the Total Commit Charge. Here&#8217;s how I got those values using Tim Golden&#8217;s WMI module:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> wmi
&nbsp;
c = wmi.<span style="color: black;">WMI</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> c.<span style="color: black;">Win32_PerfFormattedData_PerfOS_Memory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    commitChargeLimit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>item.<span style="color: black;">CommitLimit</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> c.<span style="color: black;">Win32_PerfFormattedData_PerfOS_Memory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    commitChargeTotal = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>item.<span style="color: black;">CommittedBytes</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Commit Charge Limit: &quot;</span>, commitChargeLimit
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Commit Charge Total: &quot;</span>, commitChargeTotal</pre>
<p>This is neat stuff and shows how easy it is to take the documentation at MSDN and translate it into something usable in Python. Unfortunately, it just didn&#8217;t give me the information I needed. My next stop was the <a href="http://mail.python.org/mailman/listinfo/python-win32">PyWin32 mailing list</a> where Mark Hammond informed me of the win32pdh and win32pdhutil modules. These expose performance counters, but I couldn&#8217;t find a way to use it to get this information either. Fortunately, I found an old post on the <a href="http://forum.sysinternals.com/forum_posts.asp?TID=15540&#038;PID=75852">sysinternals forum </a> that gave me a clue. Here is what it said:</p>
<p><quote><br />
The only way I&#8217;m aware of that one can get this detail is from the uMmPeakCommitLimit member of the SYSTEM_PERFORMANCE_INFORMATION structure one passes to NtQuerySystemInformation when calling it with the SystemPerformanceInformation type.<br />
</quote></p>
<p>I asked Mr. Hammond if that meant that I would need to use <a href="http://python.net/crew/theller/ctypes/">ctypes</a> since the <a href="http://msdn.microsoft.com/en-us/library/ms724509%28VS.85%29.aspx">NtQuerySystemInformation</a> class isn&#8217;t exposed by PyWin32 and he said &#8220;probably&#8221;. The ctypes module is pretty low-level and not something I&#8217;ve used except when I&#8217;ve copied a script from ActiveState. It&#8217;s a pretty handy module though and was added to the <a href="http://docs.python.org/library/ctypes.html">standard library</a> in version 2.5. From what I can tell, it was created by Thomas Heller.</p>
<p>Anyway, ctypes has its own <a href="https://lists.sourceforge.net/lists/listinfo/ctypes-users">mailing list</a>, so I decided to try there. I received two replies, one of which was from the man himself (Heller). He gave me a script that didn&#8217;t appear to work at first, but after a little back and forth with him, he got me straightened out. Here&#8217;s the result:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
SystemBasicInformation = <span style="color: #ff4500;">0</span>
SystemPerformanceInformation = <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SYSTEM_BASIC_INFORMATION<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
    _fields_ = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved1&quot;</span>, c_long <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NumberOfProcessors&quot;</span>, c_byte<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bUnknown2&quot;</span>, c_byte<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bUnknown3&quot;</span>, c_short<span style="color: black;">&#41;</span>
                <span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SYSTEM_PERFORMANCE_INFORMATION<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
    _fields_ = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;IdleTime&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ReadTransferCount&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WriteTransferCount&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;OtherTransferCount&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ReadOperationCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WriteOperationCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;OtherOperationCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;AvailablePages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalCommittedPages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalCommitLimit&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PeakCommitment&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PageFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WriteCopyFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TransitionFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved1&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DemandZeroFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagesRead&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PageReadIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved2&quot;</span>, c_ulong <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagefilePagesWritten&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagefilePageWriteIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MappedFilePagesWritten&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MappedFilePageWriteIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolUsage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NonPagedPoolUsage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolAllocs&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolFrees&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NonPagedPoolAllocs&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NonPagedPoolFrees&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalFreeSystemPtes&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SystemCodePage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalSystemDriverPages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalSystemCodePages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SmallNonPagedLookasideListAllocateHits&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SmallPagedLookasideListAllocateHits&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved3&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MmSystemCachePage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolPage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SystemDriverPage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadResourceMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadNotPossible&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadResourceMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadNotPossible&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinMappedDataCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ReadAheadIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;LazyWriteIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;LazyWritePages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DataFlushes&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DataPages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ContextSwitches&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FirstLevelTbFills&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SecondLevelTbFills&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SystemCalls&quot;</span>, c_ulong<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
sbi = SYSTEM_BASIC_INFORMATION<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
retlen = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
res = windll.<span style="color: black;">ntdll</span>.<span style="color: black;">NtQuerySystemInformation</span><span style="color: black;">&#40;</span>SystemBasicInformation,
                                            byref<span style="color: black;">&#40;</span>sbi<span style="color: black;">&#41;</span>,
                                            sizeof<span style="color: black;">&#40;</span>sbi<span style="color: black;">&#41;</span>,
                                            byref<span style="color: black;">&#40;</span>retlen<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> res, retlen
<span style="color: #ff7700;font-weight:bold;">print</span> sbi.<span style="color: black;">NumberOfProcessors</span>
&nbsp;
&nbsp;
spi = SYSTEM_PERFORMANCE_INFORMATION<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
retlen = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
res = windll.<span style="color: black;">ntdll</span>.<span style="color: black;">NtQuerySystemInformation</span><span style="color: black;">&#40;</span>SystemPerformanceInformation,
                                            byref<span style="color: black;">&#40;</span>spi<span style="color: black;">&#41;</span>,
                                            sizeof<span style="color: black;">&#40;</span>spi<span style="color: black;">&#41;</span>,
                                            byref<span style="color: black;">&#40;</span>retlen<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> res, retlen
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Peak commit: &quot;</span>,
<span style="color: #ff7700;font-weight:bold;">print</span> spi.<span style="color: black;">PeakCommitment</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">4096</span> / <span style="color: #ff4500;">1024</span></pre>
<p>I don&#8217;t really understand everything that&#8217;s going on here, but I&#8217;m glad it works. Well, I should say that it works on Windows XP Professional, 32-bit with Python 2.5. I tried it on Windows 7 64-bit as well and while the script ran, it returned &#8220;0L&#8221;. I&#8217;m guessing that 64-bit operating systems need a slightly different script, but since all our workstations currently use 32-bit, it doesn&#8217;t really matter at this point. Once again, the Python community came through for me and showed me just how amazing they are!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 26.377 seconds -->
