I'm quite a beginner in Python and I found the answer of Anand was very good but quite complicated to me, so I try to reformulate :

1) insert and append methods are not specific to sys.path and as in other languages they add an item into a list or array and :
* append(item) add item to the end of the list,
* insert(n, item) inserts the item at the nth position in the list (0 at the beginning, 1 after the first element, etc ...).

2) As Anand said, python search the import files in each directory of the path in the order of the path, so :
* If you have no file name collisions, the order of the path has no impact,
* If you look after a function already defined in the path and you use append to add your path, you will not get your function but the predefined one.

But I think that it is better to use append and not insert to not overload the standard behaviour of Python, and use non-ambiguous names for your files and methods.

Answer from herve-guerin on Stack Overflow
Top answer
1 of 2
96

I'm quite a beginner in Python and I found the answer of Anand was very good but quite complicated to me, so I try to reformulate :

1) insert and append methods are not specific to sys.path and as in other languages they add an item into a list or array and :
* append(item) add item to the end of the list,
* insert(n, item) inserts the item at the nth position in the list (0 at the beginning, 1 after the first element, etc ...).

2) As Anand said, python search the import files in each directory of the path in the order of the path, so :
* If you have no file name collisions, the order of the path has no impact,
* If you look after a function already defined in the path and you use append to add your path, you will not get your function but the predefined one.

But I think that it is better to use append and not insert to not overload the standard behaviour of Python, and use non-ambiguous names for your files and methods.

2 of 2
65

Because Python checks in the directories in sequential order, starting at the first directory in the sys.path list, until it finds the .py file it was looking for.

Normally, the current directory or the directory of the script is the first element in the list, unless you modify it (like you did). From documentation:

As initialized upon program startup, 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. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

So, most probably, you had a .py file with the same name as the module you were trying to import from, in the current directory (where the script was being run from).

Also, something to note about ImportErrors - Let's say the import error says: ImportError: No module named main. This doesn't mean main.py is overwritten - if it was overwritten it would still exist and we would not have issues trying to read it. It's some module above this that got overwritten with a .py or some other file.

For example, consider a directory structure that looks like this:

 - test
    - shared
       - __init__.py
       - phtest.py
    - testmain.py

Now from testmain.py, I call from shared import phtest, which works fine.

Now let's say I introduce a shared.py in test directory:

 - test
    - shared
       - __init__.py
       - phtest.py
    - testmain.py 
    - shared.py

Now when I try to do from shared import phtest from testmain.py, I get the error:

ImportError: cannot import name 'phtest'

As you can see above, the file that introduced the issue is shared.py, not phtest.py.

Discussions

Why does sys.path.insert change the working directory and how to make it stop?
It doesn't sys.path is a list of path elements. The insert method on a list does not change the current working directory. Please post the code that made you believe it was the case. More on reddit.com
🌐 r/learnpython
5
2
May 31, 2018
python - Why use sys.path.append(path) instead of sys.path.insert(1, path)? - Stack Overflow
Edit: based on a Ulf Rompe's comment, it is important you use "1" instead of "0", otherwise you will break sys.path. I have been doing python for quite a while now (over a year), and I am always More on stackoverflow.com
🌐 stackoverflow.com
Python files included by `sys.path.insert()` are missing on the final executable
I have this lines on my code: import sys sys.path.insert(0,'../python_debug') import debug_tools So, after compiling the executable .exe the program closed with missing module error: Traceb... More on github.com
🌐 github.com
June 6, 2017
First argument of sys.path.insert in python - Stack Overflow
But I could not find any information ... input argument of the insert() function. Where can I find official documentation regarding this? ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
pythontutorials
pythontutorials.net › blog › first-argument-of-sys-path-insert-in-python
Python sys.path.insert: What’s the Difference Between 0 and 1 as the First Argument?
Since we inserted ./my_modules at index 1, Python checks the current directory first, importing its math.py instead of the one in ./my_modules. To summarize, the choice between 0 and 1 hinges on search order:
🌐
Reddit
reddit.com › r/learnpython › why does sys.path.insert change the working directory and how to make it stop?
r/learnpython on Reddit: Why does sys.path.insert change the working directory and how to make it stop?
May 31, 2018 -

I do sys.path.insert(0, '/other/dir') in order to import modules from directories which are not in subdirectories of the current project/far away but it is changing the working directory to that directory.

I never noticed that behavior tile recently.

I prefer to do insert rather than installing modules for system wide imports because I tried that once and since I often am changing the code in those modules I found it a nuisance to constantly be having to re-install the module every time I made a change. And that is for only one. It would really get annoying if there were several files which would need reinstalling every time the slightest change was made.

I am currently having to change the working directory back to the origininal one again with os but why does it do it in the first place and how to prevent?

I am often importing modules from far away and unrelated(to the original script) places so want to know how to do this with least fuss- the advised make them installable suggestion was way more trouble though due to aforementioned reason.

🌐
CSDN
devpress.csdn.net › python › 62fd180d7e6682346619159c.html
Why use sys.path.append(path) instead of sys.path.insert(1, path)?_python_Mangs-Python
August 18, 2022 - Edit: based on a Ulf Rompe's comment, it is important you use "1" instead of "0", otherwise you will break sys.path. I have been doing python for quite a while now (over a year), and I am always confused as to why people recommend you use sys.path.append() instead of sys.path.insert().
Top answer
1 of 3
171

How python finds its modules

Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).

By default, Python looks for its modules and packages in $PYTHONPATH.

To find out what is included in $PYTHONPATH, run the following code in python (3):

import sys
print(sys.path)

