26

I noticed, when I launch the Python REPL and do:

import sys
print(sys.path)

The first element of the list is an empty string. This only happens in the REPL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
daparic
  • 3,794
  • 2
  • 36
  • 38

3 Answers3

18

sys.path[0] is an entry created by the Python executable to refer to the directory of the script being run. If no script is being run, e.g. the REPL has been invoked directly, an empty entry representing the current directory is added.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
9

The first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter.

If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

As per documentation here

vvvvv
  • 25,404
  • 19
  • 49
  • 81
moghya
  • 854
  • 9
  • 18
5

From the documentation:

If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string

So, when you're using Python through the command line, there isn't any script being used, so the first element is represented as an empty string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43