English 中文(简体)
使用PyImport_ImportModuleEx为gedit插件导入Python模块
原标题:Import Python module with PyImport_ImportModuleEx for a gedit plugin

我正在学习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

说明:

我的文件<code>mytest.gdit插件</code>只包含声明插件的最小代码:

[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文件中将<code>import markdown</code>更改为<code>将__init__导入为markdown<-code>,我可以在没有扩展的情况下使用Python markdown(<code>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的初学者。

最佳回答

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

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签