How to Retrieve a Module'S Path

How to retrieve a module's path?

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.

How to get the current running module path/name

This works for me:

__loader__.fullname

Also if I do python -m b.c from a\ I get 'b.c' as expected.

Not entirely sure what the __loader__ attribute is so let me know if this is no good.

edit: It comes from PEP 302: http://www.python.org/dev/peps/pep-0302/

Interesting snippets from the link:

The load_module() method has a few responsibilities that it must
fulfill before it runs any code:

...

  • It should add an __loader__ attribute to the module, set to the
    loader object. This is mostly for introspection, but can be used
    for importer-specific extras, for example getting data associated
    with an importer.

So it looks like it should work fine in all cases.

How do I find the location of Python module sources?

For a pure python module you can find the source by looking at themodule.__file__.
The datetime module, however, is written in C, and therefore datetime.__file__ points to a .so file (there is no datetime.__file__ on Windows), and therefore, you can't see the source.

If you download a python source tarball and extract it, the modules' code can be found in the Modules subdirectory.

For example, if you want to find the datetime code for python 2.6, you can look at

Python-2.6/Modules/datetimemodule.c

You can also find the latest version of this file on github on the web at
https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c

How do you show the path of the current running python module?

If you want a module to know where it's at even when imported, you should be able to use

import sys

sys.modules[__name__].__file__

I know this includes the absolute path on my linux system but I've heard it said that it doesn't on Windows.

How do I import a module given the full path?

For Python 3.5+ use (docs):

import importlib.util
import sys
spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
foo = importlib.util.module_from_spec(spec)
sys.modules["module.name"] = foo
spec.loader.exec_module(foo)
foo.MyClass()

For Python 3.3 and 3.4 use:

from importlib.machinery import SourceFileLoader

foo = SourceFileLoader("module.name", "/path/to/file.py").load_module()
foo.MyClass()

(Although this has been deprecated in Python 3.4.)

For Python 2 use:

import imp

foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()

There are equivalent convenience functions for compiled Python files and DLLs.

See also http://bugs.python.org/issue21436.

Retrieving a modules dotted path name

The __name__ attribute should do it. See the link for the subtle difference between it and __package__ and choose appropriately.

>>> import xml.dom.minidom as m
>>> m.__name__
'xml.dom.minidom'

See PEP 366 for further details.

How to find the path of a module by module name in python on fedora

You can look in sys.modules. For example:

>> import django
>> import sys
>> sys.modules['django']
<module 'django' from '/usr/local/lib/python2.7/dist-packages/django/__init__.pyc'>

Then: if you are constantly running into trouble with module paths [ and need to take control of that ], I'd suggest that you take a look at virtualenv ( http://www.virtualenv.org/en/latest/index.html ). It is the de-facto solution to the "module import hell" problem in the Python world.



Related Topics



Leave a reply



Submit