Plugin system, RuntimeWarning: Parent module 'ext_abc' not found while handling absolute import

T

Torsten Mohr

Hi,

i try to write a plugin system that i want to use to let users extend
a module that i write.

Within the module there is an extension loader that loads an extension
module. This extension module should be able to import modules from
my system, which provides some extensions.

Basically, this here works but gives a warning:
RuntimeWarning: Parent module 'ext_abc' not found while handling absolute import

Below are the files, i wonder what is wrong.

It would be great if anybody could give me a hint, what provokes that
warning?


Best regards,
Torsten.



FILE psys.py:
import mymodule.ext_loader
import sys
import os.path

here = os.path.abspath('.')
mpath = os.path.abspath('mymodule')
epath = os.path.abspath('extension')

sys.path.append(here)
sys.path.append(mpath)

mymodule.ext_loader.load('ext_abc')


FILE mymodule/__init__.py:
__all__ = ['base', 'ext_loader']


FILE mymodule/ext_loader.py:
import imp
import os.path


def search_file(fname):
for e in ['extension']:
candidate = os.path.join(os.path.expanduser(e), fname)

if os.path.exists(candidate):
return candidate

return None


def load(modname):
fname = modname + ".py"
abname = search_file(fname)
fd = open(abname, "rb")
mod = imp.load_module(fname, fd, abname, ['py', 'r', imp.PY_SOURCE])
fd.close()


FILE mymodule/base.py:
def func1():
print "func1 called !"


FILE extension/ext_abc.py:
import base

base.func1()
 
G

Gabriel Genellina

i try to write a plugin system that i want to use to let users extend
a module that i write.

Within the module there is an extension loader that loads an extension
module. This extension module should be able to import modules from
my system, which provides some extensions.

Basically, this here works but gives a warning:
RuntimeWarning: Parent module 'ext_abc' not found while handling
absolute import

here = os.path.abspath('.')

(Unrelated to the main question, but you probably want to use
os.path.dirname(os.path.abspath(__file__)) instead - the above just
returns the current directory, which might not be the directory containing
the module)
mpath = os.path.abspath('mymodule')
epath = os.path.abspath('extension')

sys.path.append(here)
sys.path.append(mpath)

FILE mymodule/__init__.py:

So mymodule is actually a package. Packages should *not* appear in
sys.path.
 
T

Torsten Mohr

Hello,
(Unrelated to the main question, but you probably want to use
os.path.dirname(os.path.abspath(__file__)) instead - the above just
returns the current directory, which might not be the directory containing
the module)

Thanks, i'll try that.
So mymodule is actually a package. Packages should *not* appear in
sys.path.

Oh, how does it find modules then? I thought that would be PYTHONPATH or
sys.path ?


Best regards,
Torsten.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top