siemdejong / shit-happens

Create your own Shit Happens playing cards!
GNU General Public License v3.0
4 stars 2 forks source link

Relative imports only fetch from a locally pip installed version #26

Closed siemdejong closed 1 year ago

siemdejong commented 1 year ago

Describe the bug import shithappens.sort_situations imports from a pip installed version, but during development, it is useful to let it do relative imports from a local tree.

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\siemd\mambaforge\envs\shithappens\Scripts\shithappens.exe\__main__.py", line 7, in <module>
  File "C:\Users\siemd\mambaforge\envs\shithappens\Lib\site-packages\shithappens\create_cards.py", line 648, in main_cli
    main()
  File "C:\Users\siemd\mambaforge\envs\shithappens\Lib\site-packages\shithappens\create_cards.py", line 604, in main
    from sort_situations import sort
ModuleNotFoundError: No module named 'sort_situations'

To Reproduce Steps to reproduce the behavior:

  1. pip uninstall shithappens
  2. python src/shithappens/create_cards.py examples/example -r
  3. See error

Expected behavior No ModuleNotFoundError given.

Additional context Is a possible solution to use importlib.resources?

siemdejong commented 1 year ago

https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python

siemdejong commented 1 year ago
import sys
SCRIPT_DIR = Path(__file__).absolute().parent
sys.path.append(str(SCRIPT_DIR.parent))

is needed, but I prefer to not append the directory to the python path. Not good practice for a pypi package.

siemdejong commented 1 year ago

pip install -e . to install the development version.

relative imports with package.module.