Modifying Python’s search path with .pth files

Arnaud Bertrand
2 min readOct 1, 2015

--

I’ve just spent the last few hours getting a major headache with this before eventually finding a solution so I wanted to share it with you. The official Python documentation on this is really unclear.

If you want to make sure that on startup python has all the directories you want in its search path automatically, do as follows:

  1. Open your terminal and launch python
  2. Import sys and type “sys.path”. This will show you which directories your python automatically looks into at startup:

3. In that list, you will see a path ending in “site-packages”: that’s where you want to save your .pth file.

4. Open your favourite text editor and save a file with any name but with the .pth type in the “site-packages” folder. Attention: some text editors will save that file as PHP or god knows what, you need to make sure that not only the extension but the type is .pth

5. In your .pth file list all the directory paths that you want python to consider on startup, one after the other:

6. You’re done. Next time you check for sys.path in your terminal, you’ll see the directories you’ve listed in your .pth file are now included.

--

--