<?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; Python</title>
	<atom:link href="http://www.blog.pythonlibrary.org/tag/python/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>Another GUI2Exe Tutorial &#8211; Build a Binary Series!</title>
		<link>http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 00:09:58 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Packaging]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[binaries]]></category>
		<category><![CDATA[Distribution]]></category>
		<category><![CDATA[GUI2Exe]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1163</guid>
		<description><![CDATA[This is the last article of my &#8220;Build a Binary Series&#8221;. If you haven&#8217;t done so already, be sure to check out the others. For our finale, we are to look at Andrea Gavana&#8217;s wxPython-based GUI2Exe, a nice graphical user interface to py2exe, bbfreeze, cx_Freeze, PyInstaller and py2app. The latest release of GUI2Exe is 0.5.0, [...]]]></description>
			<content:encoded><![CDATA[<p>This is the last article of my &#8220;Build a Binary Series&#8221;. If you haven&#8217;t done so already, be sure to check out the others. For our finale, we are to look at Andrea Gavana&#8217;s wxPython-based GUI2Exe, a nice graphical user interface to py2exe, bbfreeze, cx_Freeze, PyInstaller and py2app. The latest release of GUI2Exe is 0.5.0, although the source may be slightly newer. Feel free to run from the tip as well. We&#8217;ll be using the example scripts that we used for several of the previous articles: one console and one GUI script, neither of which do much of anything.<span id="more-1163"></span></p>
<h2>Getting Started with GUI2Exe</h2>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe-300x217.png" alt="" title="gui2exe.png" width="300" height="217" class="aligncenter size-medium wp-image-1165" /></a></p>
<p>Quite some time ago, I wrote another article on this cool tool. However, the look-and-feel of the application has changed quite a bit, so I felt I should re-write that article in the context of this series. To follow along with this article, you&#8217;ll need to hit <a href="http://code.google.com/p/gui2exe/">Google Code</a> for the source. Let&#8217;s begin, shall we? Here are some step-by-step directions for making the console script using py2exe via GUI2Exe:</p>
<ol>
<li>Download the source and unzip them in a convenient location</li>
<li>Run the &#8220;GUI2Exe.py&#8221; file (you can use your favorite editor, open it via the command line or whatever)</li>
<li>Go to File, New Project. A dialog will appear asking you to name the project. Give it a good name! Then hit OK.</li>
<li>Click in the &#8220;Exe Kind&#8221; column and change it to &#8220;Console&#8221;</li>
<li>Click in the &#8220;Python Main Script&#8221; column and you&#8217;ll see a button appear.</li>
<li>Press the button and use the file dialog to find your main script</li>
<li>Fill out the other optional fields however you like</li>
<li>Hit the compile button on the lower right</li>
<li>Try the result to see if it worked!</li>
</ol>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_options.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_options-300x187.png" alt="" title="gui2exe_options" width="300" height="187" class="aligncenter size-medium wp-image-1170" /></a></p>
<p>If you followed the directions above, you should now have an executable file (and a few dependencies) in a &#8220;dist&#8221; folder at the location of the main script. As you can see in the screenshot above, there are all the typical options that you would set in your setup.py file. You can set your excludes list, the includes, the optimize and compressed settings, whether or not to include a zip, packages and much more! You can tweak to your hearts content and hit the &#8220;Compile&#8221; button whenever you&#8217;re ready to see the result. If I&#8217;m experimenting, I usually change the output directory&#8217;s name so I can compare the results to see which is the most compact.</p>
<p>If you want to use bbfreeze, cx_freeze, PyInstaller or py2app, just click the respective name in the column on the right. This will cause the middle part of the screen to change according to your choice and show the corresponding options for said choice. Let&#8217;s take a quick visual tour!</p>
<h2>GUI2Exe in Pictures!</h2>
<p>The following is a snapshot of the py2app options:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_py2app.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_py2app-300x233.png" alt="" title="gui2exe_py2app" width="300" height="233" class="aligncenter size-medium wp-image-1171" /></a></p>
<p>Next is a shot of the cx_Freeze options:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_cxfreeze.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_cxfreeze-300x229.png" alt="gui2exe_cxfreeze.png" title="gui2exe_cxfreeze.png" width="300" height="229" class="aligncenter size-medium wp-image-1172" /></a></p>
<p>And here is PyInstaller&#8217;s settings:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_pyinst.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_pyinst-300x233.png" alt="gui2exe_pyinst.png" title="gui2exe_pyinst.png" width="300" height="233" class="aligncenter size-medium wp-image-1173" /></a></p>
<p>Finally, we have bbfreeze&#8217;s options:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_bbfreeze.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_bbfreeze-300x229.png" alt="gui2exe_bbfreeze.png" title="gui2exe_bbfreeze.png" width="300" height="229" class="aligncenter size-medium wp-image-1175" /></a></p>
<p>There&#8217;s also a VendorId screen, but I don&#8217;t know much about that one, so we&#8217;ll be skipping it.</p>
<h2>GUI2Exe&#8217;s Menu Options</h2>
<p>As you might guess, all these options work the same way as they do when you do it all yourself in code. If you ever need to check out the setup.py file that GUI2Exe is making for you, just go to the <strong>Builds</strong> menu and choose <strong>View Setup Script</strong>. If you want to see a handy listing of the files it output and where it output the files, go to Build, Explorer and you should see something like the screenshot below:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_explorer.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/gui2exe_explorer-300x294.png" alt="gui2exe_explorer.png" title="gui2exe_explorer.png" width="300" height="294" class="aligncenter size-medium wp-image-1176" /></a></p>
<p>Other handy option in the Builds menu include the Mission Modules and the Binary Dependencies menu items. These show you what may be missing from the dist folder that you may need to include should you distribute your masterpiece.</p>
<p>The <strong>Options </strong>menu controls options for GUI2Exe itself and a few custom items for the build process, like setting the Python version, deleting the build and/or dist folders, set the PyInstaller Path and more. The other menus are pretty self-explanatory and I leave them for the adventurous readers.</p>
<h2>Wrapping Up</h2>
<p>If you&#8217;ve read my other tutorials in the &#8220;Build a Binary Series&#8221; then you should be able to take that knowledge and use it productively with GUI2Exe. I find GUI2Exe to be very helpful when it comes time for me to build an executable and I used it to help me figure out the options for some of the other binary builders in this series. I hope you enjoyed this series and found it helpful. See you next time!</p>
<h2>Further Reading</h2>
<ul>
<li>GUI2Exe Official <a href="http://code.google.com/p/gui2exe/">website</a></li>
<li>Andrea Gavana&#8217;s <a href="http://xoomer.virgilio.it/infinity77/">website</a> and <a href="http://thedoomedcity.blogspot.com/">blog</a></li>
<li><a href="http://www.blog.pythonlibrary.org/tag/binaries/">Build a Binary Series</a></li>
<li>The other <a href="http://www.blog.pythonlibrary.org/2008/08/27/packaging-wxpymail-for-distribution/">GUI2Exe </a>tutorial</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Book Review: Python 3 Object Oriented Programming</title>
		<link>http://www.blog.pythonlibrary.org/2010/08/22/book-review-python-3-object-oriented-programming/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/08/22/book-review-python-3-object-oriented-programming/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 18:48:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Book Review]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1152</guid>
		<description><![CDATA[Python 3 Object Oriented Programming By Dusty Phillips Amazon Packt A few months ago, I wrote a preview of this book. A little over a week ago, I received my copy of the book. Before we get to the review though, I want to give full disclosure: Packt contacted me to be a &#8220;Reviewer&#8221; (read: [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/python3_oop.jpg"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/python3_oop-243x300.jpg" alt="" title="python3_oop" width="243" height="300" class="aligncenter size-medium wp-image-1154" /></a></td>
<td>
<h3>Python 3 Object Oriented Programming</h3>
<p><p>By Dusty Phillips</p>
<p><strong><a href="http://www.amazon.com/exec/obidos/ASIN/1849511268/thmovsthpy-20/">Amazon</a></strong></p>
<p><strong><a href="http://www.packtpub.com/python-3-object-oriented-programming/book?utm_source=blog.pythonlibrary.org&#038;utm_medium=bookrev&#038;utm_content=blog&#038;utm_campaign=mdb_004190">Packt</a></strong></br>
</td>
</tr>
</table>
<p><span id="more-1152"></span><br />
A few months ago, I wrote a <a href="http://www.blog.pythonlibrary.org/2010/05/20/book-preview-python-3-object-oriented-programming/">preview </a>of this book. A little over a week ago, I received my copy of the book. Before we get to the review though, I want to give full disclosure: Packt contacted me to be a &#8220;Reviewer&#8221; (read: technical editor) for this book last December. The payment? They gave me a copy of the book, one copy of any other book from their catalog that I wanted and my name appears in the book. If you think that remuneration will cause me to biased, so be it. Everyone is biased in some way or another. I tend to be harsher on things that I&#8217;m involved in though.</p>
<p>Well, enough of the baring of my soul. Let&#8217;s take a look at the book. The author of this book really likes Unified Modeling Language (UML) class diagrams, so if you find those helpful in learning, then you&#8217;ll like this book. The entire first chapter is made up of text and UML diagrams, after all and it gives a brief introduction to the Object Oriented philosophy. Chapter 2 jumps into Python Objects, modules and packages. Chapter 3 digs into inheritance (basic and multiple) and polymorphism. Chapter 4 is all about exceptions in Python. Chapter 5 attempts to explain when to use object oriented programming. I thought that was an interesting chapter because it seems to me that colleges browbeat their students into thinking they should use OOP for everything. Chapters 6 and 7 dig into the Python standard library by covering tuples, lists, dicts, sets, various comprehensions, generators and functions. I learned a lot about comprehensions from these chapters that made me wish I used Python 3 more. Chapters 8 and 9 cover Python Design Patterns. I found these chapters to be the highlight of the book and very interesting. He covers the following patterns: decorator,  observer, strategy, state, singleton, template, adapter, facade, flyweight, command, abstract factory and composite.</p>
<p>Chapter 10 covers the basics of files and strings; chapter 11 goes overs testing with unit tests and py.test. Last, chapter 12 does a quick overview of common Python 3 libraries, like SqlAlchemy, Tkinter, PyQt, lxml and CherryPy.</p>
<p>When I was reviewing the book, Chapters 5 and 8 had some Python Imaging Library examples. The author had noted that PIL hadn&#8217;t been ported to Python 3 at the time, but he thought it would be by publishing. Alas, the people behind PIL still do not have a Python 3 port. So I was curious how the author handled this conundrum. It turns out that he ended up replacing PIL with pygame. An interesting choice that appears to work all right for the examples. I didn&#8217;t re-read the book (although I plan to at some point). Instead, I just did a spot check to see if the author followed any of my suggestions or fixed errors. It appears that he did.</p>
<p>This book was good before he fixed the problems with it. There&#8217;s more theory here than you normally find in Python textbooks, but I found the examples intriguing and he has unusual exercises at the end of the chapters that should keep you thinking. I&#8217;m pretty sure I would have been quite happy if I had purchased this book. If you&#8217;re new to Python 3 and specifically object oriented programming or you want to learn about programming patterns, I recommend this book. If you want a sneak peak, Packt has Chapter 7 available on their website <a href="https://www.packtpub.com/sites/default/files/1261-chapter-7-Python%20object-oriented-shortcuts.pdf">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/08/22/book-review-python-3-object-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>Book Review: Practical Programming: An Introduction to Computer Science Using Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/08/14/book-review-practical-programming-an-introduction-to-computer-science-using-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/08/14/book-review-practical-programming-an-introduction-to-computer-science-using-python/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 13:46:50 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Computer Science]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1115</guid>
		<description><![CDATA[Practical Programming: An Introduction to Computer Science Using Python By Jennifer Campbell, Paul Gries, Jason Montojo and Greg Wilson Amazon I received the book, Practical Programming: An Introduction to Computer Science Using Python as a gift last month from a family member. This book is by four authors: Jennifer Campbell, Paul Gries, Jason Montojo and [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/pragmaticPython.jpg"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/08/pragmaticPython.jpg" alt="Pragmatic Python Cover  Image" title="pragmaticPython.jpg" width="300" height="300" class="aligncenter size-full wp-image-1126" /></a></td>
<td>
<h3>Practical Programming: An Introduction to Computer Science Using Python</h3>
<p><p>By Jennifer Campbell, Paul Gries, Jason Montojo and Greg Wilson</p>
<p><strong><a href="http://www.amazon.com/exec/obidos/ASIN/1934356271/thmovsthpy-20/7">Amazon</a></strong></p>
</td>
</tr>
</table>
<p>I received the book, <em>Practical Programming: An Introduction to Computer Science Using Python</em> as a gift last month from a family member. This book is by four authors: Jennifer Campbell, Paul Gries, Jason Montojo and Greg Wilson. I&#8217;d been interested in reading another Python-related textbook for a while, and this happens to be one of the few out there. The only other Python textbook I&#8217;ve read is <em><a href="http://www.amazon.com/exec/obidos/ASIN/1590282418/thmovsthpy-20/">Python Programming: An Introduction to Computer Science</a></em> by John Zelle. I&#8217;m aware of one or two others, but those books are beyond what I&#8217;m willing to pay.<span id="more-1115"></span></p>
<p>Normally when I see something that required multiple authors, the results are less than stellar. However, this book does a pretty good job of introducing students to computer science using Python. Of course, when I was taking classes in computer science, we spent multiple classes on just theory. Most of this book is on learning the basics of Python, such as how to use strings, lists, dicts, tuples, etc. So if you were hoping to learn algorithms and data structures using Python, you won&#8217;t be finding much of that here. There is one chapter on algorithms that covers sorts and search algorithms though, and I thought that was one of the highlights of the book.</p>
<p>On the other hand, you will get a nice introduction to one of the easiest and most fun to use programming languages ever created. Not only do you get a nice introduction, but each chapter ends with a series of exercises based on the text of the chapter. I like having review questions or problems to solve that make what I learned stick in my brain. I mostly skimmed the exercises though because the chapters were designed for college Freshmen with no programming experience.</p>
<p>This book covers basic testing in two places. It&#8217;s not thorough, but it does give the reader a taste and I think it conveys the reasoning behind testing fairly well. At the end of the book, they get a little more topical on us. The book gives an overview of the following topics: debugging, the object-oriented philosophy (classes, inheritance, polymorphism), graphical user interfaces using Tkinter, and databases (sqlite). </p>
<p>After reading Dusty Phillips&#8217; <a href="http://www.amazon.com/exec/obidos/ASIN/1849511268/thmovsthpy-20/">book </a>on Object Oriented Programming with Python, I found the one chapter in this book lacking. However, it is typical of an introduction, so I can&#8217;t knock it too much. The Tkinter chapter was fun as I have been planning on brushing up on that for other articles on this blog. The authors even spend some time on the Model-Viewer-Controller paradigm. I didn&#8217;t really like how they laid out the code though since it was hard to tell which code fit where in the whole MVC schema. The database section was almost straight-up SQL code using Python 2.5&#8242;s sqlite module. While there&#8217;s nothing particularly bad with that approach, I know I would have been a little confused by it because of all the new syntax that doesn&#8217;t match anything I had learned previously in the book.</p>
<p>There are some instances of weird or poor grammar, but I didn&#8217;t find any obvious coding errors that weren&#8217;t there on purpose. The book is priced reasonably, especially considering that it&#8217;s supposed to be a college textbook. If you are new to computer science or to Python, I think you&#8217;ll find this book helpful. If you&#8217;re an experienced programmer or you&#8217;ve already had several courses in computer science, than you&#8217;ll probably want to skip this book (unless you&#8217;re looking to learn Python).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/08/14/book-review-practical-programming-an-introduction-to-computer-science-using-python/feed/</wfw:commentRss>
		<slash:comments>3</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>August Pyowa Meeting Wrapup</title>
		<link>http://www.blog.pythonlibrary.org/2010/08/06/august-pyowa-meeting-wrapup/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/08/06/august-pyowa-meeting-wrapup/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 13:19:25 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Pyowa]]></category>
		<category><![CDATA[local user groups]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1098</guid>
		<description><![CDATA[We had our August 2010 Pyowa meeting last night in Ames at the Ames Public Library. Seven people attended the meeting, most of whom were regulars. I think we had one new guy or maybe he&#8217;s only been to one. Anyway, Scott presented on SqlAlchemy. He walked us through the basics using a movies example [...]]]></description>
			<content:encoded><![CDATA[<p>We had our August 2010 Pyowa meeting last night in Ames at the Ames Public Library. Seven people attended the meeting, most of whom were regulars. I think we had one new guy or maybe he&#8217;s only been to one. Anyway, Scott presented on <a href="http://www.sqlalchemy.org">SqlAlchemy</a>. He walked us through the basics using a movies example that I think get found on Jonathan Ellis&#8217;s <a href="http://spyced.blogspot.com/">site</a>. He also talked about SqlSoup, Migrate, a couple other extensions and SqlAlchemy&#8217;s TurboGears 2 integration. In fact, he demoed a couple TurboGears applications as well.</p>
<p>Our next meeting will be in Des Moines at the IMT Group building.  We are currently getting all the details nailed down for that, so be sure to check out the Pyowa <a href="http://www.pyowa.org">website </a>for updates or join our <a href="http://pyowalist.pythonlibrary.org/listinfo.cgi/pyowa-pythonlibrary.org">mailing list</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/08/06/august-pyowa-meeting-wrapup/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>wxPython: Creating a Simple Media Player</title>
		<link>http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 16:07:20 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[media player]]></category>
		<category><![CDATA[mplayer]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1058</guid>
		<description><![CDATA[A few months ago, I wrote about creating a simple MP3 Player using wxPython&#8217;s MediaCtrl widget. Since then, a fellow released the MplayerCtrl, a wxPython widget that wraps mplayer, a popular cross-platform media player. I actually ended up switching my MP3 Player&#8217;s backend to use this new control, but that&#8217;s a story for another post. [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago, I wrote about creating a simple MP3 Player using wxPython&#8217;s MediaCtrl widget. Since then, a fellow released the MplayerCtrl, a wxPython widget that wraps mplayer, a popular cross-platform media player. I actually ended up switching my MP3 Player&#8217;s backend to use this new control, but that&#8217;s a story for another post. This article will just focus on creating a really simple Media Player that you can play movies with. And you can do it all with Python! If you&#8217;re like me, you&#8217;ll think this rocks!<span id="more-1058"></span></p>
<h2>Playing Movies with Python &#038; Mplayer</h2>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/media_player.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/media_player.png" alt="" title="media_player" width="500" height="371" class="aligncenter size-full wp-image-1061" /></a><br />
<a href="http://www.imdb.com/title/tt1100119/">Screenshot is from a trailer for Shorts</a></p>
<p>First off, you&#8217;ll need to make sure you have the following:</p>
<ul>
<li><a href="http://www.python.org">Python </a>(duh!)</li>
<li><a href="http://www.wxpython.org">wxPython</a></li>
<li><a href="http://www.mplayerhq.hu">mplayer</a></li>
<li><a href="http://pypi.python.org/pypi/MplayerCtrl/">MPlayerCtrl</a></li>
</ul>
<p>Once you&#8217;ve confirmed that you have all that, we can continue. Let&#8217;s look at some code!</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> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> wx
<span style="color: #ff7700;font-weight:bold;">import</span> MplayerCtrl as mpc
<span style="color: #ff7700;font-weight:bold;">import</span> wx.<span style="color: black;">lib</span>.<span style="color: black;">buttons</span> as buttons
&nbsp;
dirName = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
bitmapDir = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>dirName, <span style="color: #483d8b;">'bitmaps'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Frame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</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> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent, <span style="color: #008000;">id</span>, title, mplayer<span style="color: black;">&#41;</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>, parent, <span style="color: #008000;">id</span>, title<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">panel</span> = wx.<span style="color: black;">Panel</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
        sp = wx.<span style="color: black;">StandardPaths</span>.<span style="color: black;">Get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">currentFolder</span> = sp.<span style="color: black;">GetDocumentsDir</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">currentVolume</span> = <span style="color: #ff4500;">50</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">create_menu</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create sizers</span>
        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>
        controlSizer = <span style="color: #008000;">self</span>.<span style="color: black;">build_controls</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        sliderSizer = 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>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span> = mpc.<span style="color: black;">MplayerCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, <span style="color: #ff4500;">-1</span>, mplayer<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">playbackSlider</span> = wx.<span style="color: black;">Slider</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, size=wx.<span style="color: black;">DefaultSize</span><span style="color: black;">&#41;</span>
        sliderSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">playbackSlider</span>, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create volume control</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">volumeCtrl</span> = wx.<span style="color: black;">Slider</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">volumeCtrl</span>.<span style="color: black;">SetRange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">volumeCtrl</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">currentVolume</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">volumeCtrl</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_SLIDER</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_set_volume</span><span style="color: black;">&#41;</span>
        controlSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">volumeCtrl</span>, <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>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create track counter</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">trackCounter</span> = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, label=<span style="color: #483d8b;">&quot;00:00&quot;</span><span style="color: black;">&#41;</span>
        sliderSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">trackCounter</span>, <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>
&nbsp;
        <span style="color: #808080; font-style: italic;"># set up playback timer</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span> = wx.<span style="color: black;">Timer</span><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;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_TIMER</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_update_playback</span><span style="color: black;">&#41;</span>
&nbsp;
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sliderSizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>controlSizer, <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: #008000;">self</span>.<span style="color: black;">panel</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>mainSizer<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>mpc.<span style="color: black;">EVT_MEDIA_STARTED</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_media_started</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>mpc.<span style="color: black;">EVT_MEDIA_FINISHED</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_media_finished</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>mpc.<span style="color: black;">EVT_PROCESS_STARTED</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_process_started</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>mpc.<span style="color: black;">EVT_PROCESS_STOPPED</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_process_stopped</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">panel</span>.<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> build_btn<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, btnDict, sizer<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>
        bmp = btnDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">'bitmap'</span><span style="color: black;">&#93;</span>
        handler = btnDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">'handler'</span><span style="color: black;">&#93;</span>
&nbsp;
        img = wx.<span style="color: black;">Bitmap</span><span style="color: black;">&#40;</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>bitmapDir, bmp<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        btn = buttons.<span style="color: black;">GenBitmapButton</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, bitmap=img,
                                      name=btnDict<span style="color: black;">&#91;</span><span style="color: #483d8b;">'name'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        btn.<span style="color: black;">SetInitialSize</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        btn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, handler<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>btn, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">LEFT</span>, <span style="color: #ff4500;">3</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> build_controls<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;
        Builds the audio bar controls
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        controlSizer = 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>
&nbsp;
        btnData = <span style="color: black;">&#91;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'bitmap'</span>:<span style="color: #483d8b;">'player_pause.png'</span>,
                    <span style="color: #483d8b;">'handler'</span>:<span style="color: #008000;">self</span>.<span style="color: black;">on_pause</span>, <span style="color: #483d8b;">'name'</span>:<span style="color: #483d8b;">'pause'</span><span style="color: black;">&#125;</span>,
                   <span style="color: black;">&#123;</span><span style="color: #483d8b;">'bitmap'</span>:<span style="color: #483d8b;">'player_stop.png'</span>,
                    <span style="color: #483d8b;">'handler'</span>:<span style="color: #008000;">self</span>.<span style="color: black;">on_stop</span>, <span style="color: #483d8b;">'name'</span>:<span style="color: #483d8b;">'stop'</span><span style="color: black;">&#125;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> btn <span style="color: #ff7700;font-weight:bold;">in</span> btnData:
            <span style="color: #008000;">self</span>.<span style="color: black;">build_btn</span><span style="color: black;">&#40;</span>btn, controlSizer<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> controlSizer
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> create_menu<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;
        Creates a menu
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        menubar = wx.<span style="color: black;">MenuBar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        fileMenu = wx.<span style="color: black;">Menu</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        add_file_menu_item = fileMenu.<span style="color: black;">Append</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">NewId</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;&amp;Add File&quot;</span>, <span style="color: #483d8b;">&quot;Add Media File&quot;</span><span style="color: black;">&#41;</span>
        menubar.<span style="color: black;">Append</span><span style="color: black;">&#40;</span>fileMenu, <span style="color: #483d8b;">'&amp;File'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">SetMenuBar</span><span style="color: black;">&#40;</span>menubar<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_MENU</span>, <span style="color: #008000;">self</span>.<span style="color: black;">on_add_file</span>, add_file_menu_item<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> on_add_file<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;
        Add a Movie and start playing it
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wildcard = <span style="color: #483d8b;">&quot;Media Files (*.*)|*.*&quot;</span>
        dlg = wx.<span style="color: black;">FileDialog</span><span style="color: black;">&#40;</span>
            <span style="color: #008000;">self</span>, message=<span style="color: #483d8b;">&quot;Choose a file&quot;</span>,
            defaultDir=<span style="color: #008000;">self</span>.<span style="color: black;">currentFolder</span>,
            defaultFile=<span style="color: #483d8b;">&quot;&quot;</span>,
            wildcard=wildcard,
            style=wx.<span style="color: black;">OPEN</span> | wx.<span style="color: black;">CHANGE_DIR</span>
            <span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> dlg.<span style="color: black;">ShowModal</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == wx.<span style="color: black;">ID_OK</span>:
            path = dlg.<span style="color: black;">GetPath</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">currentFolder</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            trackPath = <span style="color: #483d8b;">'&quot;%s&quot;'</span> <span style="color: #66cc66;">%</span> path.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span>, <span style="color: #483d8b;">&quot;/&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">Loadfile</span><span style="color: black;">&#40;</span>trackPath<span style="color: black;">&#41;</span>
&nbsp;
            t_len = <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">GetTimeLength</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">playbackSlider</span>.<span style="color: black;">SetRange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, t_len<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span>.<span style="color: black;">Start</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> on_media_started<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Media started!'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> on_media_finished<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Media finished!'</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span>.<span style="color: black;">Stop</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> on_pause<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;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span>.<span style="color: black;">IsRunning</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;pausing...&quot;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">Pause</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span>.<span style="color: black;">Stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;unpausing...&quot;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">Pause</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span>.<span style="color: black;">Start</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> on_process_started<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Process started!'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> on_process_stopped<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Process stopped!'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> on_set_volume<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;
        Sets the volume of the music player
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">currentVolume</span> = <span style="color: #008000;">self</span>.<span style="color: black;">volumeCtrl</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">SetProperty</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;volume&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">currentVolume</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> on_stop<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;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;stopping...&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">Stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">playbackTimer</span>.<span style="color: black;">Stop</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> on_update_playback<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;
        Updates playback slider and track counter
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            offset = <span style="color: #008000;">self</span>.<span style="color: black;">mplayer</span>.<span style="color: black;">GetTimePos</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> offset
        mod_off = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>offset<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> mod_off == <span style="color: #483d8b;">'0'</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;mod_off&quot;</span>
            offset = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>offset<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">playbackSlider</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>offset<span style="color: black;">&#41;</span>
            secsPlayed = <span style="color: #dc143c;">time</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%M:%S'</span>, <span style="color: #dc143c;">time</span>.<span style="color: black;">gmtime</span><span style="color: black;">&#40;</span>offset<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">trackCounter</span>.<span style="color: black;">SetLabel</span><span style="color: black;">&#40;</span>secsPlayed<span style="color: black;">&#41;</span></pre>
<p>We&#8217;re going to focus on the hightlights and ignore the boilerplate. If you don&#8217;t know wxPython code, then go to the wxPython website and read the tutorials. Now, to create an instance of the MplayerCtrl, we need to call <em>mpc.MplayerCtrl(self.panel, -1, mplayer)</em> and pass in the path to the mplayer. To keep track of how much of the movie has played, we create a wx.Slider. We also use a slider for the volume control. We use some of wx&#8217;s generic button widgets to give us some easy-to-use bitmap buttons. Finally, we create a timer that we&#8217;ll use to update the playback slider&#8217;s position. To load a movie and start it playing, we create a menu item.</p>
<p>The <em>build_btn</em>, <em>build_controls</em>, and <em>create_menu</em> are helper methods that build our GUI. You can figure those out for yourself. We&#8217;ll just look at a few of the mplayer-related methods. In <em>on_add_file</em>, we open a FileDialog and let the user find a movie file on their PC. Once they&#8217;ve picked one out, we hack the path to the file slightly to make it more palatable to mplayer by replacing backslashes with forward slashes. Then we call our MPlayerCtrl&#8217;s LoadFile method to load the movie into mplayer and get it started. We also start our timer and update the playback slider.</p>
<p>You can Pause or Stop the playback with the two buttons that call the appropriate methods. The MplayerCtrl also exposes lots of other methods that we can use to get the length of the movie, get the bitrate, the codec, and more. Check out the documentation to see what all you can accomplish.</p>
<h2>Wrapping Up</h2>
<p>As you can see, creating a simple media player in Python is really easy. Getting the MplayerCtrl to play something takes about 5 minutes. Adding the sliders and functions add 30-45 minutes (depending on your skillz). Feel free to add enhancements, like a playlist, a way to speed up or slow down the playback or get the frame to resize the movie so its aspect ratio is constant. Note that sometimes mplayer doesn&#8217;t want to kill the movie when you close the frame. This is a bug which the author of the control is trying to figure out. Anyway, have fun watching movies with Python!</p>
<p><em>Note: This code was tested with MplayerCtrl 0.1.2 and 0.2.4, wxPython 2.8.10.1, Python 2.5/2.6 on Windows XP and Windows 7. I used the mplayer rtm-svn-31170 version in my testing as well.</em></p>
<h2>Further Information</h2>
<ul>
<li>MplayerCtrl <a href="http://mplayerctrl.dav1d.de/docs/">Documentation</a></li>
</ul>
<h2>Downloads</h2>
<ul>
<li><a href='http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/MediaPlayer.zip'>MediaPlayer.zip</a></li>
<li><a href='http://www.blog.pythonlibrary.org/wp-content/uploads/2010/07/MediaPlayer.tar'>MediaPlayer.tar</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Python Lambda</title>
		<link>http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 01:17:59 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[python basics]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1051</guid>
		<description><![CDATA[When I first started learning Python, one of the most confusing concepts to get my head around was the lambda statement. I&#8217;m sure other new programmers get confused by it as well and some of you are probably wondering what I&#8217;m talking about. So, in the spirit of education, let&#8217;s have a pop quiz: Q. [...]]]></description>
			<content:encoded><![CDATA[<p>When I first started learning Python, one of the most confusing concepts to get my head around was the lambda statement. I&#8217;m sure other new programmers get confused by it as well and some of you are probably wondering what I&#8217;m talking about. So, in the spirit of education, let&#8217;s have a pop quiz:</p>
<p>Q. What is a lambda?</p>
<p>A. the 11th letter of the Greek alphabet<br />
B. the craniometric point at the junction of the sagittal and lamboid sutures of the skull<br />
C. the driver in an Arm Slave mecha that allows it to change the user&#8217;s thoughts into reality<br />
D. the name of a series of Japanese rocket<br />
E. anonymous (unbound) functions</p>
<p>If you guessed, <em>F &#8211; all of the above</em>, you got it right! Of course, in the context of this article, &#8220;E&#8221; is really the right answer. The Python lambda statement is an anonymous or unbound function and a pretty limited function at that. Let&#8217;s take a look at a few typical examples and see if we can find a use case for it.<span id="more-1051"></span></p>
<p>The typical examples that one normally sees for teaching the lambda is some kind of boring doubling function. Just to be contrary, our simple example will show how to find the square root. First we&#8217;ll show a normal function and then the lambda equivalent:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> sqroot<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Finds the square root of the number passed in
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">math</span>.<span style="color: black;">sqrt</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
&nbsp;
square_rt = <span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: #dc143c;">math</span>.<span style="color: black;">sqrt</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span></pre>
<p>If you try each of these functions, you&#8217;ll end up with a float. Here are a couple examples:</p>
<p><code><br />
&gt;&gt;&gt; sqroot(49)<br />
7.0<br />
&gt;&gt;&gt; square_rt(64)<br />
8.0<br />
</code></p>
<p>Pretty slick, right? But where would we actually use a lambda in real life? Maybe a calculator program? Well, that would work, but it&#8217;s a pretty limited application for a builtin of Python! One of the major pieces of Python that lambda examples are applied to regularly are Tkinter callbacks. We&#8217;ll take a look at that, but we&#8217;ll also take that information and try it with wxPython to see if works there just as well.</p>
<h2>Tkinter + lambda</h2>
<p>We&#8217;ll start with Tkinter since it&#8217;s included with the standard Python package. Here&#8217;s a really simple script with three buttons, two of which are bound to their event handler using a lambda:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">Tkinter</span> as tk
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> App:
    <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>
        frame = tk.<span style="color: black;">Frame</span><span style="color: black;">&#40;</span>parent<span style="color: black;">&#41;</span>
        frame.<span style="color: black;">pack</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        btn22 = tk.<span style="color: black;">Button</span><span style="color: black;">&#40;</span>frame, text=<span style="color: #483d8b;">&quot;22&quot;</span>, command=<span style="color: #ff7700;font-weight:bold;">lambda</span>: <span style="color: #008000;">self</span>.<span style="color: black;">printNum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">22</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        btn22.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>side=tk.<span style="color: black;">LEFT</span><span style="color: black;">&#41;</span>
        btn44 = tk.<span style="color: black;">Button</span><span style="color: black;">&#40;</span>frame, text=<span style="color: #483d8b;">&quot;44&quot;</span>, command=<span style="color: #ff7700;font-weight:bold;">lambda</span>: <span style="color: #008000;">self</span>.<span style="color: black;">printNum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">44</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        btn44.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>side=tk.<span style="color: black;">LEFT</span><span style="color: black;">&#41;</span>
&nbsp;
        quitBtn = tk.<span style="color: black;">Button</span><span style="color: black;">&#40;</span>frame, text=<span style="color: #483d8b;">&quot;QUIT&quot;</span>, fg=<span style="color: #483d8b;">&quot;red&quot;</span>, command=frame.<span style="color: black;">quit</span><span style="color: black;">&#41;</span>
        quitBtn.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>side=tk.<span style="color: black;">LEFT</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> printNum<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, num<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;">print</span> <span style="color: #483d8b;">&quot;You pressed the %s button&quot;</span> <span style="color: #66cc66;">%</span> num
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    root = tk.<span style="color: black;">Tk</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app = App<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
    root.<span style="color: black;">mainloop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Notice the btn22 and btn44 variables. This is where the action is. We create a <em>tk.Button</em> instance here and bind to our <em>printNum </em>method in one fell swoop. The lambda is assigned to the button&#8217;s <em>command</em> parameter. What this means is that we&#8217;re creating a one-off function for the command, much like in the quit button where we call the frame&#8217;s quit method. The difference here is that this particular lambda is a method that calls another method and passes the latter an integer. In the <em>printNum</em> method, we print to stdout which button was pressed by using the information that was passed to it from the lambda function. Did you follow all that? If so, we can continue&#8230;if not, re-read this paragraph as many times as necessary until the information sinks in or you go crazy, whichever comes first. </p>
<h2>wxPython + lambda</h2>
<p>Our wxPython example is pretty similar to the Tkinter one, only a little more verbose:</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> 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;wx lambda 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 = wx.<span style="color: black;">Panel</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
        button8 = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span>panel, label=<span style="color: #483d8b;">&quot;8&quot;</span><span style="color: black;">&#41;</span>
        button8.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #ff7700;font-weight:bold;">lambda</span> evt, name=button8.<span style="color: black;">GetLabel</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: <span style="color: #008000;">self</span>.<span style="color: black;">onButton</span><span style="color: black;">&#40;</span>evt, name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        button10 = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span>panel, label=<span style="color: #483d8b;">&quot;10&quot;</span><span style="color: black;">&#41;</span>
        button10.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #ff7700;font-weight:bold;">lambda</span> evt, name=button10.<span style="color: black;">GetLabel</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: <span style="color: #008000;">self</span>.<span style="color: black;">onButton</span><span style="color: black;">&#40;</span>evt, name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        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>button8, <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>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>button10, <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>
        panel.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>sizer<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onButton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event, buttonLabel<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;">print</span> <span style="color: #483d8b;">&quot;You pressed the %s button!&quot;</span> <span style="color: #66cc66;">%</span> buttonLabel
&nbsp;
<span style="color: #808080; font-style: italic;"># Run the program</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;">PySimpleApp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame = DemoFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Show</span><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>In this case, we create a two-parameter anonymous function with the lambda statement. The first parameter is <em>evt</em> and the second is the button&#8217;s label. These are passed on to the <em>onButton </em>event handler, which is called when the user clicks on one of the two buttons. This example also prints the button&#8217;s label to stdout.</p>
<h2>Wrapping Up</h2>
<p>The lambda statement is used in all kinds of other projects as well. If you Google a Python project name and lambda, you can find lots of live code out there. For example, if you search for &#8220;django lambda&#8221;, you&#8217;ll find out that django has a modelformset factory that utilizes lambdas. The Elixir plugin for SqlAlchemy also uses lambdas. Keep your eyes open and you&#8217;ll be surprised how many times you&#8217;ll stumble across this handy little function maker.</p>
<h2>Further Reading</h2>
<ul>
<li>Dive Into Python: <a href="http://diveintopython.org/power_of_introspection/lambda_functions.html">Using lambda functions</a></li>
<li><a href="http://effbot.org/zone/tkinter-callbacks.htm">Effbot </a>Tkinter lambda examples</li>
<li>wxPython Wiki: <a href="http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks">Passing Arguments to Callbacks</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/feed/</wfw:commentRss>
		<slash:comments>9</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>
	</channel>
</rss>

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