1

I'm following the plugins packaging guide, using the 'namespace packages' approach. I've written two plugins, with the following structure:

plugin_1
├── pyproject.toml
├── setup.py
└── myapp
    └── plugins
        └── plugin_1.py

plugin_2
├── pyproject.toml
├── setup.py
└── myapp
    └── plugins
        └── plugin_2.py

This works as long the the actual plugins file names are different - pip copies both files (plugin_1.py, plugin_2.py) to venv\Lib\site-packages\myapp\plugins\, where they live side-by-side.

However, if the file names are identical, pip will happily overwrite the first plugin which was installed. Since I do not have control over plugin writers, this could lead to serious problems.

Is there a way to mitigate this, or at least make pip fail in such cases?

sinoroc
  • 18,409
  • 2
  • 39
  • 70
bavaza
  • 10,319
  • 10
  • 64
  • 103
  • The content of the guide linked seems a bit strange to me, surprising. My recommendation would rather be to do something like this `myapp/plugins/plugin1/__init__.py` and `myapp/plugins/plugin2/__init__.py`, where `myapp` and `myapp/plugins` should always be free of any file (only sub-directories, i.e. only sub-packages). – sinoroc Jul 28 '22 at 21:20
  • @sinoroc - tried it, but if the name of the plugin folder is the same (i.e. instead of plugin1, plugin2 you have my_plugin), the __init__.py file inside it gets overwritten. – bavaza Jul 31 '22 at 06:42
  • OK, but why would you do that? Of course pip will overwrite. I really recommend you to follow the pattern I mentioned. Each plugin should have its own dedicated sub-package: `myapp.plugins.plugin1` and `myapp.plugins.plugin2`, and make sure that `myapp` and `myapp.plugins` do not contain any file at all (only subdirectories). This is guaranteed to work. – sinoroc Jul 31 '22 at 09:01
  • 1
    Okay, I re-read your question and I realize I was focusing on the wrong aspect. You are asking how to prevent "bad" plugin authors to write a plugin that clashes with an already existing plugin. My bad... -- In short, I think that it is not possible, we have to assume the community works in respect of each other. https://github.com/pypa/pip/issues/4625 – sinoroc Jul 31 '22 at 09:06
  • Also, as far as I know, this is a problem that is not specific to namespace packages, pip will overwrite any file (I think, I did not check). – sinoroc Jul 31 '22 at 09:13
  • 1
    FYI: There is an open issue about this subject on github: [pip overwrites existing files unconditionally during installation #4625](https://github.com/pypa/pip/issues/4625) – Luuk Jul 31 '22 at 09:21

0 Answers0