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 speciﬁcations to site.path and that is by using path conﬁguration ﬁles. 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 ﬁle. I ﬁnd it diﬃcult, at times, to locate detailed information when it is in this form. Nonetheless, path conﬁguration information is included in the help ﬁle in the Python Standard Library document under Python Runtime Services, Section 29.13 site – Site Speciﬁc Conﬁguration Hook. More detail can be found by reading the site.py source code ﬁle 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 ﬁnal 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 conﬁguration ﬁles. Place your path conﬁguration ﬁles in a directory where Python can ﬁnd them. Every time Python is started, it will check for the conﬁguration ﬁles and make the speciﬁed additions to site.path.
A Path Conﬁguration File is a simple text ﬁle with a .pth extension in the form name.pth. My own ﬁrst .pth ﬁle looks like this:
Python does not enforce any speciﬁc format for these conﬁguration ﬁles. 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 conﬁguration ﬁle(s) should be placed in directory:
If you’re on a Unix or Mac Operating System, carefully read the documentation and site.py source ﬁle mentioned earlier. Note that on my installation sys.preﬁx and sys.exec_preﬁx 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 diﬀerent IDE’s that I use frequently. In each I imported sys and examined sys.path to ensure that my path conﬁguration speciﬁcations were successful.