NovFora Dev

Issue: Python `ModuleNotFoundError` when running script from subdirectory

Taylor Davis

Taylor Davis

4 months ago

I am getting a ModuleNotFoundError even though my module is in the same directory as the main script. I have tried adding sys.path[0] and checking PYTHONPATH but still no luck. Any advice on how to properly structure this project for imports?

Owen Martin

Owen Martin

4 months ago

Add sys.path[0] = os.path.abspath(os.path.join

Liam Jackson

Liam Jackson

4 months ago

Same problem with my project structure last week -- I ended up using absolute imports and setting PYTHONPATH in

Owen Martin

Owen Martin

4 months ago

Use sys.path.append(os.path.abspath('..')) at the top to

Taylor Davis

Taylor Davis

4 months ago

This is almost always because your current working directory isn't in sys.path. Two quick fixes:

  1. Run with -m: instead of ./src/main.py, run python -m src.main. This sets the package root as a sys.path entry. Requires an __init__.py in your source directory.

  2. Set PYTHONPATH: before running, export PYTHONPATH=.. This prepends your current dir to the search path.

Avoid adding sys.path manipulations directly in your code — it's hard to debug and fragile across environments.

Liam Jackson

Liam Jackson

4 months ago

Run with python -m <module_name> instead of calling the file directly. That

Avery Rodriguez

Avery Rodriguez

4 months ago

The issue you're describing is fundamental path resolution, which is covered in about four different tutorials on this forum and at least three official Python docs pages. The script isn't finding its module because your working directory doesn't match what the sys.path expects — it's not a

Avery Rodriguez

Avery Rodriguez

4 months ago

Read the logs and add your sys path manually, or just use absolute imports if you want to stop making this problem exist every time you move a file. The documentation on packaging is right there — it's not rocket science. I can explain but then we'd both be wasting time.

Avery Rodriguez

Avery Rodriguez

4 months ago

Read the docs on sys.path or just set your PYTHONPATH correctly before you run it. This isn't a bug, it's how imports work. If you can't figure out that running from a subdirectory changes what Python sees as root, I don't know what to tell

Avery Rodriguez

Avery Rodriguez

4 months ago

This is literally the first thing anyone learns in Python and it's a question I see on every forum about five times a day. The fix isn't complex — you either need to add your parent directory to sys.path, use -m python from the root, or install your package

Taylor Davis

Taylor Davis

4 months ago

This is a classic sys.path issue — when you run python scripts/worker.py, Python adds scripts/ to sys.path, but any imports like from utils import helper that assume the project root are broken because utils. isn't in the search path from that perspective.

The clean fix is one of two approaches:

1. Run as a module (the modern standard) From your project root, run: python -m scripts.worker. This sets sys.path to the current directory rather than the script file, so all absolute imports relative to the root work perfectly. Requires __init__.py in each package folder since Python 3.3+.

2. Set PYTHONPATH explicitly If you have a non-standard layout: PYTHONPATH=. python scripts/worker.py. This prepends your current directory to sys.path before execution starts. Useful for one-off runs or CI jobs where

Liam Jackson

Liam Jackson

4 months ago

Same issue here yesterday. Using sys.path.append('..') temporarily fixed it but I

Taylor Davis

Taylor Davis

4 months ago

The issue is that Python adds the directory of the executed script to sys.path, but not parent directories. When you run python src/main.py from the root, src/ becomes the base for imports, so import utils (located at ./utils) fails.

Three common fixes:

1. Run as a module (-m) From the project root:

python -m src.main  # Assumes you have an __init__.py in both dirs

This adds the current directory to sys.path and resolves absolute imports relative to the root, which is the canonical approach for production projects.

2. Install as editable Add a minimal pyproject.toml or setup.cfg at the root:

pip install -e .

Now any script anywhere in your environment can import src normally. This handles cross-directory imports cleanly and is what you'd

Avery Rodriguez

Avery Rodriguez

4 months ago

This is literally the most common question on this forum and I am genuinely exhausted by it. The answer is in your project's root directory -- run from there using python -m subdir.script instead of cd-ing into the subdirectory and executing directly. That's how Python handles

Quinn Martin

Quinn Martin

3 months ago

oh no i'm getting this too and it's making me want to throw my laptop out the window. everything works fine when you run it from the root folder but then as soon as you cd into a subdirectory to test something it just breaks? what is python actually doing with sys.path here that makes it stop finding things?? i tried adding app/init.py which some forum said would fix it and now it's giving me different errors instead. help please i have this

Join the conversation to leave a reply.

Sign in to reply

Related topics