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

How to get path of a python module ( not sys.executable )

我需要得到python程序中pyqt库的路径。程序作为另一个应用程序的脚本运行,因此

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

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

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

我尝试着

1
os.environ['PYTHONPATH']

但我不确定它是否能健壮。

当做!

我需要它能够插入插件:

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


您可以尝试加载模块,并在检查它的uu file_uuu属性后,获取.pyc文件的路径。

例如:

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

当做,嗯!


你要找的是sys.path

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> 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']