1

I have the following structure

abc/ __init__.py settings.py tests/ __init__.py test.py 

in test.py, I am getting an ImportError for

#test.py import abc.settings 
5
  • 2
    You have to add the main directory (where abc is in), to your sys.path Commented Dec 15, 2014 at 22:17
  • @BlackVegetable oh, so if I move tests folder to the same level as abc then it should work Commented Dec 15, 2014 at 22:18
  • My comment was slightly mistaken. Look to @Wolph for the correct answer. Commented Dec 15, 2014 at 22:18
  • If you feel like getting your hands dirty, you can try out this module: docs.python.org/2/library/imp.html Commented Dec 15, 2014 at 22:38
  • That seems like an odd arrangement for tests. See e.g. jeffknupp.com/blog/2013/08/16/… for a handy guide to structuring a package. Commented Dec 16, 2014 at 15:08

2 Answers 2

2

You have two ways.

Firstly, by setting the path variable

import os import sys sys.path.insert(0, <Complete path of abc>) 

Or by using relative imports.

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

2 Comments

I cannot mess with sys.path as the framework already does this. could you talk about the relative import method? I tried from ..abc import settings and ran it with python -m but I'm still getting an error
Refer this
1

The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:

you need to add your root directory to sys.path :

import sys sys.path.append('path_of_root') 

Aldo '..'+sys.path[0] can give you the path of abc directory !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.