Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving Import Error: No Module named ***, but has __init__.py

I understand that this question has been asked several times but after reading them, and making the suggested fixes, I'm still stumped.

My project structure is as follows:

Project
      |
      src
        |
         root - has __init__.py
            |
            nested - has __init__.py
                  |
                  tests - has __init__.py
                  |
                  utilities - has __init__.py
                  |
                  services - has __init__.py

I've successfully run a unittest regression class from Eclipse without any issues.

As soon as I attempted to run the same class from the command-line (as other users who will be running the suite do not have access to Eclipse) I receive the error:

ImportError: No module named 'root'

As you can see from above, the module root has an __init__.py All __init__.py modules are completely empty.

And assistance would be gratefully received.

like image 759
Mark Rowlands Avatar asked May 10 '13 11:05

Mark Rowlands


People also ask

How do I fix No module named error in Python?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

What is __ init __ py for?

The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.


1 Answers

Try adding a sys.path.append to the list of your imports.

import sys
sys.path.append("/Project/src/")
import root
import root.nested.tests
like image 164
Gus E Avatar answered Oct 23 '22 09:10

Gus E