This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:

import sys
sys.path.insert(0,'/path/to/mod_directory')

That said, there are usually better ways to manage imports than either using PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.

Answer from Ned Deily on Stack Overflow
Discussions

How do I add a directory to PATH? I am having issues running a python script by just its name.
so when you type >echo $PATH it shows you that output? Great, you are actually halfway there. At the prompt do the following >export PATH=$PATH: Now then, that only made it happen for that instance you were in... You want it permanent? Assuming that you are using bash and it sounds like you are then yes, add it to your .bashrc file It appears you are also adding a definition for SCRIPTS_DIR, that would need to be defined in the .bashrc as well. It would be much simpler to simply put PATH=$PATH:/home/user/Programming/Scripts More on reddit.com
๐ŸŒ r/linux4noobs
8
13
September 4, 2019
How to add my project folder to the PYTHONPATH?
Search environment variable in the windows search bar New dialogue box appears Then click on environment variable Then u can find in users tab PATH Double click amd open it Then add the path of u r file Then save and then apply That's it โ˜บ๏ธ More on reddit.com
๐ŸŒ r/learnpython
17
1
August 17, 2021
how to add a directory to sys.path?
In any folder that is listed in sys.path, you can make a plain text file with ".pth" extension to add names of other folders you want to include in sys.path. Normally, I create a file named "mymodules.pth" inside the "site-packages" folder. But it is a matter of preference. Additionally, there is a system variable named "PYTHONPATH". Any paths you add to this variable will automatically be included in sys.path. More on reddit.com
๐ŸŒ r/learnpython
3
0
September 10, 2023
How to add python to path and how to use it in commandline/powershell?
Images are banned so you post code blocks instead of screenshots/picture of your code, I'm assuming. It's much easier to diagnose and troubleshoot from a code block than a picture. More on reddit.com
๐ŸŒ r/learnpython
17
2
October 25, 2025
๐ŸŒ
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. Since path is a list, you can use the append method to add new directories to the path.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ sysadmin โ€บ how to add python to path on windows, linux, and mac
How to Add Python to PATH on Windows, Linux, and Mac
December 12, 2025 - When a user types a terminal command without specifying a command path, the system searches the PATH directories for the corresponding binary. If the binary is in any of the directories, the system executes it. Use Windows System Properties to add Python's installation directory to the PATH variable.
๐ŸŒ
Real Python
realpython.com โ€บ add-python-to-path
How to Add Python to PATH โ€“ Real Python
July 23, 2026 - The path to the directory is what youโ€™ll be adding to the PATH environment variable. To find the Python executable, youโ€™ll need to look for a file called python.exe. The Python executable could be in a directory in C:\Python\ or in your AppData\ folder, for instance.
๐ŸŒ
Reddit
reddit.com โ€บ r/linux4noobs โ€บ how do i add a directory to path? i am having issues running a python script by just its name.
r/linux4noobs on Reddit: How do I add a directory to PATH? I am having issues running a python script by just its name.
September 4, 2019 -

I am trying to run a python script by just its name (practice.py) if possible. Or just python practice.py. Either way I would like to be able to run it regardless of what directory I am in. This file is in my Scripts directory. My path is /home/user/miniconda3/bin:/home/user/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:SCRIPTS_DIR. I added SCRIPTS_DIR. SCRIPTS_DIR = /home/user/Programming/Scripts. I added the path to my .bashrc. The file is executable. The script works if I am in the directory but I cannot figure out how to be able to call it from anywhere. I have tried multiple times to understand adding paths but so far no luck. Any help is appreciated.

๐ŸŒ
GitHub
bic-berkeley.github.io โ€บ psych-214-fall-2016 โ€บ using_pythonpath.html
Using PYTHONPATH โ€” Functional MRI methods
The PYTHONPATH variable has a value that is a string with a list of directories that Python should add to the sys.path directory list.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to add my project folder to the pythonpath?
r/learnpython on Reddit: How to add my project folder to the PYTHONPATH?
August 17, 2021 -

I am using a scheduler app that requires the full path of my project.pythonfile.method to run the method that needs to be scheduled.

But it is not working.

I noticed even from project.pythonfile import method is not found. I never needed that as I am using relative paths until now (from .pythonfile import method). But as my scheduler is a third party app, I don't think it will work.

