Sunday, January 05, 2014

Modifying The Python Module Search Path

After having done a fair amount of programming in Perl, I decided a while ago that while learning Python, I wanted to not only learn the details of the language, but also make sure that I try and do things in a manner that is both easy to re-figure out later, and is also not considered an awful practice by the community.

Currently, while working on a project, I have been researching adding a directory to the module search path in Python.  While reading the module documentation, it was mentioned that one of the easiest things to do is to just add a path to the PYTHONPATH system variable.  That would be a fantastically easy thing to do, except I logged into a few different systems where I have python installed, and not one of them had that variable set.  (Those systems included both Linux and Mac OSx operating systems for those curious).

To be honest, I didn't want to venture down the path of determining all of the path's that should be set in that variable as it could be many.  So, I started up the python interpreter, imported the sys module and then printed the path like so:

> python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path

This printed out quite a long list of path's that python used to search for modules.  So, now I needed a way to add a directory to that list, so that I could place all of the home grown modules for my project in one place and reference it in the search path.

After thinking back to a few weeks ago, I remember a method that a colleague used in a script that he had written and after doing a little testing with some of that code, came up with the following method to do what I needed to:

#!/usr/bin/env python 
import os
import sys 
modulesDir  = "lib"
prog    = os.path.abspath(__file__)
cwd     = os.path.dirname(prog)
libDir = os.path.join(cwd,modulesDir)
sys.path.append(libDir)
For the above solution, you replace "lib' with whatever you called the directory.  This assumes that you have main directory where your primary script resides, and in there is a subdirectory holding your modules.  It nicely appends that directory to your search path.  I am not sure if its the prettiest or most acceptable way to accomplish this, but it does the job.  If someone knows of a better, more acceptable way, I would definitely be interested in listening to their explanation.




No comments:

 
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.