手动导入gtk失败:找不到模块 [英] Manually importing gtk fails: module not found
问题描述
sys.path
搜索各个模块。因此,我的想法是取代
import sys
import gdk.gtk
加上
import sys
import imp
imp.load_source(gtk .gdk,/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py)
(我从 os.path.abspath(gtk .__ file__)
然而,python告诉我这是无效的: module'gtk'not found
。但是,这不正是我正在尝试导入吗?
- 我做错了什么?或 li>
- 有没有更好的方式来实现直接导入?
低)
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk / __ init__.py:23 :
RuntimeWarning:处理绝对导入时未找到父模块'gtk'
导入sys
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py :30:
RuntimeWarning:处理绝对导入时未找到父模块'gtk'
将gobject导入为_gobject
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk /__init__.py:40:
RuntimeWarning:处理绝对导入时未找到父模块'gtk'gtk导入时出现
_gtk
__ init __。pyc
似乎在这里工作,使用 import_module
而不是 import_source
。然而,导入仍然显着缓慢...... #手动完成
文件,文件名,descr = imp .find_module('gtk')
打印文件,文件名,descr
脚本:
#script
gtk = imp.load_module('gtk',FILE,FILENAME,DESCRIPTION)#各个值
#gtk = imp.load_module(gtk,None,/ usr / lib / python2.7 / dist-packages / gtk-2.0 / gtk,('','',5))
从gtk导入gdk
So I would like to execute a python script from command line then and again, and it has to be very quick. Imports in python are slow because the entire sys.path
is searched for the respective modules.
Thus, my idea was to replace
import sys
import gdk.gtk
with
import sys
import imp
imp.load_source("gtk.gdk", "/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py")
(I got that path from os.path.abspath(gtk.__file__)
However, python tells me this is invalid: module 'gtk' not found
. But isn't this exactly what I am trying to import here?
- what am I doing wrong? or
- would there be a better way to achieve a direct import?
(error messages in detail below)
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:23:
RuntimeWarning: Parent module 'gtk' not found while handling absolute import
import sys
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:30:
RuntimeWarning: Parent module 'gtk' not found while handling absolute import
import gobject as _gobject
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40:
RuntimeWarning: Parent module 'gtk' not found while handling absolute import
from gtk import _gtk
Importing the compiled __init__.pyc
seems to work here, using import_module
instead of import_source
. However, the import is still notably slow...
# done manually once
file,filename,descr=imp.find_module('gtk')
print file,filename,descr
script:
# script
gtk=imp.load_module('gtk',FILE,FILENAME,DESCRIPTION) # the respective values
# gtk=imp.load_module("gtk",None,"/usr/lib/python2.7/dist-packages/gtk-2.0/gtk",('','',5))
from gtk import gdk
这篇关于手动导入gtk失败:找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!