Assuming your project is actually contained in a project directory like so, ...
my_package
|
├── gen.py
├── lexer
│ ├── __init__.py
│ ├── engine.py
| └── ...
├── parser
| ├── __init__.py
| ├── engine.py
| └── ...
├── tests
| ├── test_thingy.py
in gen.py:
import my_package.lexer.engine
import my_package.parser.engine
in the parent directory of my_package,you can run python -m my_package.gen
. This should run exactly as expected without the name conflicts. With similar import statements in your tests, if you run your test modules in the same way, it should work just fine.
I tested this with the following. In E/work/temp/
I have a directory called my_package
. It has the following structure.
my_package
|
├── __init__.py # needed in python 2, but not 3
├── import_test_b.py
├── parser
| ├── __init__.py
| └── import_test_a.py
└── tests
├── __init__.py # needed in python 2 but not python 3
└── test_imports.py
import_test_a:
def test(num):
return num+3
import_test_b:
from my_package.parser.import_test_a import tst
print(tst(4))
test_imports.py:
from my_package.parser.import_test_a import tst
import unittest
class TestTst(unittest.TestCase):
def test_one(self):
self.assertEqual(tst(4), 7)
if __name__ == '__main__':
unittest.main()
in E/work/temp
:
run: python -m my_package.import_test_b
- output = 7
run: python -m my_package.tests.test_imports
output:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK