napari / cookiecutter-napari-plugin

Cookiecutter for napari plugins
BSD 3-Clause "New" or "Revised" License
69 stars 41 forks source link

Editable install fails with pyproject.toml #185

Closed guiwitz closed 5 months ago

guiwitz commented 6 months ago

I created a plugin using the cookiecutter and specifying the versioning should be done with setuptools_scm. Then doing an editable install fails, with an error message ending with ModuleNotFoundError: No module named 'napari_skimage' where napari_skimage is my plugin name.

It seems that I ran into exactly this problem, where there's an attempt to import the package (which is not yet installed) by this line in the toml file:

[tool.setuptools.dynamic]
version = {attr = "napari_skimage.__init__.__version__"}

It's not clear to me exactly how this can be fixed (it might be related to the difficulty of importing a package in the src structure). It seems to be a known issue as there's a specific note in the setuptools docs:

attr first tries to read the value from the module by examining the module’s AST. If that fails, attr falls back to importing the module, using importlib.util.spec_from_file_location() recommended recipe (see example on Python docs about “Importing a source file directly”). Note however that importing the module is error prone since your package is not installed yet. You may also need to manually add the project directory to sys.path (via setup.py) in order to be able to do that.

that is not very helpful to me.

In the meantime, I managed to make an editable install by commenting out this in the toml file:

#[tool.setuptools.dynamic]
#version = {attr = "napari_skimage.__init__.__version__"}

#[tool.setuptools.packages.find]
#where = ["src"]

and adding back a setup.cfg file with:

[options]
package_dir =
    =src
setup_requires = setuptools_scm

[options.packages.find]
where = src
Czaki commented 6 months ago

hm. It may be a bug in template.

Did you configure project to use setuptools_scm? If yes then these lines should be obsolete:

[tool.setuptools.dynamic]
version = {attr = "napari_skimage.__init__.__version__"}

at least my project does not contain them https://github.com/4DNucleome/PartSeg/blob/develop/pyproject.toml

guiwitz commented 6 months ago

Yes I did select the setuptools_scm. Indeed just commenting out these lines fixes the problem (even without a setup.cfg file), so I guess this should only be present if not using setuptools_scm. I made a plugin without the setuptools_scm option and the editable install works. I guess this should solve the issue (I'm not very familiar with cookiecutter syntax)?:

{% if cookiecutter.use_git_tags_for_versioning == 'y' -%}
[tool.setuptools.dynamic]
version = {attr = "{{cookiecutter.module_name}}.__init__.__version__"}
{%- endif %}

Thanks!