Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
like image 228
Intelligent-Infrastructure Avatar asked Feb 15 '11 11:02

Intelligent-Infrastructure


People also ask

How do I find the path of a Python module?

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.

What is path () in Python?

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.

What does __ file __ mean in Python?

__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.

Is Pythonpath same as SYS path?

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 .


1 Answers

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!

like image 127
SubniC Avatar answered Oct 08 '22 07:10

SubniC