13

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')
n611x007
  • 8,952
  • 8
  • 59
  • 102
  • 2
    Check here maybe it can help : http://stackoverflow.com/questions/4693608/find-path-of-module-without-importing-in-python/4693681#4693681 – mouad Feb 15 '11 at 11:49
  • get standard library path, and list of its modules: http://stackoverflow.com/a/6464112/611007 – n611x007 Mar 27 '14 at 19:49

3 Answers3

24

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!

SubniC
  • 9,807
  • 4
  • 26
  • 33
  • this will not work with namespace packages, u will get: `AttributeError: module 'MODULE' has no attribute '__file__'` – lnshi Apr 13 '21 at 09:22
5

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']
mdeous
  • 17,513
  • 7
  • 56
  • 60
0

A very simple solution:

import PyQt

PyQt.__file__
Wojciech Moszczyński
  • 2,893
  • 21
  • 27