3

According to documentation, sys.path is initialized from PYTHONPATH when a Python interpreter session starts. However, in my case, PYTHONPATH variable is empty. When I execute this in a terminal:

echo $PYTHONPATH

it returns blank. On the other hand, when I start the Python REPL and inspect sys.path:

import sys
print (sys.path)

I get back a long list of paths. Where do those get loaded from? What am I missing?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matej
  • 932
  • 4
  • 14
  • 22

1 Answers1

6

Check the documentation again:

It says

[sys.path is initialized] from the environment variable PYTHONPATH, plus an installation-dependent default.

And furthermore,

the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter [… or] the empty string

That’s why yours isn’t empty.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Also, some added explanation [here](https://stackoverflow.com/a/18247492/1832539) on what `PYTHONPATH` is and when to use it. It is up to you to set what you want in that env var for *additional* paths you might want to use. – idjaw Feb 03 '18 at 16:30
  • Thanks. Can you elaborate on installation-dependent defaults? – Matej Feb 03 '18 at 18:42
  • @Matej Not really: I don’t know exactly where they are configured but most of them are probably hard-coded into the Python configuration during compilation/installation. When installing Python from source you can configure its “prefix” path. That value, in conjunction with relative library path locations, is used to populate `sys.path`. – Konrad Rudolph Feb 04 '18 at 14:09