[image] Mohammed Zuhaib: Where exactly is the code for adding paths to sys.path on startup located in CPython source? The implementation starts in pymain_run_python() in “Modules/main.c”: python/cpython/blob/v3.11.2/Modules/main.c#L573-L587 else if (!config->s… Answer from eryksun on discuss.python.org
🌐
Python.org
discuss.python.org › python help
Sys.path does not have current directory by default - Python Help - Discussions on Python.org
March 29, 2023 - Hi, I am trying to do a debug build of Python-3.11.1 on NonStop systems(custom OS and somewhat similar to linux), I see that whenever I invoke the interpreter, sys.path does not have the current directory by default. This goes against the default behavior of windows and linux.
🌐
Python.org
discuss.python.org › python help
Sys.path is included in the current directory by default - Python Help - Discussions on Python.org
March 29, 2023 - Hi, I am trying a custom build of python 3.11 on linux, I see that sys.path has current directory by default, I would like to change this behavior by changing the cpython source, >>> import sys >>> sys.path ['', '/usr/local/lib/python311.zip', '/home/mohammed/python/Python-3.11.0/Lib', '/home/mohammed/python/Python-3.11.0/build/build/lib.linux-x86_64-3.11'] >>> where can I make desired changes in Cpython source code such that my current directory is not included by default?
Discussions

Not adding current directory to sys.path
I can't work out why some instances of ptpython add the current directory to sys.path. Most of my projects to date, using 0.41, do this with no issues, but in recent installations I've noticed that it is missing from sys.path. Is there a condition for this I'm missing? I can't do basic imports. Stock python ... More on github.com
🌐 github.com
10
June 6, 2018
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
import - How Python adds current directory to sys.path - Stack Overflow
I understand that Python would add "current directory" to the sys.path. More on stackoverflow.com
🌐 stackoverflow.com
November 9, 2020
sys.path does not automatically append the current working directory
Actual Behavior import sys sys.path returns a list of inbuilt paths but not the current working directory, which requires me to add the current working directory everytime. A regular python install... More on github.com
🌐 github.com
July 12, 2018
🌐
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.
🌐
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 - Modules are searched in the order listed in sys.path. Thus, modules in the directory of the currently executed script file (.py) are searched first. 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › sys-path-in-python
sys.path in Python - GeeksforGeeks
August 21, 2024 - 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. If not found it looks through the list of directories(a directory is a folder that contains related modules) defined by sys.path. ... DEFAULT- By default, the interpreter looks for a module within the current directory.
🌐
GitHub
github.com › prompt-toolkit › ptpython › issues › 246
Not adding current directory to sys.path · Issue #246 · prompt-toolkit/ptpython
June 6, 2018 - I can't work out why some instances of ptpython add the current directory to sys.path. Most of my projects to date, using 0.41, do this with no issues, but in recent installations I've noticed that it is missing from sys.path. Is there a condition for this I'm missing? I can't do basic imports. Stock python (3.6.4) interpreter and bpython - as a quick check - add the current directory without an issue.
Author: prompt-toolkit
🌐
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
🌐
Stack Overflow
stackoverflow.com › questions › 64746046 › how-python-adds-current-directory-to-sys-path
import - How Python adds current directory to sys.path - Stack Overflow
November 9, 2020 - Third case is a bit different. sys.path[0] is your current working directory, because python automatically adds your current working directory to sys.path before searching for importExample module
🌐
GitHub
github.com › ContinuumIO › anaconda-issues › issues › 9752
sys.path does not automatically append the current working directory · Issue #9752 · ContinuumIO/anaconda-issues
July 12, 2018 - Actual Behavior import sys sys.path returns a list of inbuilt paths but not the current working directory, which requires me to add the current working directory everytime. A regular python installation does that automatically. Expected ...
🌐
Stack Exchange
emacs.stackexchange.com › questions › 72221 › how-can-i-add-the-current-directory-to-pythonpath-automatically
python - How can I add the current directory to `PYTHONPATH` automatically? - Emacs Stack Exchange
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/steve/.virtualenvs/venv/lib/python3.10/site-packages', '/home/steve/Python/'] I was adding the path every project using os.chdir() but this didn't work for modules so I have been manually appending the path that I'm working in every new project I start by using sys.path.append() and this works fine. How can I have the current directory I'm in added to the path's without having to append it every time?
Top answer
1 of 3
31

Python adds the directory where the initial script resides as first item to sys.path:

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 what goes on underneath the surface is that Python appends (or rather, prepends) the 'local' directory to sys.path for you.

This simply means that the directory the script lives in is the first port of call when searching for a module.

__init__.py has nothing to do with all this. __init__.py is needed to make a directory a (regular) package; any such directory that is found on the Python module search path is treated as a module.

2 of 3
1

I have faced same problem when running python script from Intellij Idea. There is a script in a

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic

It uses

from meshtastic import portnums_pb2, channel_pb2, config_pb2

and fails. I have realized that it looks for

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic\meshtastic

and changed working directory of this script in Run Configuration from

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic

to

C:\Users\user\IdeaProjects\Meshtastic-python

so it can find this module UNDERNEATH workdir during execution

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic
🌐
KK's Blog
fromkk.com › posts › sys-dot-path-in-python
Sys.path in Python - KK's Blog (fromkk)
June 4, 2026 - By default, as initialized upon program startup, a potentially unsafe path is prepended to sys.path: ... python script.py: prepend the script’s directory. If it’s a symbolic link, resolve symbolic links. python -c and python (REPL): prepend an empty string, which means the current working directory.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-current-directory-python
Get Current directory in Python - GeeksforGeeks
July 12, 2025 - In this example, we have used sys.argv[0] to retrieve the path of the script file and os.path.dirname() extracts the current directory from the path.
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › sys_path.html
Where does Python look for modules? — Functional MRI methods
This is because the directory code that we specified is a relative path, and therefore Python looks for the code directory in the current working directory. To make the hack work when running the code from any directory, you could use some path manipulation on the The “__file__” variable: ... from os.path import dirname, abspath, join import sys # Find code directory relative to our directory THIS_DIR = dirname(__file__) CODE_DIR = abspath(join(THIS_DIR, '..', 'code')) sys.path.append(CODE_DIR) import a_module a_module.func()
🌐
Stack Overflow
stackoverflow.com › questions › 73104391 › why-isnt-the-current-directory-in-sys-path-for-a-django-application
python - Why isn't the current directory in sys.path for a django application - Stack Overflow
Which is the root directory of the project, instead of the current directory of urls.py which should be c:\\Users\\5403\\Desktop\\tutorial\\tutorial · Could someone explain this inconsistency and correct me if I have any misconceptions, thanks. ... Django modifies the sys.path to contain the project root directory so that things like import mysite.settings should work everywhere in your project. ... Also, Python does allow modification of sys.path.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get and Change the Current Working Directory in Python | note.nkmk.me
April 17, 2025 - Get the path of the current file (script) in Python: __file__ os.getcwd() returns the absolute path of the current working directory as a string (str).
🌐
Learnings
ankursingh.hashnode.dev › learnings › #python › the hitchhiker's guide to `sys.path`
The Hitchhiker's Guide to `sys.path` - Learnings
May 3, 2022 - You can read this blog or watch this video to better understand the difference between python modules and packages. One interesting thing to note is that, Python has automatically adds Current Working Directory (CWD) to the list.