I did some research and I think I need to append my project folder to the PYTHON PATH variable. Does anyone know how to do that? is there a GUI in the Windows interface? or is it all command based?

I assume it is different from the OS Environment PATH variable? or no?

I am using Windows btw.

๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ how to add python to path?
How To Add Python To Path?
November 15, 2024 - You can avoid this error by providing the full directory path to the Python executable each time you want to use it. For example: ... But that's cumbersome and prone to mistakes. A more convenient solution is to add the directory containing the Python executable to the Path variable.
๐ŸŒ
CodeGenes
codegenes.net โ€บ blog โ€บ how-to-add-directory-to-path-python
Adding a Directory to the Python Path โ€” codegenes.net
In Python, the `sys.path` is a crucial concept. It is a list of directories that Python searches through when it tries to import a module. Sometimes, you may have custom modules or libraries in a directory that Python doesn't know about by default. In such cases, you need to add that directory to the Python path so that Python can find and import the required modules.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to add a directory to sys.path?
r/learnpython on Reddit: how to add a directory to sys.path?
September 10, 2023 -

Hello everyone,

I have some modules I want to reuse, I don't want to parent import because they don't are related to the current project. The option I'll like to use is to add a directory to sys.path so they can be found. Also, I'd like to to achieve this preferably by configuration, not programmatically manipulating the sys.path value.

Just like venv does when activating the environment. I have looked into .venv/script/activate but there is no explicit instruction to include .venv/Lib/site-packages so, how does it do it?

๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 592199 โ€บ languages โ€บ add-directory-sys-path
How to add directory to sys.path?? (Jython/Python forum at Coderanch)
Directory structure is like this: - A-- --help.py B-- --two.py Now, two.py and help.py are in two parallel directory... I want to use help.py in two.py.. I have found one way to do this by adding following lines to two.py: - But I don't want to do this in every python module I make.. I python documentation I read that, sys.path is " Initialized from the environment variable PYTHONPATH, plus an installation-dependent default."
๐ŸŒ
Safjan
safjan.com โ€บ home โ€บ note โ€บ adding parent directory to python path
Adding Parent Directory to Python Path - Krystian Safjan's Blog
May 28, 2024 - Another way to use pathlib is to use pathlib.Path().joinpath() to add the parent directory to the python path.
๐ŸŒ
YouTube
youtube.com โ€บ rajarshi chattopadhyay
How to ADD FOLDERS to PYTHON PATH - Calling modules from anywhere in your PC - IMPORT ERROR - SOLVED - YouTube
This video demonstrates how to add any folder o your Windows 10 PC to the Python path. This makes calling your custom made modules easier and from anywhere i...
Published: July 2, 2019
Views: 2K
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to add the python scripts directory to PATH - YouTube
In this video I cover how to fix the warning message "Python scripts directory not in PATH". The warning is shown at: 1:52 - 2:38The fix is applied at: 3:05 ...
Published: January 7, 2024
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ add package, directory to pythonpath in windows?
Add Package, Directory to PYTHONPATH in Windows? - Spark By {Examples}
May 31, 2024 - What is PYTHONPATH in Python and how to add to it in Windows? You may need to use a custom module or package that is not included in the standard library.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-add-python-to-path-variable-in-windows
How to add Python to PATH variable in Windows
A typical installation would have the following path, C:\Users\MyUserName\AppData\Local\Programs\Python\Python310 ยท Now, we will need to add this to the PATH variable, we right click on "This PC" and go to "Properties".
๐ŸŒ
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 - In the example environment with Python3 installed by Homebrew on a Mac, the following directories are stored in sys.path. The directory containing the executed script file (.py) The directory set by the environment variable PYTHONPATH (more on this later) ... If you change your current directory in the terminal and execute the script again, 3. current directory will be updated to this new path.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ sys_path_init.html
The initialization of the sys.path module search path โ€” Python 3.15.2 documentation
The first entry in the module search path is the directory that contains the input script, if there is one. Otherwise, the first entry is the current directory, which is the case when executing the interactive shell, a -c command, or -m module. The PYTHONPATH environment variable is often used to add directories to the search path.
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.