Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First argument of sys.path.insert in python

Some examples show

sys.path.insert(0, "path/to/whatever")

and other examples show

sys.path.insert(1, "path/to/whatever").

But I could not find any information of regarding the effect of using 0 as opposed to 1 and vice versa, for the first input argument of the insert() function.

Where can I find official documentation regarding this?

Also what is the difference between the two?

like image 357
theQuestionMan Avatar asked May 12 '16 03:05

theQuestionMan


People also ask

What is SYS path insert in Python?

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.

When should I use sys path append?

The sys. path. append() method is used to temporarily add a path and as such, that path would be valid for a session for example.

How do I add a directory to a path in Python?

Clicking on the Environment Variables button o​n the bottom right. In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable. Clicking on New and entering Python's install directory.


1 Answers

sys.path is a list, and so, of course, supports all list methods with their exact semantics.

But for sys.path specifically, element 0 is the path containing the script, and so using index 1 causes Python to search that path first and then the inserted path, versus the other way around when inserted at index 0.

like image 179
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 10:09

Ignacio Vazquez-Abrams