Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the first element in python's sys.path an empty string?

Tags:

python

I noticed, when I launch 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.

like image 224
eigenfield Avatar asked Mar 29 '18 14:03

eigenfield


People also ask

What does Sys path contain?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.

How does Python initialize SYS path?

The directory of the script which python is executing is added to sys. path . On Windows, this is always the empty string, which tells python to use the full path where the script is located instead. The contents of PYTHONPATH environment variable, if set, is added to sys.

What is SYS path append in Python?

Python sys path append() The sys. path. append() is a built-in function of the sys module in Python that can be used with path variables to add a specific path for an interpreter to search.


2 Answers

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.

like image 150
Ignacio Vazquez-Abrams Avatar answered Oct 20 '22 01:10

Ignacio Vazquez-Abrams


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

like image 32
moghya Avatar answered Oct 20 '22 01:10

moghya