Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get module location

Tags:

python

when i import module using

import module_name

is it possible to see where in my hard disk is that module located?

like image 745
Bunny Rabbit Avatar asked Dec 12 '10 09:12

Bunny Rabbit


People also ask

How do I find the location of a module?

You can manually go and check the PYTHONPATH variable contents to find the directories from where these built in modules are being imported. Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built in modules.

Where are modules located in Python?

Usually in /lib/site-packages in your Python folder. (At least, on Windows.) You can use sys. path to find out what directories are searched for modules.

What is __ path __ in Python?

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.

How do I get the path of a PowerShell module?

How can I easily find the path to a Windows PowerShell module? Use the Get-Module cmdlet and a wildcard character for the name, and select the Path property.


1 Answers

It is worth mentioning that packages have __file__ attribute which points to __init__.py, they also have __path__ which points to the package directory. So you can use hasattr(module_name, '__path__') and module_name.__path__[0] or module_name.__file__.

Example (in REPL):

import socket, SOAPpy # SOAPpy is a package
socket.__file__
# .../python2.5/socket.pyc
socket.__path__
# AttributeError: 'module' object has no attribute '__path__'
SOAPpy.__file__
# .../2.5/site-packages/SOAPpy/__init__.pyc
SOAPpy.__path__
# ['.../2.5/site-packages/SOAPpy']
like image 163
khachik Avatar answered Oct 08 '22 12:10

khachik