getnikola / plugins

Extra plugins for Nikola
https://plugins.getnikola.com/
MIT License
56 stars 92 forks source link

[commonmark] AttributeError: module 'commonmark' has no attribute 'Parser' #434

Closed CarstenGrohmann closed 1 week ago

CarstenGrohmann commented 6 months ago

Hi,

After updating my Nikola installation to v8.3.0 the locally installed commonmark plugin fails.

$ nikola --version
[2024-01-26 22:06:19] ERROR: PluginManager: Plugin commonmark from /.../carstengrohmann.de/plugins/commonmark/commonmark.plugin (/.../carstengrohmann.de/plugins/commonmark/commonmark.py) threw an exception while creating the instance
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/nikola/plugin_manager.py", line 215, in load_plugins
    plugin_object = plugin_class()
                    ^^^^^^^^^^^^^^
  File "/.../carstengrohmann.de/plugins/commonmark/commonmark.py", line 57, in __init__
    self.parser = commonmark.Parser()
                  ^^^^^^^^^^^^^^^^^
AttributeError: module 'commonmark' has no attribute 'Parser'
[2024-01-26 22:06:19] WARNING: PluginManager: Some plugins failed to load. Please review the above warning messages.
[2024-01-26 22:06:19] WARNING: PluginManager: You may need to update some plugins (from plugins.getnikola.com) or to fix their .plugin files.
[2024-01-26 22:06:19] WARNING: PluginManager: Waiting 2 seconds before continuing.
Nikola v8.3.0

I can well imagine that with the new Plugin Manager the search path of the modules has changed and the plugin imports itself.

https://github.com/getnikola/plugins/blob/126d38c6e850589d8812f7c5c45121c65ef047b1/v8/commonmark/commonmark.py#L34-L37

This would explain the difference between the following tests with import commonmark && commonmark.Parser, if the command is executed once in the plugin directory and once outside.

$ pwd
/.../carstengrohmann.de/plugins/commonmark

$ ll
insgesamt 24
-rw-r--r-- 1 carsten carsten 1452 19. Okt 22:46 README.md
drwxr-xr-x 2 carsten carsten   40 20. Okt 20:28 __pycache__
-rw-r--r-- 1 carsten carsten  277 19. Okt 22:46 commonmark.plugin
-rw-r--r-- 1 carsten carsten 4184 19. Okt 22:46 commonmark.py
-rw-r--r-- 1 carsten carsten  495 19. Okt 22:46 conf.py.sample
-rw-r--r-- 1 carsten carsten   18 19. Okt 22:46 requirements.txt

$ python
Python 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import commonmark
>>> commonmark.Parser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'commonmark' has no attribute 'Parser'
$ pwd
/.../carstengrohmann.de/plugins

$ ll
insgesamt 4
-rw-r--r-- 1 carsten carsten  25 25. Mai 2014  __init__.py
drwxr-xr-x 3 carsten carsten 134 20. Okt 20:28 commonmark
drwxr-xr-x 3 carsten carsten  18 25. Mai 2014  compile

$ python
Python 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import commonmark
>>> commonmark.Parser
<class 'commonmark.blocks.Parser'>

What do you think about this issue?

Regards, Carsten

asmeurer commented 2 weeks ago

Easy workaround for this,

mv plugins/commonmark/commonmark.py plugins/commonmark/commonmark_plugin.py

and edit plugins/commonmark/commonmark.plugin:

diff --git a/plugins/commonmark/commonmark.plugin b/plugins/commonmark/commonmark.plugin
index 84e28cb..810ca29 100644
--- a/plugins/commonmark/commonmark.plugin
+++ b/plugins/commonmark/commonmark.plugin
@@ -1,6 +1,6 @@
 [Core]
 Name = commonmark
-Module = commonmark
+Module = commonmark_plugin

 [Nikola]
 PluginCategory = PageCompiler
CarstenGrohmann commented 1 week ago

That's a good idea. Let's ask @Kwpolska if he would accept a simple PR with this workaround or if he would like to change the search path handling in the newly introduced plugin manager.

Kwpolska commented 1 week ago

I think changing the plugin names would be a better fix, as it would be more in line with how Python imports work. We’re happy to accept a PR.

CarstenGrohmann commented 1 week ago

@asmeurer: It's your change - would you create a PR for it?

asmeurer commented 1 week ago

I'll see what I can do later but if you already know where to make the change at you should just do it. I'd need to figure out where this code is even located.

CarstenGrohmann commented 1 week ago

ok, I'll provide a PR.