Data Analysis

Set the system path for Python Jupyter notebooks

In Jupyter, when I was a newbie, I often needed to reference some Python library code located in some weird place on my PC, so I did this at the top of each Jupyter notebook I created:

import sys
sys.path.append(‘C:\users\name\code\my-Python-object-location’)

Doing so made the path (temporarily) part of sys.path for as long as that session was active. But when I started a new notebook, I always had to include sys.path.append() again at the top of each new notebook. Drove me nuts.

Here’s the fix:

Add your Python object path(s) to “PYTHONPATH” or an exiting “path”¬† entry in your system environment variables (via the Windows Control Panel).

How to do it:

On your system (for Windows 10, enter the following in the “Type here to search” box, screen bottom left), search for “control panel” then in the upper right of the panel, search for “environment” and click on “Set your environment variables”

Next, in the Environment Variables section (see image below), check if you already have PYTHONPATH. If yes, select it and click “Edit” and add additional paths as needed. If it’s not there, click “New” and add PYTHONPATH (if you have an existing¬†‘path’ variable, simply edit it. But I like to add PYTHONPATH to keep it logically separate from the generalized Windows system ‘path’ variable).

Paths in environment variables such as PYTHONPATH need to be separated with a semicolon, “;” … like this: ‘C:\users\name\code\my-library111′;’C:\users\name\code\my-library222′;’C:\users\name\code\my-library333’

So, click ‘Save’ farther down at the bottom of the Environment Variables box and you are done.

Remove the sys.path.append() code from your notebooks and restart them and you should be good to go. (Just to be safe, adjust one notebook first and check it out to make sure this system path fix is working for you!)

Good luck. The game is afoot!

 

add python path to system Peter Bakke