Josh
Josh

Reputation: 61

Python ModuleNotFoundError: No module named 'src'

I know its a common question but somehow dosnt work for me. Here is my project structure:

MAIN
| --src
     | -- _init__.py
     | -- data 
            | -- __init__.py
            | -- operation.py
      | start
          | __init__.py
          | go.py

in file go.py i have:

from src.data.operation import X

When im starting go file from MAIN directory:

python src/start/go.py

It throws: ModuleNotFoundError: No module named 'src'

How can I fix it ?

Upvotes: 5

Views: 13868

Answers (4)

Leonardo
Leonardo

Reputation: 318

Check this: https://stackoverflow.com/a/66180118/12568621

Linux:

export PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"

Windows:

set PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"

Upvotes: 0

Aaron
Aaron

Reputation: 1

This solution worked Perfectly :-

Adding a setup.py file would help in the project's root folder with the code :

from setuptools import setup, find_packages

setup(name='llm-pr', version='1.0', packages=find_packages()) 

After so, in the terminal paste the command: pip install -e .

  • -e for interactive installation
  • . for this directory

Example of terminal and directory

Reference: https://www.youtube.com/watch?v=Mgp6-ZMEcE0

Upvotes: 0

Anakhand
Anakhand

Reputation: 3018

The problem is that you're running src.start.go as if it were a script, i.e. by giving the full path to the python command:

python src/start/go.py

This makes Python think this go.py file is a standalone script that isn't part of any package. As defined in the docs (see here, under <script>), this means that Python will search for modules in the same directory as the script, i.e. src/start/:

If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the __main__ module.

But of course, src is not in src/start/, so it will throw an error.

The correct way to start a script that is part of a package is to use the -m switch (reference), which takes a module path as an argument and executes that module as a script (but keeping the current working directory as a module search path):

If this option is given, [...] the current directory will be added to the start of sys.path.

So you should use the following to start your program:

python -m src.start.go

Now when src.start.go tries to import the src package, it will search in your current working directory (which contains the src/... tree) and succeed.


As a personal recommendation: don't put runnable scripts inside packages. Use packages only to store all the logic and functionality (functions, classes, constants, etc.) and write a separate script to run your application how you want it to, putting it outside the package. This will save you from these kind of problems, and has also the advantage that you can write several run configurations for the same package by just making a separate startup script for each.

Upvotes: 14

coderboy
coderboy

Reputation: 769

Check the file structure, the first tree should have init.py and not init_.py

MAIN
| --src
     | -- __init__.py
     | -- data 
            | -- __init__.py
            | -- operation.py
      | start
          | __init__.py
          | go.py

Upvotes: -3

Related Questions