Clone wiki

bnpy-dev / Configuration

Configuration of Environment Variables

After you have installed a copy of bnpy on your system, you need to adjust a few key settings to make sure that you are ready to use bnpy. Here, we introduce the concept of environment variables, and discuss how bnpy uses them.

  • PYTHONPATH : Tell Python where to find the bnpy module, so you can execute import bnpy.
  • BNPYOUTDIR : Tell bnpy where to save results
  • BNPYDATADIR (optional) : Tell bnpy where to find your custom datasets.
  • BNPYROOT (optional) : Tell other code (Python or otherwise) where bnpy code lives on file system.

What is an Environment Variable?

Environment variables are variables that you can set in your terminal. They have names like PYTHONPATH or BNPYDATADIR, which are by convention all capitalized. They hold string values.

They allow you (the user) to define key preferences specific to your local system (where to read data, where to save results, etc.), without these needing to be hard-coded into the bnpy module or passed as an argument every time bnpy runs. After a variable is defined, any program executed in your terminal can read the value of any (global) environment variable.

For more info, see this tutorial-style introduction: (

Basic practice

Here, in a terminal, we set a variable to 42 and print the results.

$ MYVAR=42
$ echo $MYVAR

In most UNIX systems, the keyword export makes a variable global, so that other processes (like python) can read that variable's value. Below, we define a global environment variable and then show how to access it from python.

$ export MYVAR=42
$ python -c "import os; print os.environ['MYVAR']"

Here, the -c flag means execute the following statement as Python code.

Step 1: Tell Python where to find the bnpy module

Python always looks at the environment variable called PYTHONPATH to find custom-installed modules.

This is how to append the bnpy code to the PYTHONPATH, so that you can execute import bnpy from any Python script you write anywhere on your system.

export PYTHONPATH=${PYTHONPATH}:/path/to/bnpy-dev/;

Step 2: Tell bnpy where to save results

bnpy looks at BNPYOUTDIR to define the complete path of the directory where results are saved.

export BNPYOUTDIR=/path/to/my/results/

Make sure this directory is readable and writeable by you. Also make sure it has enough free disk space (a few GBs will do just fine) if you plan to do extensive experimentation.

(optional) Step 3: Tell bnpy where to load custom datasets from

By default, bnpy will already know how to find several toy and real datasets. However, to run bnpy on custom, user-defined data, you will need to specify a location.

bnpy can process any dataset defined in a dataset script. The location of these scripts are specified by the Unix environment variable BNPYDATADIR.

export BNPYDATADIR=/path/to/my/custom/dataset/

In general, you might change this location every time you work with a different custom dataset.

Environment Variables and IDEs

If you choose to develop and run your code in IDE, often environment variables need to be set more permanently than just in the terminal.

PyCharm 3.4 on Mac OS X

In the menu bar of PyCharm, select Run -> Edit Configurations.... Then in Environment -> Environment Variables, manually add the environment variables mentioned above as key value pairs (e.g. PYTHONPATH /path/to/bnpy-dev/). When done, press OK.

A more general (but dangerous) way to do this is that you can edit the file /etc/launchd.conf to include the line:

setenv PYTHONPATH /path/to/bnpy

After restarting the machine, this new configuration will be maintained no matter how many times you restart in the future. We've found this to be a successful way to keep autocompletion working in PyCharm.