Python Paths

1 minute read

Published:

Often when working in science/research we are rapidly developing and modifying code (and maybe not following the very best practices for coding). In these instances we may not be writing, developing, or installing Python modules which can be accessed by the Python interpreter. Rather we may have a directory with set of scripts, functions, or classes. In Python in order to access these without installing a module we need to add the file path of our directory to the Python path. This can be easily acomplished using the sys module.

file_path = 'PATH\TO\CODE\'
sys.path.append(os.path.dirname(file_path))

The above will add the file_path variable to the Python path allowing you to access the code within that directory. This works well when working in IPython and Jupyter Notebooks.

Note, your directory of code file_path should still be structured like a Python package so you can easily access the functionality of the code using import (see below for more info).

Alternatively, you can add the file path to a conda development path file, conda.pth. This can be done using the conda-develop command or by creating a conda.pth file in the Python path and adding the directory of interest to the file. Note, it is not recommended that this is done routinely as it can cause problems. To mitigate potential problems this should be done in an environment seperate from the main Python or base environment.

This is similar to adding file paths or directories to the IDL path and may be simpler (and more familiar) to those more experienced in IDL.

You can read more about packages and modules here: