使用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
,我可以在不使用它的扩展(mytest/markdown/extensions/
)的情况下使用Python Markdown,但无论如何,这样还是不能在我的例子中工作:
/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:两个细节:不需要root安装,并且可以修改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方面真的很初学。
1 个回答
如果你想使用 markdown
而不想去改动它,那么你需要把它放在 Python 库能找到的地方,比如 site-packages/
文件夹里。否则,你就得改一下代码,使用相对导入,而不是绝对导入。