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

发布于 2024-10-17 08:46:12 字数 471 浏览 2 评论 0原文

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

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

实际 python 路径(以及 PyQt 库模块的路径),

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

和我需要我正在尝试的

os.environ['PYTHONPATH']

但我不确定它是否可靠。

问候!

附言。我需要它能够插入插件:

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

I need to get Path for PyQt library in python program. Program is run as a script from another application, therefore my

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

and I need my actual python path (and path for PyQt library module)

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

im trying with

os.environ['PYTHONPATH']

but I'm not sure if it can be robust.

Regards!

PS. I need it to be able to plug plugins:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咆哮 2024-10-24 08:46:12

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

例如这样:

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

问候,
哈!

you can try to load the module and after check for it's __file__ attribute to get the path of the .pyc file.

for example like this:

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

Regards,
HTH!

深海不蓝 2024-10-24 08:46:12

您正在寻找的是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']

What you're looking for is 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']
鱼窥荷 2024-10-24 08:46:12

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

import PyQt

PyQt.__file__

A very simple solution:

import PyQt

PyQt.__file__
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文