<?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>Intrepid Blog &#187; package</title>
	<atom:link href="http://blog.affien.com/archives/tag/package/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 23 Jan 2012 08:47:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Virtual packages in python</title>
		<link>http://blog.affien.com/archives/2007/11/10/virtual-packages-in-python/</link>
		<comments>http://blog.affien.com/archives/2007/11/10/virtual-packages-in-python/#comments</comments>
		<pubDate>Sat, 10 Nov 2007 21:11:07 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[package]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2007/11/10/virtual-packages-in-python/</guid>
		<description><![CDATA[When writing an application in python, it can be very c [...]]]></description>
			<content:encoded><![CDATA[<p>When writing an application in python, it can be very convenient to be able to import a module from the top of your module tree.  Eg. <code>import myapp.config</code> instead of (python 2.5 only) relative imports: <code>import ....config</code>. To do this one would have to make <code>myapp</code> a package.  The normal way to do this is to put your application directory (which now has to be named <code>myapp</code>) somewhere in python&#8217;s package search path.  This isn&#8217;t all to convenient.</p>
<p>The solution: manually set up your package module:</p>
<p><code>import os<br />
import sys<br />
import imp<br />
def setup_virtual_package(name, path=os.curdir):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot; Sets up a package at the given path with a given<br />
&nbsp;&nbsp;&nbsp;&nbsp;        name &quot;&quot;&quot;<br />
&nbsp;&nbsp;&nbsp;&nbsp;modulePath = os.path.abspath(path)<br />
&nbsp;&nbsp;&nbsp;&nbsp;f, fn, suffix = imp.find_module('__init__', <br />
&nbsp;&nbsp;&nbsp;&nbsp;                                &nbsp;&nbsp;&nbsp;&nbsp;[modulePath])<br />
&nbsp;&nbsp;&nbsp;&nbsp;imp.load_module(name, f, fn, suffix)<br />
&nbsp;&nbsp;&nbsp;&nbsp;sys.modules[name].__path__ = [modulePath]</code></p>
<p>Now <code>import myapp.something</code> works like a charm.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2007/11/10/virtual-packages-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Virtual package for your python application</title>
		<link>http://blog.affien.com/archives/2007/04/21/virtual-package-for-your-python-application/</link>
		<comments>http://blog.affien.com/archives/2007/04/21/virtual-package-for-your-python-application/#comments</comments>
		<pubDate>Sat, 21 Apr 2007 18:45:06 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Code sniplets]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[package]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2007/04/21/virtual-package-for-your-python-application/</guid>
		<description><![CDATA[When you've got a big python application, you'll usuall [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;ve got a big python application, you&#8217;ll usually split it up in modules.  One big annoyance I&#8217;ve had is that a module inside a directory cannot (easily) import a module higher up in the tree. Eg: drawers/gtk.py cannot import state/bla.py.</p>
<p>This is usually solved by making the application a package. This allows for <code>import myapp.drawers.gtk</code> from everywhere inside your application. To make it a package though, you need to add the parent directory in the <code>sys.path</code> list.  But unfortunately this also includes all other subdirectories of the parent directory as packages.</p>
<p>However, when the package module (eg: <code>myapp</code>) was already loaded, then the path from which <code>myapp</code> was loaded is used to find the submodules (eg: <code>myapp.drawers.gtk</code>) and <code>sys.path</code> isn&#8217;t looked at, at all. So, here is the trick:</p>
<pre>import sys
import os.path

p = os.path.dirname(__file__)
sys.path.append(os.path.abspath(p+"/.."))
__import__(os.path.basename(p))
sys.path.pop()</pre>
<p>Note that this script doesn&#8217;t work when directly executed, because the <code>__file__</code> attribute is only available when loaded as a module.</p>
<p>Save this script as <code>loader.py</code> in the root of your application. <code>import loader</code> from the main script in your app, and you&#8217;ll be able to import modules by <code>myapp.a.module</code>, where <code>myapp</code> is the root directory name of your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2007/04/21/virtual-package-for-your-python-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

