Wednesday, July 25, 2018

Python Default Search Path

Suppose you have a bunch of handy python utility functions. As as example consider the matrixTeX function, to write numpy matrices in LaTeX format.

I use this function all the time, would like it to be visible, no matter where I fire my python session from. To do this we need to modify python's default search path.

Here is a three-step setup process:

1. Store the Utility Functions in a Subdirectory

There are several ways to do this.

  • create a sub-directory called 'myPython' in my home directory ('/home/sachins/')
  • in '/home/sachins/myPython/' create a file myPyUtils.py
  • put all your utility functions in this file

> cat /home/sachins/myPython/myPyUtils.py

def matrixTeX():
    function definition

def util2():
    function definition

def util3():
    function definition

2. Point Python to the Subdirectory

You can do this temporarily or permanently.

To set it up temporarily, fire up a python session, and append the sys.path variable to expand the default search path. In our example, I would do the following:

> python3
>>> import sys
>>> sys.path.append('/home/sachins/myPython')

This setting is forgotten when you terminate your python session. Thus, you have to invoke this pair of commands, every time you start a session.

To avoid this, you can set things up so that they are more permanent.

Open your .bashrc file (if you are using bash) and modify the PYTHONPATH variable.

In this case, I  add the following lines:

# set python path to look for scripts
export PYTHONPATH=$PYTHONPATH:/home/sachins/myPython

Now you are all set. We can import the utility functions and use them from anywhere.

3. Import the Utilities Functions

Fire up a python session. Once you have expanded the search path (temporarily or permanently), you can import the utility file (myPyUtils.py) as a module, and access its individual functions. As an example,

>>> import numpy as np
>>> from myPyUtils import *
>>> print(matrixTeX(np.array([1,2])))
\begin{bmatrix}
  1 & 2\\
\end{bmatrix}

Notes:
  • the first time you import the file, it will create a ".pyc" file in the subdirectory
  • you can be more careful with the namespace during importing

No comments:

Post a Comment