如何获取python模块的路径(不是sys.executable)

时间:2011-02-15 11:46:36

标签: python path pyqt qwebkit

我需要在python程序中获取PyQt库的Path。程序作为另一个应用程序的脚本运行,因此我的

sys.executable = 'D:/program files/visum/exe/visum115.exe

我需要我的实际python路径(以及PyQt库模块的路径)

Path = C:\Python25\Lib\site-packages\PyQt4\plugins

我正在尝试

os.environ['PYTHONPATH']

但我不确定它是不是很健壮。

问候!

PS。我需要它能够插入插件:

qApp.addLibaryPath('C:\Python25\Lib\site-packages\PyQt4\plugins')

3 个答案:

答案 0 :(得分:17)

您可以尝试加载模块并检查其 __ 文件 __ 属性后获取.pyc文件的路径。

例如:

import MODULE, os
path = os.path.dirname(MODULE.__file__)

此致 HTH!

答案 1 :(得分:5)

您要找的是sys.path

>>> import sys
>>> sys.path
 ['',
 '/usr/lib/python2.7/site-packages/virtualenvwrapper.github-0.1-py2.7.egg',
 '/usr/lib/python2.7/site-packages/pycharm-debug.egg',
 '/usr/lib/python2.7/site-packages/Fom-0.9.2-py2.7.egg',
 '/usr/lib/python2.7/site-packages/blinker-1.1-py2.7.egg',
 '/usr/lib/python2.7/site-packages/httplib2-0.6.0-py2.7.egg',
 '/usr/lib/python27.zip',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/site-packages',
 '/usr/lib/python2.7/site-packages/Numeric',
 '/usr/lib/python2.7/site-packages/PIL',
 '/usr/lib/python2.7/site-packages/gst-0.10',
 '/usr/lib/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages/setuptools-0.6c11.egg-info',
 '/usr/lib/python2.7/site-packages/wx-2.8-gtk2-unicode']

答案 2 :(得分:0)

一个非常简单的解决方案:

import PyQt

PyQt.__file__