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.
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:
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:
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.
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 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:
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.
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.
Best Regards...CGF