8

when i import module using

import module_name

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

Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
  • Duplicate of: http://stackoverflow.com/questions/269795/how-do-i-find-the-location-of-python-module-sources – Hugo Sep 24 '12 at 14:25

4 Answers4

11

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']
khachik
  • 28,112
  • 9
  • 59
  • 94
  • Do you have evidence? I just tried it on a package of my own, `__file__` is there (`package/__init__.pyc`), as it should be, seeing as the "package" import is just a module called `__init__`. (`package.__path__ == ['package']`) – Chris Morgan Dec 12 '10 at 10:06
  • what about functions? can there location be also determined (well the other option seems to be to just check path of the module from which it is being imported and hope that there is not mangling in between. – Bunny Rabbit Dec 12 '10 at 10:21
  • @Bunny you can use inspect as @Nedec suggested. – khachik Dec 12 '10 at 10:25
3

Try: module_name.__file__.

If you want to 'inspect' an object, use: inspect

Nedec
  • 836
  • 10
  • 15
1
import module_name
module_name.__file__
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
0

You can use module's file attribute to find the location.

import sys print sys.__file__

Bilal Mahmood
  • 169
  • 1
  • 3