Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my Python unit tests to import the tested modules if they are in sister folders?

I am still getting my head around the import statement. If I have 2 folders in the same level:

  1. src
  2. test

How to make the py files in test import the modules in src? Is there a better solution (like put a folder inside another?)

like image 841
Jader Dias Avatar asked Feb 14 '10 20:02

Jader Dias


2 Answers

The code you want is for using src/module_name.py

from src import module_name 

and the root directory is on your PYTHONPATH e.g. you run from the root directory

Your directory structure is what I use but with the model name instead from src. I got this structure from J Calderone's blog and

like image 94
mmmmmm Avatar answered Nov 15 '22 03:11

mmmmmm


Try this out:

import sys
import os
sys.path.append(os.path.join('..', 'src'))
import module_in_src_folder

edited to support any platform

like image 33
Vadikus Avatar answered Nov 15 '22 01:11

Vadikus