Isolated import of Python Modules
Project description
.. raw:: html
<p align="center">
Isolated import of Python Modules for embedded applications.
.. raw:: html
</p>
.. raw:: html
<h1 align="center">
localimport
.. raw:: html
</h1>
``localimport`` is a Python class that is used as a context manager to
hook into the Python module importer mechanism and ensure a safe and
isolated import of third-party modules. This is especially useful for
embedded Python applications that are packaged with their dependencies
to keep the global importer state clean and to avoid package collisions
between plugins.
Features
========
- Takes ``pkg_resources`` namespaces into account
- Mocks ``pkgutil.extend_path()`` to support zipped Python Eggs
- Emulates a partly isolated environment for local modules
- Evaluates ``*.pth`` files
Example
=======
Given your Python script, application or plugin comes with a directory
that contains modules for import, you can use localimport to keep the
global importer state clean.
::
app.py
res/modules/
some_package/
__init__.py
.. code:: python
# app.py
with localimport('res/modules') as _importer:
import some_package
assert 'some_package' not in sys.modules
**Note**: It is very important that you keep the reference to the
``localimport`` object alive, especially if you use
``from xx import yy`` imports.
Use with `require <https://github.com/NiklasRosenstein/py-require>`__
=====================================================================
The ``localimport`` class is defines as ``exports`` symbols, thus when
you ``require()`` the module, what you get is the class directly rather
then the module.
.. code:: python
import require
localimport = require('./localimport')
with localimport('res/modules') as _importer:
# ...
Changelog
=========
v1.4.16
-------
- fix possible ``KeyError`` when restoring namespace module paths
- renamed ``_localimport`` class to ``localimport``
- ``localimport(parent_dir)`` parameter is now determined dynamically
using ``sys._getframe()``
- support for
`py-require <https://github.com/NiklasRosenstein/py-require>`__
v1.4.14
-------
- Mockup ``pkg_resources.declare_namespace()``, making it call
``pkgutil.extend_path()`` afterwards to ensure we find all available
namespace paths
v1.4.13
-------
- fixed possible KeyError and AttributeError when using the
``_localimport.disable()`` method
v1.4.12
-------
- Removed auto discovering of modules importable from the local site
- Add ``_localimport.disable()`` method
v1.4.11
-------
- Fixed a bug where re-using the ``_localimport`` context added local
modules back to ``sys.modules`` but removed them immediately (#15)
v1.4.10
-------
- Fix #13, ``_extend_path()`` now keeps order of the paths
- Updat class docstrings
- Add ``do_eggs`` and ``do_pth`` parameters to the constructor
- Fix #12, add ``_discover()`` method and automatic disabling of
modules that could conflict with modules from the ``_localimport``
site
v1.4.9
------
- Fix #11, remove ``None``-entries of namespace packages in
``sys.modules``
- ``_localimport._extend_path()`` is is now less tolerant about
extending the namespace path and only does so when a
``__init__.{py,pyc,pyo}`` file exists in the parsed directory
v1.4.8
------
- Now checks any path for being a zipfile rather than just .egg files
License
=======
The MIT License (MIT)
Copyright (c) 2015-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.
<p align="center">
Isolated import of Python Modules for embedded applications.
.. raw:: html
</p>
.. raw:: html
<h1 align="center">
localimport
.. raw:: html
</h1>
``localimport`` is a Python class that is used as a context manager to
hook into the Python module importer mechanism and ensure a safe and
isolated import of third-party modules. This is especially useful for
embedded Python applications that are packaged with their dependencies
to keep the global importer state clean and to avoid package collisions
between plugins.
Features
========
- Takes ``pkg_resources`` namespaces into account
- Mocks ``pkgutil.extend_path()`` to support zipped Python Eggs
- Emulates a partly isolated environment for local modules
- Evaluates ``*.pth`` files
Example
=======
Given your Python script, application or plugin comes with a directory
that contains modules for import, you can use localimport to keep the
global importer state clean.
::
app.py
res/modules/
some_package/
__init__.py
.. code:: python
# app.py
with localimport('res/modules') as _importer:
import some_package
assert 'some_package' not in sys.modules
**Note**: It is very important that you keep the reference to the
``localimport`` object alive, especially if you use
``from xx import yy`` imports.
Use with `require <https://github.com/NiklasRosenstein/py-require>`__
=====================================================================
The ``localimport`` class is defines as ``exports`` symbols, thus when
you ``require()`` the module, what you get is the class directly rather
then the module.
.. code:: python
import require
localimport = require('./localimport')
with localimport('res/modules') as _importer:
# ...
Changelog
=========
v1.4.16
-------
- fix possible ``KeyError`` when restoring namespace module paths
- renamed ``_localimport`` class to ``localimport``
- ``localimport(parent_dir)`` parameter is now determined dynamically
using ``sys._getframe()``
- support for
`py-require <https://github.com/NiklasRosenstein/py-require>`__
v1.4.14
-------
- Mockup ``pkg_resources.declare_namespace()``, making it call
``pkgutil.extend_path()`` afterwards to ensure we find all available
namespace paths
v1.4.13
-------
- fixed possible KeyError and AttributeError when using the
``_localimport.disable()`` method
v1.4.12
-------
- Removed auto discovering of modules importable from the local site
- Add ``_localimport.disable()`` method
v1.4.11
-------
- Fixed a bug where re-using the ``_localimport`` context added local
modules back to ``sys.modules`` but removed them immediately (#15)
v1.4.10
-------
- Fix #13, ``_extend_path()`` now keeps order of the paths
- Updat class docstrings
- Add ``do_eggs`` and ``do_pth`` parameters to the constructor
- Fix #12, add ``_discover()`` method and automatic disabling of
modules that could conflict with modules from the ``_localimport``
site
v1.4.9
------
- Fix #11, remove ``None``-entries of namespace packages in
``sys.modules``
- ``_localimport._extend_path()`` is is now less tolerant about
extending the namespace path and only does so when a
``__init__.{py,pyc,pyo}`` file exists in the parsed directory
v1.4.8
------
- Now checks any path for being a zipfile rather than just .egg files
License
=======
The MIT License (MIT)
Copyright (c) 2015-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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
localimport-1.4.16.tar.gz
(3.8 kB
view hashes)