fs.expose.importhook

Expose an FS object to the python import machinery, via a PEP-302 loader.

This module allows you to import python modules from an arbitrary FS object, by placing FS urls on sys.path and/or inserting objects into sys.meta_path.

The main class in this module is FSImportHook, which is a PEP-302-compliant module finder and loader. If you place an instance of FSImportHook on sys.meta_path, you will be able to import modules from the exposed filesystem:

>>> from fs.memoryfs import MemoryFS
>>> m = MemoryFS()
>>> m.setcontents("helloworld.py","print 'hello world!'")
>>>
>>> import sys
>>> from fs.expose.importhook import FSImportHook
>>> sys.meta_path.append(FSImportHook(m))
>>> import helloworld
hello world!

It is also possible to install FSImportHook as an import path handler. This allows you to place filesystem URLs on sys.path and have them automagically opened for importing. This example would allow modules to be imported from an SFTP server:

>>> from fs.expose.importhook import FSImportHook
>>> FSImportHook.install()
>>> sys.path.append("sftp://some.remote.machine/mypath/")
class fs.expose.importhook.FSImportHook(fs_or_url)

PEP-302-compliant module finder and loader for FS objects.

FSImportHook is a module finder and loader that takes its data from an arbitrary FS object. The FS must have .py or .pyc files stored in the standard module structure.

For easy use with sys.path, FSImportHook will also accept a filesystem URL, which is automatically opened using fs.opener.

find_module(fullname, path=None)

Find the FS loader for the given module.

This object is always its own loader, so this really just checks whether it’s a valid module on the exposed filesystem.

get_code(fullname, info=None)

Get the bytecode for the specified module.

get_data(path)

Read the specified data file.

get_filename(fullname, info=None)

Get the __file__ attribute for the specified module.

get_source(fullname, info=None)

Get the sourcecode for the specified module, if present.

classmethod install()

Install this class into the import machinery.

This classmethod installs the custom FSImportHook class into the import machinery of the running process, if it is not already installed.

is_package(fullname, info=None)

Check whether the specified module is a package.

load_module(fullname)

Load the specified module.

This method locates the file for the specified module, loads and executes it and returns the created module object.

classmethod uninstall()

Uninstall this class from the import machinery.

This classmethod uninstalls the custom FSImportHook class from the import machinery of the running process.