1

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?

pistacchio
  • 56,889
  • 107
  • 278
  • 420

2 Answers2

1

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?

jrennie
  • 1,937
  • 12
  • 16
0

Relative imports are only allowed intra-packages wise. You mustn't confuse directory-references with package-handling.

Proposed solutions on a similar question.

Community
  • 1
  • 1
Don Question
  • 11,227
  • 5
  • 36
  • 54
  • thanks! but, in my example, i tried to sys.path.append('..') or sys.path.append('../') but from my_package import my_class still picks the installed one – pistacchio May 08 '12 at 12:10