I developed a solution with the following structure:
my_package/
my_test_data/
test.py
In test.pyI can easily import my_package (from my_package import my_class). This is very useful in my IDE of choice where I can write test cases, execute them and eventually set breakpoints in the code being tested where needed.
The fine structure, ready for distribution, changed to:
my_package/
tests/
my_test_data/
test.py
This is ok if someone wants to test that what's been installed works fine. Test references the installed version of my_package. The problem is that during development I need to reference my_package from the development folder, so that I can test the live version I'm developing and eventually step into it for debugging purposes. I tried to solve the problem with relative import from .my_package import my_class, from .. my_package import my_class and other combinations but I get this Exception:
ValueError: Attempted relative import in non-package
Any help?
2 Answers 2
I'll assume that the development structure is under /dev and the distribution structure is under /install.
Note that, by default, sys.path has the script directory as its first entry. So, if test.py has an import my_package statement and you run /dev/test.py, it should find my_package, even if your $PYTHONPATH is empty. If /install is the first entry in $PYTHONPATH, then running /install/tests/test.py should find /install/my_package for import my_package.
Summary: have you tried using import my_package in test.py and including /install in your $PYTHONPATH?
Comments
Relative imports are only allowed intra-packages wise. You mustn't confuse directory-references with package-handling.
Proposed solutions on a similar question.