How to add a directory

Occasionally

From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:

import sys
sys.path.insert(0, "/path/to/your/package_or_module")

For example:

if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:

import sys
sys.path.insert(0, "/home/myname/pythonfiles")

And I can simply import the file module_1.py by:

import module_1

When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py

For example:

To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `

from <packagename> import module_2

Adding directories to $PYTHONPATH permanently:

Add the following line to your ~/.profile file.

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add

Subdirectories

From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:

import packagename.subfolder.module_3

Given the fact that all subfolders in the package include their own __init__.py file.

When a module is in the same directory as the script or application

There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.

Example:

If I have a folder, containing script.py and module.py, I can simply import the module by:

import module
2 of 3
7

The correct way to use relative import is:

from ..Common import foo1

And you also need a __init__.py in all your folders.

🌐
YouTube
youtube.com › watch
PYTHON : Effect of using sys.path.insert(0, path) and sys.path(append) when loading modules - YouTube
PYTHON : Effect of using sys.path.insert(0, path) and sys.path(append) when loading modules [ Gift : Animated Search Engine : https://www.hows.tech/p/recomme
Published: December 8, 2021
🌐
GitHub
gist.github.com › justinmklam › c453ea62b47f4e302dbd83c05882e3bd
Techniques for python path manipulation and relative imports in (i.e. when not dealing with a package). · GitHub
python-paths-and-imports.md · ....path.join(current_dir, "..")) For simple project structures: import sys, os sys.path.insert(1, os.path.join(sys.path[0], '..')) When absolute paths are required: import os,sys,inspect currentdir ...
Find elsewhere
Top answer
1 of 3
55

If you really need to use sys.path.insert, consider leaving sys.path[0] as it is:

sys.path.insert(1, path_to_dev_pyworkbooks)

This could be important since 3rd party code may rely on sys.path documentation conformance:

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter.

2 of 3
51

If you have multiple versions of a package / module, you need to be using virtualenv (emphasis mine):

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

That's why people consider insert(0, to be wrong -- it's an incomplete, stopgap solution to the problem of managing multiple environments.

🌐
GitHub
github.com › pyinstaller › pyinstaller › issues › 2640
Python files included by `sys.path.insert()` are missing on the final executable · Issue #2640 · pyinstaller/pyinstaller
June 6, 2017 - I have this lines on my code: import sys sys.path.insert(0,'../python_debug') import debug_tools So, after compiling the executable .exe the program closed with missing module error: Traceb...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sys-path-in-python
sys.path in Python - GeeksforGeeks
August 21, 2024 - Sys is a built-in Python module that contains parameters specific to the system i.e. it contains variables and methods that interact with the interpreter and are also governed by it. sys.path is a built-in variable within the sys module.
🌐
Python Forum
python-forum.io › thread-33862.html
sys.path.insert()
i inserted a path by doing sys.path.insert(0,mypath). how do i delete it?
🌐
codestudy
codestudy.net › blog › python-sys-path-modification-not-working
Python sys.path Modification Not Working: Why Inserting Paths Fails But Replacing sys.path Works (Python 2.5.4) — codestudy.net
By using absolute paths, prioritizing insertion at index=0, and verifying sys.path after modification, you can reliably add custom directories to Python’s module search path—even in legacy environments.
🌐
YouTube
youtube.com › watch
PYTHON : Why use sys.path.append(path) instead of sys.path.insert(1, path)? - YouTube
PYTHON : Why use sys.path.append(path) instead of sys.path.insert(1, path)? [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYT
Published: December 6, 2021
🌐
Note.nkmk.me
note.nkmk.me › home › python
Check and Add the Module Search Path in Python: sys.path | note.nkmk.me
May 12, 2023 - Be aware that if there is a file with the same name as a standard library in the same directory as the executed script file, Python will import that file instead. Since sys.path is essentially a list, you can easily add new paths. In this example, the append() method is used, but you can also use the insert() method or other list methods.
🌐
DNMTechs
dnmtechs.com › exploring-the-impact-of-sys-path-insert0-path-and-sys-path-appendpath-on-module-loading-in-python-3
Exploring the Impact of sys.path.insert(0, path) and sys.path.append(path) on Module Loading in Python 3 – DNMTechs – Sharing and Storing Technology Knowledge
Python, being a versatile and powerful programming language, offers various ways to manage module loading and importing. Two commonly used methods are sys.path.insert(0, path) and sys.path.append(path). These methods allow developers to specify additional directories where Python should search ...
🌐
Codemia
codemia.io › home › knowledge hub › adding directory to sys.path /pythonpath
adding directory to sys.path /PYTHONPATH | Codemia
September 24, 2025 - python · 1import sys 2from pathlib import Path 3 4extra_dir = Path("/opt/myproject/lib") 5sys.path.insert(0, str(extra_dir)) 6 7import mymodule 8print(mymodule) Using insert(0, ...) places the directory at the front, so Python looks there first.
🌐
How to Use Linux
howtouselinux.com › home › understanding sys.path in python
understanding sys.path in Python - howtouselinux
October 9, 2025 - Second, sys.path is initialized when Python starts, so if you want to add or remove paths from sys.path , you’ll need to do it before importing any modules.
🌐
Solved
code.i-harness.com › en › q › 9a09bd
python - working - sys.path.insert 0 vs 1 - Solved
December 4, 2018 - That's why people consider insert(0, to be wrong -- it's an incomplete, stopgap solution to the problem of managing multiple environments. Edit: based on a Ulf Rompe's comment, it is important you use "1" instead of "0", otherwise you will break ...