I suggest not to rely on the raw PYTHONPATH because it can vary depending on the OS.

Instead of the PYTHONPATH value in the os.environ dict, use sys.path from the sys module. This is preferrrable, because it is platform independent:

import sys
print(sys.path)

The value of sys.path is initialized from the environment variable PYTHONPATH, plus an installation-dependent default (depending on your OS). For more info see

https://docs.python.org/2/library/sys.html#sys.path

https://docs.python.org/3/library/sys.html#sys.path

Answer from Maxime Lorant on Stack Overflow
People also ask

How do I add PYTHONPATH inside a Python script?
Changing os.environ["PYTHONPATH"] changes the process environment and can affect child processes started afterwards. For an import in the running interpreter, add the intended directory to sys.path before importing, or install the package into that interpreter’s environment.
🌐
askpython.com
askpython.com › python › built-in-methods › pythonpath-in-shell
How to Get the Pythonpath in Shell? - AskPython
Is PYTHONPATH required to import installed packages?
Python adds applicable package locations through its normal initialization. If an installed package cannot be imported, identify the active interpreter before adding extra paths.
🌐
askpython.com
askpython.com › python › built-in-methods › pythonpath-in-shell
How to Get the Pythonpath in Shell? - AskPython
How do I set multiple directories in PYTHONPATH?
Join directory paths with the operating system’s separator. It is a colon on Linux and macOS and a semicolon on Windows, and os.pathsep reports the appropriate character.
🌐
askpython.com
askpython.com › python › built-in-methods › pythonpath-in-shell
How to Get the Pythonpath in Shell? - AskPython
🌐
GeeksforGeeks
geeksforgeeks.org › python › sys-path-in-python
sys.path in Python - GeeksforGeeks
August 21, 2024 - THROUGH ENVIRONMENT VARIABLES- An environment variable that contains the path an interpreter can take while looking for modules can be employed. Once set, it hints interpreter with directories to locate a module. The following example shows how this can be done. PYTHONPATH=C:\Users\Vanshi\Desktop · Python · # importing module import sys # printing all directories sys.path ·
🌐
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 - sys.path — System-specific parameters and functions — Python 3.11.3 documentation · sys.path is a list of strings specifying the module search path. You can view these paths using print().
🌐
Medium
medium.com › @gokulg.me › python-sys-module-from-beginner-to-advanced-b0f6bc73c8c2
Python sys Module: From Beginner to Advanced | by Gokul G | Medium
February 26, 2025 - Python searches for modules in directories listed in sys.path. You can append a new path dynamically: sys.path.append("/custom/path/to/module") print(sys.path)
Find elsewhere
🌐
Linux Hint
linuxhint.com › sys-path-append-python
How to Use Sys.path.append() in Python – Linux Hint
By declaring the following, we can check the current Python path: import sys from pprint import pprint pprint(sys.path) Here, the pprint module is just used to make things look pretty, so you can ignore that part, and concentrate on the rest. All we’re trying to do here is to print out sys.path.
🌐
How to Use Linux
howtouselinux.com › home › understanding sys.path in python
understanding sys.path in Python - howtouselinux
October 9, 2025 - To access a function or variable in an imported module, you need to use the dot (.) operator. For example, if you want to use the print function from the sys module, you would use the following syntax: ... Despite these potential problems, sys.path is a powerful tool that can make your life as a Python programmer a lot easier.
🌐
W3docs
w3docs.com › home › code snippets › python › how do i find out my pythonpath using python?
How do I find out my PYTHONPATH using Python? | W3docs
You can use the sys module in Python to access the sys.path variable, which contains a list of directories that Python looks in for modules to import. Here is a code snippet that you can use to print out the current value of sys.path:
🌐
CodeGenes
codegenes.net › blog › how-to-check-python-path
How to Check Python Path — codegenes.net
The sys module in Python provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter. You can use the sys.path variable to check the Python path in a Python script. import sys # Print the Python path print(sys.path)
🌐
Dummies
dummies.com › article › technology › programming-web-design › python › how-to-find-path-information-in-python-148336
How to Find Path Information in Python | dummies
December 28, 2021 - You see the Python Shell window appear. Type import sys and press Enter. Type for p in sys.path: print(p) in a new cell and click Run Cell
Author:
🌐
YouTube
youtube.com › watch
Python Programming 53 - Sys.path and Changing Module Paths - YouTube
Start your software dev career - https://calcur.tech/dev-fundamentals 💯 FREE Courses (100+ hours) - https://calcur.tech/all-in-ones🐍 Python Course - https:...
Published: September 10, 2020
🌐
GitHub
gist.github.com › bngsudheer › 8b84b2e4dd5440333621e603104ccf6c
Print $PYTHONPATH · GitHub
print-python-path.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
PythonAnywhere
help.pythonanywhere.com › pages › DebuggingImportError
Debugging with sys.path / ImportError issues | PythonAnywhere Help
May 13, 2015 - If it loads OK, it will drop you into a Python shell. Try doing the import manually at the command line. Then, check whether they really are coming from where you think they are: from foo.bar import baz import foo print(foo.__file__) # this should show the path to the module. Is it what you expect? import sys print('\n'.join(sys.path)) # does this show the files and folders you need?
🌐
YouTube
youtube.com › watch
PYTHON sys.path and PYTHONPATH - YouTube
Where does Python look to find modules and packages? How can you adjust the Python search path so that modules that you create can be located.
Published: July 23, 2021
🌐
GitHub
github.com › pybind › pybind11 › discussions › 4511
Pybind11 and python sys.path · pybind/pybind11 · Discussion #4511
May 5, 2023 - #include <pybind11/stl.h> #include <pybind11/pybind11.h> #include <pybind11/embed.h> #include <pybind11/numpy.h> namespace py = pybind11; int main() { py::scoped_interpreter guard{}; py::exec("import sys\nprint(sys.path)"); } ... > ./main ['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/lib/python3.10/site-packages'] ... In [1]: import sys In [2]: print(sys.path) ['/usr/bin', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '', '/home/xxx/.local/lib/python3.10/site-packages', '/usr/lib/python3.10/site-packages']
Author: pybind