🌐
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 - How to import modules, packages, ... · sys.path is a list of strings specifying the module search path. You can view these paths using print()....
🌐
Reddit
reddit.com › r/learnpython › python environment and modules
r/learnpython on Reddit: Python environment and modules
April 6, 2022 -

can anyone point me to a good resource that explains or guides me on setting up a robust(for lack of a better term) python environment?

as a learner, I am constantly running into issues with installing modules/libraries that end with a lot of google searching and trial and error and not knowing what did it in the end.

I am currently working on a script using the netifaces module.It installed correctly, and I can import it and use it using the python command line, but if I switch to jupyter notebooks, it can't find it.

It's just become a nuisance that I'd like to finally get to grips with

edit: running Windows 10 by the way

Top answer
1 of 2
2
It's less setting up an environment and more about understanding how environments work. Python has a list of locations it searchers for modules in. This list can be seen if you run the following code. import sys print(sys.path) The shown list is the list of directories the CURRENTLY RUNNING PYTHON will look into to find modules you will "import". the all capitals letters on "CURRENTLY RUNNING PYTHON" is on purpose because the things you describe happens when you have multiple python executables/installations on your system. pip installing modules into one, will probably won't help the other one see the modules as it looks in different directories. (Run the above code with python command line, and from jupyter notebook, you'll see different output) Now virtual environments are just another copy of python inside a local directory. You can see the python executable that runs the code if you run: import sys print(sys.executable) To solve this you either need to consolidate your environments to one, and that can be difficult because different products (jupyter,manual install, pycharm) install and work with their own copy of python = their own environment. Or you just need to accept that, and remember to install modules you need on the environments you need.
2 of 2
1
as a learner, I am constantly running into issues with installing modules/libraries that end with a lot of google searching and trial and error and not knowing what did it in the end. It doesn't get better as you progress. The thing that changes is that in companies, and the bigger they are the more likely it is to happen, there will be dedicated infra people who will solve this mess for you. Python packaging is a dumpster fire, and the more you know about it, the worse it gets. If you aren't aiming for infra / ops position, it's probably better if you find some way that things work for you, and once things need to work for real, have infra people set it up.
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. Unless explicitly noted oth...
🌐
Leemendelowitz
leemendelowitz.github.io › blog › how-does-python-find-packages.html
How does python find packages? // Lee On Coding // My blog about coding and stuff.
>>> import sys >>> print '\n'.join(sys.path) /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/lib/python2.7/dist-packages
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.

🌐
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 ·
Find elsewhere
🌐
Entri
entri.app › home › what does import sys print(sys.path) output?
What does import sys print(sys.path) output?
July 28, 2025 - sys.path returns a list of directories where Python searches for modules. Suggest Corrections · View Solution · View Solution · View Solution View More · Strengthen Your Practice with our comprehensive question bank. Download Now · Basics of Engineering · Civil Engineering · Data Science and Machine Learning · Digital Marketing · Electronics and Communication · Abstraction · Basic Printing in Python ·
🌐
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)
🌐
Practical-neuroimaging
practical-neuroimaging.github.io › python_path.html
Course on Practical Neuroimaging in Python — Practical neuroimaging analysis
When Python is looking for a new module, like myfunctions.py, it searches a list of directories stored as the path variable, in the sys module. Here is the contents of that variable on my system: >>> import sys >>> print('\n'.join(sys.path)) /Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-s
🌐
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:
🌐
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?
🌐
Tutorial Teacher
tutorialsteacher.com › python › sys-module
Python Sys Module
import sys print(sys.path) Try it · This attribute displays a string containing the version number of the current Python interpreter. Example: sys.version Copy · import sys print(sys.version) Try it ·
🌐
Learnings
ankursingh.hashnode.dev › learnings › #python › the hitchhiker's guide to `sys.path`
The Hitchhiker's Guide to `sys.path` - Learnings
May 3, 2022 - The first and the most obvious question is, "Where does the import statement search for packages?". Its quite simple, just run the following: ... As you can see, sys.path is just a python list with a bunch of strings (representing paths). import statement uses paths in sys.path list to search for modules/packages.
🌐
Michael Currin
michaelcurrin.github.io › dev-cheatsheets › cheatsheets › python › libraries › built-in › sys.html
sys | Dev Cheatsheets
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.
🌐
Xmodulo
xmodulo.com › change-syspath-pythonpath-python.html
How to change sys.path or PYTHONPATH in Python
September 23, 2020 - By default, sys.path is constructed as a concatenation of (1) the current working directory, (2) content of PYTHONPATH environment variable, and (3) a set of default paths supplied by the installed Python interpreter. If the module you are trying to import is not found in any of the directories defined in sys.path, you will encounter the following error:
🌐
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.