我正在学习 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 ( Edit > Preferences > Plugins )中激活我的插件时,终端中的输出是:
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'])
我发现如果我更改import markdown
Python 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: 2 细节:无需 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 的初学者。