9

I have been looking for a way to solve this.

I have a python project, and this is the folder structure I want:

/project/main.py /project/src/models.py /project/test/tests.py 

I want to be able to run the tests by executing the tests.py in terminal. tests.py imports modules in /project/src/ for testing. First I solved this by adding sys.path.insert(0, '..') in tests.py. But then the paths used in models.py for opening text files had to be relative to the tests.py, etc. Which means the program wouldn't run when excecuted from main.py, cause of the paths.

I also tried with dots when importing modules into tests.py, like from ..src.models import *, but that gave error message saying "Attempted relative import in non-package".

What should I put in the top of tests.py to be able to import the modules from models.py?

1
  • What version of Python are you using? Commented Dec 19, 2013 at 23:30

2 Answers 2

4

The structure you're using is not one I would recommend, but I'm a comparaitive newb to how Python projects are usually structured. I believe this will do what you're after:

1) Place an __init__.py file inside /project, /project/src, and /project/test to make sure they're treated as packages.

2) Place from __future__ import absolute_import at the top of each Python file.

3) Then use relative imports:

test.py:

from ..src import models 

main.py:

from .src import models 

4) You'll need to start your application differently. Ensure your current directory is the parent of /project (which appears to be the file system root) and run your project this way:

python -m project.main 

For my own project, I would definitely put main.py inside src if it's the start point of your application. I might put tests.py in src, too, but if not, I would add /project/src to the test runner's Python path on the command line instead of in code.

I would still use absolute_import regardless. In my experience, it's a very clean solution to module organization, and it's also how things work by default in Python 3.

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

5 Comments

I still get this: ValueError: Attempted relative import in non-package. I got init.py everywhere now.
@user1121487 Yeah. Just realized that. Sorry. My edit should fix it, if you didn't figure it out already.
@user1121487 Updated. You have to run Python differently. I think this explains why I would arrange my files differently.
Still can't get it to work. ImportError: No module named models.
@user1121487 I can't reproduce that. How are you getting it?
0

first put your main.py in the src directory..

in your tests you can do , sys.path.append('the src directory')

if you like to force the execution to be in specific directory regardless from where you are executing the app i suggest you adding

import os os.chdir('relative path to the src dir') 

thisway your program will run in the directory you specified so it will respect the relative paths you have in your code

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.