5

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 ?

Josh
  • 61
  • 1
  • 4

3 Answers3

11

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.

Anakhand
  • 2,838
  • 1
  • 22
  • 50
  • so how can we fix this then – coderboy Nov 26 '20 at 10:46
  • He is trying to import a function from that file into another file, will running something like that on the CLI be appropriate. – coderboy Nov 26 '20 at 10:57
  • @coderboy Yes; the problem is how they're starting the script, not how they're importing the other module. – Anakhand Nov 26 '20 at 10:59
  • 0 Running the command: python -m src.start.go works like charm. Now, I have 4 GPU machines, and when I try to run my script in distributed mode like that: python -m torch.distributed.launch --nproc_per_node=4 src.start.go It throws me an error: 'no module src' It happens after adding distributed mode. How can I fix it @Anakhand ? – Josh Jan 08 '21 at 12:19
  • @Josh, you'll be better off asking a separate question for that, it has probably something to do with how `pytorch` loads modules and how you have configured your environment. – Anakhand Jan 08 '21 at 12:21
0

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

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

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

Leonardo
  • 120
  • 9
-3

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
coderboy
  • 741
  • 2
  • 17