Package not recognized correctly with relative imports in PyCharm 2017.3
Hi,
I am having trouble getting relative imports to work both in unit tests and in the package. Following various posts, I have:
- added "-m" when running python
- restructured my project like this:
data_processing (dir)
__init__.py
audio_processing.py
file_utils.py
tests (dir)
__init__.py
test_audio_processing.py
test_file_utils.py
In both test_audio_processing.py and test_file_utils.py, I import file_utils.py with the following:
from .. import file_utils as ftl
If I print out this line at the top of both files, in test_audio_processing.py, the __package__ is set correctly and I get no failure and in test_file_utils.py, the package is None and it fails:
print('__file__={0:<35} | __name__={1:<20} | __package__={2:<20}'.format(__file__,__name__,str(__package__)))
test_audio_processing.py:
Testing started at 5:32 PM ...
/Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_unittest_runner.py --path /Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_audio_processing.py
Launching unittests with arguments python -m unittest /Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_audio_processing.py in /Users/kmatsuda/sources/proappsprototypes2/mlresources
__file__=/Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_audio_processing.py | __name__=data_processing.tests.test_audio_processing | __package__=data_processing.tests
__file__=/Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/audio_processing.py | __name__=data_processing.audio_processing | __package__=data_processing
Ran 13 tests in 0.091s
OK
Process finished with exit code 0
test_file_utils.py:
Testing started at 5:15 PM ...
/Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_unittest_runner.py --path /Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_file_utils.py
Launching unittests with arguments python -m unittest /Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_file_utils.py in /Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests
__file__=/Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_file_utils.py | __name__=test_file_utils | __package__=
Error
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 601, in run
testMethod()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_file_utils
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/kmatsuda/sources/proappsprototypes2/mlresources/data_processing/tests/test_file_utils.py", line 10, in <module>
from .. import file_utils as ftl
ImportError: attempted relative import with no known parent package
Ran 1 test in 0.002s
FAILED (errors=1)
Process finished with exit code 1
//------------------
I don't know if this is the right way to invoke "python -m" even though it seems to print out "-m" in the console. The main goal is to use this package in code that is a client of this, so I needed to include file_utils.py from audio_processing.py. It isn't clear that I have that working correctly either, since I get a different set of issues resulting from the __package__ not being set in those cases.
Any help would be appreciated, since it isn't clear whether I am having trouble with PyCharm or with python's package and relative import syntax.
Please sign in to leave a comment.
I figured this out by creating a new project with a new package structure. For some reason, when I ran the test_*.py files individually, they worked, but unit tests failed with relative import errors when run from the "test" directory. If I examined the configuration, the working directory was set differently. If I set the working directory to be the top level project directory, then all the unit tests run and relative imports work correctly. I needed to reset the working directory manually when I was getting relative import errors.