import series works when series.py is in the same folder as your program. Move to another folder and the same import fails. This section explains why.
sys.path
When you import a module, Python looks through a list of folders, in order, and uses the first matching file it finds. That list is sys.path:
How sys.path is built (in this order)
1. The folder of the script being run (or '' = current directory, in the interactive shell)2. Folders listed in the PYTHONPATH environment variable, if it is set3. Installation folders: the standard library and site-packages (where pip installs third-party packages)
If no folder contains the module, Python raises ModuleNotFoundError: No module named '...'.
Making your own module importable
Method
How
Lasts
Keep the module next to the script
Put series.py in the program’s folder
Always (simplest)
Append to sys.path in the program
sys.path.append("/path/to/folder") before importing
Only for that run
Set PYTHONPATH
e.g. export PYTHONPATH=/path/to/folder (Linux/macOS) or System Properties → Environment Variables (Windows)
Every run in that environment
Install it as a package
pip install a packaged project (pip install -e . during development)
Permanently
Key takeaways
sys.path is the ordered list of folders searched on import.
It starts with the script’s folder, then PYTHONPATH, then the installation folders.
A missing module raises ModuleNotFoundError.
Extend the path with sys.path.append, PYTHONPATH, or by installing the code.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
List three ways to make a module stored in D:\mylib importable from any of your programs, and say which lasts only for the current run.
Show worked answer
(1) Add D:\mylib to the PYTHONPATH environment variable, which affects every Python run that sees that variable. (2) Call sys.path.append(r"D:\mylib") at the start of a program before importing, which lasts only for the current run. (3) Package the code and install it with pip, which places it in site-packages, a folder that is always on the path. (Copying the module into the program’s own folder also works, for that program only.)
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/modules-and-packages/the-module-search-path/ to read this page.