Python No module named
Question: I have a custom module that I am trying to read from a folder under a
hierarchy:

    
    
    > project-source
            /tests
                /provider
                 my_provider.py
             settings_mock.py
             __init__.py
    

I am trying to call, from my_provider.py

    
    
    import tests.settings_mock as settings
    

Example from command line:

    
    
    project-source> python tests/provider/my_provider.py
    

Error:

    
    
    ... ImportError: No module named settings_mock
    

I keep getting `No module named settings_mock` as error. I have already
exported `project_source` path to PYTHONPATH. I have made tests into a package
by creating a `__init__.py` file in its root, but no change in the error then.

I can print the `settings_mock.py` attributes when cd'ing project source

    
    
    >>> import tests.settings_mock as settings
    >>> print settings.storage_provider
    correct storage provider value
    

Is anyone able to point out my mistake here? Thanks!


Answer: You only have one small mistake. To use subfolders, you need `__init__.py`,
not `init.py` as you stated in the question. The difference is that `__init__`
is a builtin function of python, whereas `init` is not. Having this file in
each subfolder tells the pyhon interpreter that the folder is a "package" that
needs to be initialized.

**UPDATED:** It should be noted that python usually runs from the current
directory that the script is located. If your executable main script is
`my_provider.py`, then it's not going to know what to import, since the main
script is located in a lower directory than the object it is trying to import.
Think of it as a hierarchy. Scripts can only import things that are beneath
them. Try separating out the executable from everything else in that file, if
there are things that `settings_mock` needs to import.

