Im sure there is a simple way to get a modules path from the modulename, right? I.e. I want to retrieve /path/to/module from path.to.module preferably in python 2.7.
I dont intend to import the module and I have the modulename as a string.
Im sure there is a simple way to get a modules path from the modulename, right? I.e. I want to retrieve /path/to/module from path.to.module preferably in python 2.7.
I dont intend to import the module and I have the modulename as a string.
This is fairly easy after importing a module:
import os
print os.__file__
prints
/usr/lib/python2.7/os.pyc
on my machine.
To do this before importing a module, you can use imp.find_module()
:
imp.find_module("os")[1]
sys.modules['path.to.module'].__file__
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> import sys
>>> sys.modules['collections'].__file__
'/usr/lib/python2.7/collections.pyc'
>>>