fpom / snakes

SNAKES is the Net Algebra Kit for Editors and Simulators
https://snakes.ibisc.univ-evry.fr
Other
89 stars 29 forks source link

snakes plugin load from relative path of the project #22

Closed pksec closed 3 years ago

pksec commented 3 years ago

Question - Help Needed

How to correctly import local plugin modules?

Project Folder

. ├── myplugin │   ├── pycache │   │   └── hello1.cpython-37.pyc │   └── hello1.py ├── Pipfile └── snakes-plugin-test.py

snakes-test.py

import snakes.plugins
# snakes.plugins.load(['gv', 'hello'], 'snakes.nets', 'mynet')
from myplugin import hello1
snakes.plugins.load(['gv', 'hello1'], 'snakes.nets', 'mynet')

from mynet import *
n = PetriNet('N')
n.hello()

Output: with standard hello plugin import

> Hello from N

Output: with local hello1 plugin import

I tried couple of imports (relative/absolute), but I couldn't understand where the load function searches for plugin modules "snakes.plugins.load "

Error

 File "snakes-plugin-test.py", line 4, in <module>
    snakes.plugins.load(['gv', 'hello1'], 'snakes.nets', 'mynet')
  File "[...].venv/lib/python3.8/site-packages/snakes/plugins/__init__.py", line 103, in load
    plug = __import__(plug, fromlist=["__name__"])
ModuleNotFoundError: No module named 'snakes.plugins.hello1'

Quick fix

I created a symbolic link of my hello1.py into [...].venv/lib/python3.8/site-packages/snakes/plugins/

ln -rs hello1.py .venv/lib/python3.8/site-packages/snakes/plugins/hello1.py

Now it works, but I assume there must be another way to import locally developed plugins.

fpom commented 3 years ago

Each plugin passed to load may be given as:

Try:

import path.to.hello1 as myplugin
snakes.plugins.load(['gv', myplugin], 'snakes.nets', 'mynets')