使用PyImport_ImportModuleEx为gedit插件导入Python模块



我正在学习Python,并尝试在gedit插件中使用Python Markdown。以下是我的文件的组织方式:

~/.gnome2/gedit/plugins/mytest.gedit-plugin
~/.gnome2/gedit/plugins/mytest/
~/.gnome2/gedit/plugins/mytest/__init__.py
~/.gnome2/gedit/plugins/mytest/markdown/
~/.gnome2/gedit/plugins/mytest/markdown/__init__.py
~/.gnome2/gedit/plugins/mytest/markdown/preprocessors.py
~/.gnome2/gedit/plugins/mytest/markdown/OTHER_FILES
~/.gnome2/gedit/plugins/mytest/markdown/extensions/
~/.gnome2/gedit/plugins/mytest/markdown/extensions/__init__.py
~/.gnome2/gedit/plugins/mytest/markdown/extensions/headerid.py
~/.gnome2/gedit/plugins/mytest/markdown/extensions/OTHER_FILES

说明:

我的文件mytest.gedit-plugin只包含声明插件的最小代码:

[Gedit Plugin]
Loader=python
Module=mytest
IAge=2
Name=My test

我的插件有自己的子文件夹(mytest)。文件mytest/__init__.py包含:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import gedit
import markdown
class MyTestPlugin(gedit.Plugin):
    def __init__(self):
        gedit.Plugin.__init__(self)
    def activate(self, window):
        texte = "# Header 1 {#id}"
        print markdown.markdown(texte, extensions=['headerid'])

最后,文件夹mytest/markdown包含默认的Python Markdown代码。

当我在gedit中激活我的插件时(编辑>首选项>插件),终端中的输出为:

Traceback (most recent call last):
  File "/home/moi/.gnome2/gedit/plugins/mytest/__init__.py", line 5, in <module>
    import markdown
  File "/home/moi/.gnome2/gedit/plugins/mytest/markdown/__init__.py", line 161, in <module>
    import preprocessors
  File "/home/moi/.gnome2/gedit/plugins/mytest/markdown/preprocessors.py", line 11, in <module>
    import markdown
ImportError: No module named markdown
** (gedit:8790): WARNING **: Error loading plugin 'My test'

然而,我成功地在gedit之外使用了Python Markdown。例如,当我在与Python Markdown主文件夹相同位置的终端中运行以下文件时,它的工作效果很好:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import markdown
texte = "# Header 1 {#id}"
print markdown.markdown(texte, extensions=['headerid'])

我发现,如果我将Python Markdown文件中的import markdown更改为import __init__ as markdown,我可以使用没有扩展名的Python Markdown(mytest/markdown/extensions/),但无论如何,它仍然不适用于我的示例:

/home/moi/.gnome2/gedit/plugins/mytest/markdown/__init__.py:114: MarkdownWarning: Failed loading extension 'headerid' from 'markdown.extensions.headerid' or 'mdx_headerid'
  warnings.warn(text, MarkdownWarning)
<h1>Header 1 {#id}</h1>

因此,我的问题是,我如何修改import以进行扩展,或我如何在本地位置安装Python Markdown(在$HOME中,没有root访问权限),以便能够在gedit插件中使用Python Markdown?

非常感谢。

注意:我认为gedit使用PyImport_ImportModuleEx()来加载插件,所以这就是我把它放在问题标题中的原因。


编辑1:2详细信息:没有根安装,可以修改Python Markdown文件。

编辑2:扩展在mytest/markdown/__init__.py中加载以下代码(关于第525行):

# Setup the module names
ext_module = 'markdown.extensions'
module_name_new_style = '.'.join([ext_module, ext_name])
module_name_old_style = '_'.join(['mdx', ext_name])
# Try loading the extention first from one place, then another
try: # New style (markdown.extensons.<extension>)
    module = __import__(module_name_new_style, {}, {}, [ext_module])
except ImportError:
    try: # Old style (mdx.<extension>)
        module = __import__(module_name_old_style)
    except ImportError:
       message(WARN, "Failed loading extension '%s' from '%s' or '%s'"
           % (ext_name, module_name_new_style, module_name_old_style))
       # Return None so we don't try to initiate none-existant extension
       return None

也许有一种方法可以导入相对路径。我真的是Python的初学者。

如果你想在不修改markdown的情况下使用它,那么你必须把它放在Python库期望的地方,比如site-packages/中。否则,您将不得不将其修改为使用相对导入而不是绝对导入。