title: django: taking DRY & code reuse to the next level?
body: <p>In the django culture, I have encountered the concept of app reuse but not snippet reuse. Here is an example of what I mean by snippet reuse: I have a function getDateTimeObjFromString( sDateTime ), obviously you pass a string date time and it returns a python date-time object.</p>
  
  <p>Back in the late 1980's or early 1990's, I was exposed to the idea of snippet reuse at a FoxPro developers conference.  If you write code for a specific problem and find it is useful elsewhere in your project, move it to a project library.  If you find that code is useful for other projects, move it to a generic library that can be accessed by all projects.</p>
  
  <p>(At the FoxPro DevCon, they did not call it snippet reuse.  I coined that term to make clear that I am referring to reuse of chunks of code smaller than an entire app.  The FoxPro DevCon was long ago, I do not remember exactly what they called it.)</p>
  
  <p>I read the most recent "Two Scoops of Django", and it does mention reusing snippets within a single project but I did not find any mention of the concept of snippet reuse across multiple projects.</p>
  
  <p>I wrote and used getDateTimeObjFromString() long before I tackled my django app.  It is in packages I keep under /home/Common/pyPacks.  On my computers, I set PYTHONPATH=/home/Common/pyPacks, so every project can access the code there.  The code for getDateTimeObjFromString() is under a Time subdirectory in a file named Convert.py.  So to use the code in any project:</p>
  
  <pre><code>from Time.Convert import getDateTimeObjFromString
  </code></pre>
  
  <p>My django app downloads data from an API, and that data includes timestamps.  It would be nice if the API sent python date time objects, but what you get are strings.  Hence the utility of getDateTimeObjFromString().</p>
  
  <p>This is just one example, there are many little functions under /home/Common/pyPacks that I found convenient to access in my django project.</p>
  
  <p>Yes /home/Common/pyPacks are under version control in github and yes I deploy on any particular machine via git pull.</p>
  
  <p>When working on my django project from a development computer, PYTHONPATH works, and I can import the packages.  But then I tried running my django app on a server via wsgi.py -- PYTHONPATH is disabled.  I can set PYTHONPATH both at the OS and Apache2 level, but python ignores it, the functions cannot import.</p>
  
  <p>I do not want to bother with making my personal generic library an official python package under PyPI.  </p>
  
  <p>Does the django community expect me to copy and paste?</p>
  
  <p>I arrived at a work around: make /home/Common/pyPacks a psudeo site-package by putting a "pyPks" symlink in the virtual environment's site-packages directory to /home/Common/pyPacks, adding "pyPks" to the INSTALLED_APPS, then changing all the import statements as follows:</p>
  
  <pre><code>original: 
      from Time.Convert import getDateTimeObjFromString
  work around update:
      from pyPks.Time.Convert import getDateTimeObjFromString
  </code></pre>
  
  <p>I also had to update all my generic library files to handle both absolute imports via PYTHONPATH and relative imports.</p>
  
  <p><a href="https://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py">How to fix “Attempted relative import in non-package” even with <strong>init</strong>.py</a></p>
  
  <p>Is there a better way to reuse snippets in a django project?</p>