require() for Python
This Python module provides a new approach to loading Python modules
similar to Node's
require() that is decoupled from the Python import
import require status = require('./lib/status') status.yell()
This is particularly useful in Python applications with a plugin architecture and solves potential problems when using traditional Python modules that can easily result in dependency conflicts.
pip install py-require
- In Python 2, no statement must be on the first line of the file. This is
due to the fact that require prepends the text
from __future__ import absolute_import;in the first first line to avoid RuntimeWarnings when import other modules using Pythons standard
Create a new independent instance. Note that the path argument is processed with
require(file, directory=None, path=(), reload=False, cascade=False, inplace=False, get_exports=True)
Loads a Python module by filename. If file is a relative path starting with
./, it will be loaded relative to directory. Otherwise, if it is not an absolute path, it will be searched in the search path. Note that file should be a UNIX-style path on every platform.
The algorithm will check the following forms of file:
c@x-yis the suffix of bytecode files for the current Python version. If file is the string
'.', it will be translated to
- file -- The name of the Python module to load.
- directory -- The directory to load a local module from. If omitted, will be determined automatically from the caller's global scope using
- path -- A list of additional search paths when loading other modules with
require(). Subsequent loads inherit this search path. Note that these paths are preprocesed with
require.preprocess_path(), thus elements that start with
!(exclamation mark) will be assumed relative to the directory that the
require()function is called from.
- reload -- True to force reload the module.
- cascade -- If reload is True, passing True causes a cascade reload.
- inplace -- If reload is True, modules will be reloaded in-place instead of creating a new module object.
- get_exports -- Return the
exportsmember of the module if there is any. False can be passed to always get the actual module object. Can also be callable that is passed the module object. The result of this callable is returned.
types.ModuleTypeobject, unless the module has a member called
exports, in which case the value of this member will be returned.
require.error-- If the module could not be found or loaded.
require.load_file(load_file, real_file=None, info=None, path=(), reload=False, cascade=False, inplace=False, get_exports=True, cascade_index=None, parent_context=None)
Load a Python module by filename. If real_file is specified, it must be the name of the original source file and is the name under which the module is stored. load_file must be the name of a bytecache file in that case.
The info parameter is passed to
Require.free_module()and must be the same as would be returned by
Class of the
requiremodule that can be instantiated to create a new, decoupled require environment. You can also subclass it and overwrite the
Require.find_module()method.import require require = require.Require() require('./hello').say_hello()
A list of global search directories that will always be taken into account when using
This dictionary maps absolute filenames to the Python modules that are loaded by
- fix loading the
requiremodule without the
importstatement when another instance of the
requiremodule was already imported and present in
require.pathis now taken into account when searching for modules again
require(path)arguments are now preprocessed with the
- filenames are now normalized before using them as module names
Require.find_module()must now return a three-element tuple
(load_file, real_file, info)instead of a two element tuple
- fix NameError where old
bcsuffixvariable was used instead of
- add global
require.pyfor cases when
sys.modulescan not be patched
sys.modulesbeing patched when there is already a module named
requirethat is not the same object as the currently executed
- rewrite, using
Requireclass and cleaner code base
- support for translating
- fix bug with parent context not being inherited when calling the
requiremodule directly instead of using
require.require(), by introducing a new _stackdepth parameter
- fix #9 –
requiremodule is now a custom
types.ModuleTypeinstance that implements
'.'path from default value of
- rename module to
sys.modulehook to allow calling
requireas a module instead of having to use
from require import require
- directory to load local modules from (
./) now falls back to the current working directory
The MIT License (MIT)
Copyright (c) 2016 Niklas Rosenstein
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.