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')
We can also use the inspect module in python to locate a module. We will use inspect. getfile() method of inspecting module to get the path. This method will take the module's name as an argument and will return its path.
path module is a very extensively used module that is handy when processing files from different places in the system. It is used for different purposes such as for merging, normalizing and retrieving path names in python . All of these functions accept either only bytes or only string objects as their parameters.
__file__ (A Special variable) in Python A double underscore variable in Python is usually referred to as a dunder. A dunder variable is a variable that Python has defined so that it can use it in a “Special way”. This Special way depends on the variable that is being used.
PYTHONPATH is related to sys. path very closely. PYTHONPATH is an environment variable that you set before running the Python interpreter. PYTHONPATH , if it exists, should contain directories that should be searched for modules when using import .
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With