1

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!

6
  • You can use relative imports, if your subfolders are packages (i.e. they contain an __init__.py): from .. import settings_mock Commented Jul 6, 2016 at 15:08
  • just add an empty __init__.py to your tests directory Commented Jul 6, 2016 at 15:09
  • where's the __init__.py. Commented Jul 6, 2016 at 15:09
  • I have already added __init__py in the folder tests. I now updated the question for clarity. Commented Jul 6, 2016 at 15:11
  • where is the main script (the one you're executing) located? It could have something to do with the path. Commented Jul 6, 2016 at 15:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry, the formatting of the SO question didn't show but I had written __init__.py (edited the question to show this), so it isn't the problem.
Thanks for the update. I'll see if I can reproduce your problem again.
I was unable to reproduce the issue with your setup, while running a main.py in the project-source directory that imports the settings_mock.py. Where are you running the main.py file from?
my working directory is project-source but the script being run is project-source/tests/provider/my_provider.py
How are you changing the working directory to project-source? Traditionally, the working directory is set to wherever the script is located.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.