Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail: ImportError: attempted relative import with no known parent package #237

Closed
yurivict opened this issue Feb 22, 2023 · 7 comments

Comments

@yurivict
Copy link

$ make test
===>  Testing for py39-openTSNE-0.7.1
===>   py39-openTSNE-0.7.1 depends on package: py39-pytest>=7,1 - found
===>   py39-openTSNE-0.7.1 depends on file: /usr/local/bin/python3.9 - found
cd /usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1/tests && /usr/bin/env XDG_DATA_HOME=/usr/ports/math/py-openTSNE/work-py39  XDG_CONFIG_HOME=/usr/ports/math/py-openTSNE/work-py39  XDG_CACHE_HOME=/usr/ports/math/py-openTSNE/work-py39/.cache  HOME=/usr/ports/math/py-openTSNE/work-py39 PATH=/usr/local/libexec/ccache:/usr/ports/math/py-openTSNE/work-py39/.bin:/home/yuri/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin PKG_CONFIG_LIBDIR=/usr/ports/math/py-openTSNE/work-py39/.pkgconfig:/usr/local/libdata/pkgconfig:/usr/local/share/pkgconfig:/usr/libdata/pkgconfig MK_DEBUG_FILES=no MK_KERNEL_SYMBOLS=no SHELL=/bin/sh NO_LINT=YES PREFIX=/usr/local  LOCALBASE=/usr/local  CC="cc" CFLAGS="-O2 -pipe  -fstack-protector-strong -isystem /usr/local/include -fno-strict-aliasing "  CPP="cpp" CPPFLAGS="-isystem /usr/local/include"  LDFLAGS=" -fstack-protector-strong -L/usr/local/lib " LIBS=""  CXX="c++" CXXFLAGS="-O2 -pipe -fstack-protector-strong -isystem /usr/local/include -fno-strict-aliasing  -isystem /usr/local/include "  MANPREFIX="/usr/local" CCACHE_DIR="/tmp/.ccache" BSD_INSTALL_PROGRAM="install  -s -m 555"  BSD_INSTALL_LIB="install  -s -m 0644"  BSD_INSTALL_SCRIPT="install  -m 555"  BSD_INSTALL_DATA="install  -m 0644"  BSD_INSTALL_MAN="install  -m 444" PYTHONPATH=/usr/ports/math/py-openTSNE/work-py39/stage/usr/local/lib/python3.9/site-packages /usr/local/bin/python3.9 -m pytest -k '' -rs -v -o addopts= 
========================================================================================== test session starts ==========================================================================================
platform freebsd13 -- Python 3.9.16, pytest-7.2.1, pluggy-1.0.0 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1/tests/.hypothesis/examples')
Using --randomly-seed=4136650396
rootdir: /usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1
plugins: forked-1.4.0, hypothesis-6.65.2, mypy-plugins-1.10.1, cov-2.9.0, randomly-3.12.0, timeout-2.1.0, rerunfailures-10.1, flaky-3.7.0, xdist-2.5.0, env-0.6.2, freezegun-0.4.2, mock-3.10.0
collected 118 items / 1 error                                                                                                                                                                           

================================================================================================ ERRORS =================================================================================================
___________________________________________________________________________ ERROR collecting tests/test_nearest_neighbors.py ____________________________________________________________________________
ImportError while importing test module '/usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1/tests/test_nearest_neighbors.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_nearest_neighbors.py:17: in <module>
    from .test_tsne import check_mock_called_with_kwargs
E   ImportError: attempted relative import with no known parent package
=========================================================================================== warnings summary ============================================================================================
../../../../../../local/lib/python3.9/site-packages/pytest_freezegun.py:17: 236 warnings
  /usr/local/lib/python3.9/site-packages/pytest_freezegun.py:17: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
    if LooseVersion(pytest.__version__) < LooseVersion('3.6.0'):

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
==================================================================================== 236 warnings, 1 error in 1.51s =====================================================================================
*** Error code 2

Version: 0.7.1
Python-3.9
FreeBSD 13.1

@pavlin-policar
Copy link
Owner

Hi, I don't at all understand how exactly you are running this. openTSNE doesn't use make, and you appear to be running a makefile.

The error you're getting is most likely as a result of the Python paths being setup incorrectly. Without knowing how exactly you're running the tests, what your environment is, and why you're even running the tests in the first place, it's hard for me to make any useful suggestions.

@yurivict
Copy link
Author

