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
Python: Best way to add to sys.path relative to the current running script - Stack Overflow
oh this beats setting the PYTHONPATH environment or the sys.path answers. I have learnt something today - for which I am very, very grateful! 2021-12-06T14:50:00.01Z+00:00 ... If you don't want to change the script content in any ways, prepend the current working directory . More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
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.

Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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__ ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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
๐ŸŒ
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....
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Programming 53 - Sys.path and Changing Module Paths
To learn more, please visit the YouTube Help Center: https://www.youtube.com/help