Python 3.6 site.path Configuration

Curtis G. Flippin
Flippin Engineering
Flippin-Eng@lighthousepubl.com

May 27, 2018

Abstract

If, like me, you are new to Python 3.6 you may have wanted to add the path to one or more development directories to Python’s site.path but not known how to go about it.

Many developers have asked this question on the Web and many people have advice about how to do it. One common suggestion is to write a module that inserts the directories in the site.path list. The problem is that these changes are lost every time you restart the python shell.

I did learn the proper way to add additional path specifications to site.path and that is by using path configuration files. It is easy to do and this Tech Note describes how to make path changes to Python 3.6 that do not get lost.

Introduction

I used the Python Windows OS installer, python-3.6.5-amd64.exe, to install Python 3.6.5 on my Windows 7 operating system. This 64 bit version of Python installed to my 64-bit programs folder at:

C:\Users\Curtis\Program Files\Python36.

The documentation for Python 3.6 is in a single huge help file. I find it difficult, at times, to locate detailed information when it is in this form. Nonetheless, path configuration information is included in the help file in the Python Standard Library document under Python Runtime Services, Section 29.13 site – Site Specific Configuration Hook. More detail can be found by reading the site.py source code file which is located at:

\Python36\Lib\site.py

If the documentation seems disjointed and confusing to you, I agree. This often happens with so called self documenting code using Docstrings. I actually found the comments within site.py to be more helpful. And then, there is the code to study, as well.

In the final analysis, it comes down to this. Create lists of the folders i.e. directories that you want to include in site.path and put them into path configuration files. Place your path configuration files in a directory where Python can find them. Every time Python is started, it will check for the configuration files and make the specified additions to site.path.

Path Configuration Files

A Path Configuration File is a simple text file with a .pth extension in the form name.pth. My own first .pth file looks like this:

# Python 3.6  
# File: Dev.pth  
# Last Updated: 25 May 2018  
#  
# Place the following folders in sys.path so that Python can  
# find uninstalled modules and packages under development.  
# This path configuration file must be placed in folder:  
# C:\Program Files\Python36\Lib\site-packages  
# Note that forward slashes must be used vs back slashes.  
# See site.py for all the gory details  
#  
C:/Users/Curtis/Py36Work/ForthWork  
C:/Users/Curtis/Py36Work  
# Dev.pth ends here

Python does not enforce any specific format for these configuration files. Rules are simple.

As far as I’m able to determine, if you have Python 3.6 installed in a Windows Operating System, the path configuration file(s) should be placed in directory:

...\Python36\Lib\site-packages

If you’re on a Unix or Mac Operating System, carefully read the documentation and site.py source file mentioned earlier. Note that on my installation sys.prefix and sys.exec_prefix both point to the Python36 directory which corresponds to /user/local as mentioned in the documentation.

Test Results

Testing simply consisted of starting the Python shell in several different IDE’s that I use frequently. In each I imported sys and examined sys.path to ensure that my path configuration specifications were successful.

1.
Python IDLE 3.6.5 – I first tried the Path Browser item in the File menu and confirmed that my path additions were there. They were. I also imported sys and checked sys.path. I then imported one of my own modules from a Dev path and that worked perfectly.
2.
EMACS 25.3 – Ran EMACS and opened a python mode buffer. Opened a Python shell buffer, imported sys and checked sys.path. My Dev paths were both there.
3.
JEdit 5.5.0 – Ran JEdit and opened a python shell. Again, I checked sys.path and it also was good.
4.
MS Visual Studio 2017 15.7.2 – Opened a Python source file and started the Python shell. One more time, sys.path was good. I did a ’from’ import of one of my development modules and it worked perfectly.

Best Regards...CGF

BACK