This is not recommended, as it hard-codes a path and makes it difficult to run the script elsewhere, but you can do

>>> import sys
>>> sys.path.insert(0,'/home/anand/')
>>> print(sys.path)
['/home/anand/', '', '/usr/local/lib/python2.7/dist-packages/_pdbpp_path_hack', '/usr/local/lib/python2.7/dist-packages/goose-0.0.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/jieba-0.33-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/cssselect-0.9.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/nanoservice-0.1.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/nanomsg-1.0a2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/msgpack_python-0.4.2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/DecisionTree-2.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/nudepy-0.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/wsgilog-0.3-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/distribute-0.7.3-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/munkres-1.0.7-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/parsedatetime-1.4-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/argparse-1.3.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/tusker-0.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.0.3-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/numpy-1.9.2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/turkic-0.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/scikits.bootstrap-0.3.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pyvision-0.1-py2.7-linux-x86_64.egg', '/home/anand/playspace/languages/python_pkgs/ets', '/usr/local/lib/python2.7/dist-packages/Scrapy-1.1.0dev1-py2.7.egg', '/usr/lib/python2.7/dist-packages', '/home/anand/playspace', '/home/anand/workspace/pyvision/src', '/home/anand/playspace/yapf', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/Orange/orng', '/usr/local/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
>>>

After this your imports will look into the prepended path before looking anywhere else.

Answer from Software Mechanic on Stack Overflow
Top answer
1 of 8
20

This is not recommended, as it hard-codes a path and makes it difficult to run the script elsewhere, but you can do

>>> import sys
>>> sys.path.insert(0,'/home/anand/')
>>> print(sys.path)
['/home/anand/', '', '/usr/local/lib/python2.7/dist-packages/_pdbpp_path_hack', '/usr/local/lib/python2.7/dist-packages/goose-0.0.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/jieba-0.33-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/cssselect-0.9.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/nanoservice-0.1.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/nanomsg-1.0a2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/msgpack_python-0.4.2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/DecisionTree-2.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/nudepy-0.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/wsgilog-0.3-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/distribute-0.7.3-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/munkres-1.0.7-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/parsedatetime-1.4-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/argparse-1.3.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/tusker-0.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.0.3-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/numpy-1.9.2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/turkic-0.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/scikits.bootstrap-0.3.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pyvision-0.1-py2.7-linux-x86_64.egg', '/home/anand/playspace/languages/python_pkgs/ets', '/usr/local/lib/python2.7/dist-packages/Scrapy-1.1.0dev1-py2.7.egg', '/usr/lib/python2.7/dist-packages', '/home/anand/playspace', '/home/anand/workspace/pyvision/src', '/home/anand/playspace/yapf', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/Orange/orng', '/usr/local/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
>>>

After this your imports will look into the prepended path before looking anywhere else.

2 of 8
7

You shouldn't need to mess with pip's path, python actually handles it's pathing automatically in my experience. It appears you have two pythons installed. If you type:

which pip
which python

what paths do you see? If they're not in the same /bin folder, then that's your problem. I'm guessing that the python you're running (probably the original system one), doesn't have it's own pip installed. You probably just need make sure the path for the python you want to run should come before /usr/bin in your .bashrc or .zshrc

If this is correct, then you should see that:

which easy_install

shares the same path as the python installation you're using, maybe under /usr/local/bin. Then just run:

easy_install pip

And start installing the right packages for the python that you're using.

🌐
Google Groups
groups.google.com › g › python-virtualenv › c › yyUIjOhOubw
How to prepend a path to sys.path in Python?
July 14, 2015 - import sys; sys.__plen = len(sys.path) /usr/lib/python2.7/dist-packages/test_dir import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new) This file is read by site.py when Python is starting. Since its file name comes after easy-install.pth alphanumerically, it is consumed by site.py afterwards. Taken together, the first and last lines of the file prepend the path to sys.path (these lines were taken from easy-install.pth).
Discussions

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 - Prepend sys.path from shell? - Stack Overflow
If You want to use given python binary You can prepend PATH. If You want to use given libpython You can prepend LD_LIBRARY_PATH Now suppose You want to use given package directory. I tried PYTHON... More on stackoverflow.com
🌐 stackoverflow.com
November 13, 2013
what does sys.path.append(".") do?
sys.pathis a list of directories where the Python interpreter will search for modules/files when import is invoked. sys.path.append(".") tells Python to append "this current directory" (that's the dot) to the list of places it should look for files. More on reddit.com
🌐 r/learnpython
4
4
May 1, 2020
Django fixup appends to PYTHONPATH instead of prepending
This should be fine in principle as long as the PYTHONPATH is set so that the local modules have precedence over the system ones. When running Celery CLI, the system wide module has precedence over the local ones apparently. I traced this issue back to this sys.path tweak, which is actually appending local path instead of prepending ... More on github.com
🌐 github.com
6
February 18, 2019
🌐
YouTube
youtube.com › hey delphi
PYTHON : How to prepend a path to sys.path in Python? - YouTube
PYTHON : How to prepend a path to sys.path in Python?To Access My Live Chat Page, On Google, Search for "hows tech developer connect"I promised to reveal a s...
Published: April 16, 2023
Views: 5
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.

🌐
Linux Hint
linuxhint.com › sys-path-append-python
How to Use Sys.path.append() in Python – Linux Hint
In all cases, we cannot keep every single Python file we create in one directory, we may wish to use multiple directories to adequately classify our files. The problem however is that we may need to import our created modules left and right. In such cases, if we want to prevent a ModuleNotFound Error, then we can add the location of the directory – and mind you, it has to be a directory, not a file – to sys.path using the sys.path.append() method.
🌐
pytest
docs.pytest.org › en › 7.1.x › explanation › pythonpath.html
pytest import mechanisms and sys.path/PYTHONPATH — pytest documentation
Some aspects of the import process can be controlled through the --import-mode command-line flag, which can assume these values: prepend (default): the directory path containing each module will be inserted into the beginning of sys.path if not already there, and then imported with the __import__ ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › what does sys.path.append(".") do?
r/learnpython on Reddit: what does sys.path.append(".") do?
May 1, 2020 -

I have a structure like this:

Parent_Folder
-- foo.py
    Child_Folder_1
    -- bar.py
    -- egg.py
    Child_Folder_2
    -- spam.py

I tried relative importing on bar.py,

from ..import foo

but it did not work for reasons, and I somehow found a way to do this via internet.

import sys
sys.path.append(".")

now I can import foo.py and egg.py and even spam.py on bar.py, but I wonder how this is working, and what did that single line do. I've searched the web, but I doubt I found any understandable answers, so I'm posting this question here.

Since I'm not a professional at neither python nor programming, a kind explaination would be welcome :D Anyway, thanks for your time for reading this.

🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
To not prepend this potentially unsafe path, use the -P command line option or the PYTHONSAFEPATH environment variable. A program is free to modify this list for its own purposes. Only strings should be added to sys.path; all other data types are ignored during import.
🌐
GitHub
github.com › celery › celery › issues › 5347
Django fixup appends to PYTHONPATH instead of prepending · Issue #5347 · celery/celery
February 18, 2019 - This should be fine in principle as long as the PYTHONPATH is set so that the local modules have precedence over the system ones. When running Celery CLI, the system wide module has precedence over the local ones apparently. I traced this issue back to this sys.path tweak, which is actually appending local path instead of prepending ...
Author: celery
🌐
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.
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.

🌐
Johnny-lin
johnny-lin.com › cdat_tips › tips_pylang › path.html
Appending to Your Python Path - Johnny Lin
Your path (i.e. the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module.
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.

🌐
pytest
docs.pytest.org › en › stable › explanation › pythonpath.html
pytest import mechanisms and sys.path/PYTHONPATH - pytest documentation
Importing files in Python is a non-trivial process, so aspects of the import process can be controlled through the --import-mode command-line flag, which can assume these values: prepend (default): The directory path containing each module will be inserted into the beginning of sys.path if not ...
🌐
Safjan
safjan.com › home › note › adding parent directory to python path
Adding Parent Directory to Python Path - Krystian Safjan's Blog
May 28, 2024 - To add a parent directory to the Python path, you can use the following code: ... This code imports the sys module and then uses the insert() method to add the parent directory (indicated by '..') to the beginning of the sys.path list.
🌐
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 - 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. Add an item to a list in Python (append, extend, insert)
🌐
KK's Blog
fromkk.com › posts › sys-dot-path-in-python
Sys.path in Python - KK's Blog (fromkk)
June 4, 2026 - Here is the process how sys.path is set in Python, with some parts omitted. Python Command Line Arguments By default, as initialized upon program startup, a potentially unsafe path is prepended to sys.path:
🌐
Real Python
realpython.com › add-python-to-path
How to Add Python to PATH – Real Python
July 23, 2026 - Use the following line, replacing ...ON>:$PATH" to the end of .profile. The command export PATH="<PATH_TO_PYTHON>:$PATH" prepends <PATH_TO_PYTHON> to the PATH environment variable....