3

I have a project structure like this:

cartesianproduct
    test
         __init__.py
         test_cartesian_product.py
    __init__.py
    __main__.py
    main.py
    sequence_creation.py
    sequences_operations.py

In test_cartesian_product.py I import

from .. import sequences_operations
from .. import sequence_creation

In __main__.py I import from tests import test_cartesian_product_package and running test_cartesian_product_package.running_tests() But at startup __main__.py i get an error

from .. import sequences_operations
ImportError: attempted relative import beyond top-level package

If I just run test_cartesian_product.py i get the same error. How to fix it?

Nitrolacs
  • 31
  • 4
  • just simply write `import sequences_operations` in `test_cartesian_product.py` – sahasrara62 Nov 28 '21 at 16:09
  • Does this answer your question https://stackoverflow.com/questions/30669474/beyond-top-level-package-error-in-relative-import – Naveenkumar M Nov 28 '21 at 16:09
  • @sahasrara62 IDE tells me "No module named 'sequences_operations'" – Nitrolacs Nov 28 '21 at 16:21
  • @NaveenkumarM I tried some advice from there and the error didn't go away – Nitrolacs Nov 28 '21 at 16:28
  • have you followed the one with utmost upvotes – Naveenkumar M Nov 28 '21 at 16:30
  • @Nitrolacs Why are you using a `__main__.py` file? That is only meant to be used for running modules with `python -m`. Anyway, the way to solve all import problems like this is to use a startup script that is *outside of the top-level package* (and in the same directory). That script must then only use *absolute imports*, so as to act as a base for all relative imports within the package. Usually, it is very minimal, and just contains something like `if __name__ == '__main__': import sys; from toplevelpkg import mod; sys.exit(mod.run())`. – ekhumoro Nov 28 '21 at 17:20

0 Answers0