@pavlin-policar

The make there is because it is run from the ports framework.

The complete command that runs tests is also there, it begins with "cd".

@pavlin-policar
Copy link
Owner

I am unfamiliar with FreeBSD and the ports framework, but from what I can tell, ports is a FreeBSD package manager, and someone has made openTSNE available there as well. I'm very happy to see that!

Based on https://cgit.freebsd.org/ports/commit/?id=20e55e0cf2b75fa0988ae31c9814d1b8844a5428 (I'm guessing you're the author of that patch), you want to run the unit tests once the package has finished installing?

I'm not entirely sure why this is happening, it could have something to do with Python paths. You may be overwriting the Python path in

+TEST_ENV=	${MAKE_ENV} PYTHONPATH=${STAGEDIR}${PYTHONPREFIX_SITELIBDIR}

Another thing that I might try would be to run pytest from the root package directory. This is what I do on my CI servers. pytest seems to be fairly smart about finding and running tests. In that case, you'd probably need to change

+TEST_WRKSRC=	${WRKSRC}/tests

to

+TEST_WRKSRC=	${WRKSRC}

I see that the tests weren't run before the patch I've been referring to at all. Obviously, I understand you wanting to run tests on the installed package to make sure everything is working correctly, but why the sudden change now?

@yurivict
Copy link
Author

Tests weren't run before. I only added the 'test' target in this recent commit.

This line:

TEST_ENV=	${MAKE_ENV} PYTHONPATH=${STAGEDIR}${PYTHONPREFIX_SITELIBDIR}

allows to run openTSNE without installing it, from the stage directory.

Without TEST_WRKSRC= ${WRKSRC}/tests tests fail like this:

================================================================================================ ERRORS =================================================================================================
_______________________________________________________________________________ ERROR collecting tests/test_affinities.py _______________________________________________________________________________
ImportError while importing test module '/usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1/tests/test_affinities.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_affinities.py:10: in <module>
    from openTSNE import affinity, nearest_neighbors
openTSNE/__init__.py:1: in <module>
    from .tsne import TSNE, TSNEEmbedding, PartialTSNEEmbedding, OptimizationInterrupt
openTSNE/tsne.py:11: in <module>
    from openTSNE import _tsne
E   ImportError: cannot import name '_tsne' from partially initialized module 'openTSNE' (most likely due to a circular import) (/usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1/openTSNE/__init__.py)

And when I install the py39-openTSNE into the system and run pytest from the project's root directory it fails like this:

[yuri@yv /usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1]$ pytest
========================================================================================== test session starts ==========================================================================================
platform freebsd13 -- Python 3.9.16, pytest-7.2.1, pluggy-1.0.0
Using --randomly-seed=2922070264
rootdir: /usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1
plugins: forked-1.4.0, hypothesis-6.65.2, cov-2.9.0, mypy-plugins-1.10.1, randomly-3.12.0, timeout-2.1.0, rerunfailures-10.1, flaky-3.7.0, xdist-2.5.0, env-0.6.2, freezegun-0.4.2, mock-3.10.0
collected 118 items / 1 error                                                                                                                                                                           

================================================================================================ ERRORS =================================================================================================
___________________________________________________________________________ ERROR collecting tests/test_nearest_neighbors.py ____________________________________________________________________________
ImportError while importing test module '/usr/ports/math/py-openTSNE/work-py39/openTSNE-0.7.1/tests/test_nearest_neighbors.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_nearest_neighbors.py:17: in <module>
    from .test_tsne import check_mock_called_with_kwargs
E   ImportError: attempted relative import with no known parent package
=========================================================================================== warnings summary ============================================================================================
../../../../../local/lib/python3.9/site-packages/pytest_freezegun.py:17: 236 warnings
  /usr/local/lib/python3.9/site-packages/pytest_freezegun.py:17: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
    if LooseVersion(pytest.__version__) < LooseVersion('3.6.0'):

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================================================================================== short test summary info ========================================================================================
ERROR tests/test_nearest_neighbors.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
==================================================================================== 236 warnings, 1 error in 1.40s =====================================================================================

@pavlin-policar
Copy link
Owner

Sorry for the late reply. Is this still an issue or have you managed to track down what the problem was?

@pavlin-policar
Copy link
Owner

I am closing this due to inactivity, but please feel free to reopen this if the problem persists.

@yurivict
Copy link
Author

@pavlin-policar
1.0.0 still has the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants