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?

asked May 8, 2012 at 10:44

2 Answers 2

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?

answered May 8, 2012 at 15:31
Sign up to request clarification or add additional context in comments.

Comments

0

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

Proposed solutions on a similar question.

answered May 8, 2012 at 10:56

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.