8

when i import module using

import module_name

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

1

4 Answers 4

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']
Sign up to request clarification or add additional context in comments.

3 Comments

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'])
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 you can use inspect as @Nedec suggested.
3

Try: module_name.__file__.

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

Comments

1
import module_name
module_name.__file__

Comments

0

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

import sys print sys.__file__

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.