rochacbruno / python-project-template

DO NOT FORK, CLICK ON "Use this template" - A github template to start a Python Project - this uses github actions to generate your project based on the template.
https://github.com/rochacbruno/python-project-template/generate
The Unlicense
1.06k stars 157 forks source link

ImportError: attempted relative import with no known parent package when running python project_name/__main__.py #33

Closed thekyria closed 1 year ago

thekyria commented 1 year ago

Repro steps

git clone the repository and then

python -m venv venv
. venv/Scripts/activate
pip install ../python-project-template/

The project_name command works well.

$ project_name
This will do something

However, when running directly __main__.py I get the following:

$ python ../python-project-template/project_name/__main__.py
Traceback (most recent call last):
  File "C:\tmp\python-project-template\project_name\__main__.py", line 3, in <module>
    from .cli import main  # pragma: no cover
ImportError: attempted relative import with no known parent package
(venv)

Minor investigation

I noticed that if the corresponding line is changed to (removing the .)

from cli import main  # pragma: no cover

The python command goes through:

$ python ../python-project-template/project_name/__main__.py
This will do something

However, in this case, the installed project_name stops working.

$ project_name
Traceback (most recent call last):
  File "C:\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\tmp\venvs\venv\Scripts\project_name.exe\__main__.py", line 4, in <module>
  File "c:\tmp\venvs\venv\lib\site-packages\project_name\__main__.py", line 3, in <module>
    from cli import main  # pragma: no cover
ModuleNotFoundError: No module named 'cli'
(venv)

All above observations hold irrespective of whether pip install ../python-project-template/ is run with the editable (-e) flag or not.

Desktop