Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a module's path?

I want to detect whether module has changed. Now, using inotify is simple, you just need to know the directory you want to get notifications from.

How do I retrieve a module's path in python?

like image 796
Cheery Avatar asked Oct 29 '08 17:10

Cheery


People also ask

How do I get the Python Package path?

Packages support one more special attribute, __path__ . This is initialized to be a list containing the name of the directory holding the package's __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.

Where are Python libraries saved?

Usually the Python library is located in the site-packages folder within the Python install directory, however, if it is not located in the site-packages folder and you are uncertain where it is installed, here is a Python sample to locate Python modules installed on your computer.

Where are module files stored?

Location of Modules package main directory. Default value is /usr/share/Modules .

What is __ path __ in Python?

If you change __path__ , you can force the interpreter to look in a different directory for modules belonging to that package. This would allow you to, e.g., load different versions of the same module based on runtime conditions.


1 Answers

import a_module print(a_module.__file__) 

Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:

import os path = os.path.abspath(a_module.__file__) 

You can also try:

path = os.path.dirname(a_module.__file__) 

To get the module's directory.

like image 199
orestis Avatar answered Sep 19 '22 14:09

orestis