This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

– Isolated import of Python modules

Features

  • emulates a partly isolated environment for local modules
  • evaluates *.pth files
  • supports pkg_resources namespaces
  • mocks pkgutil.extend_path() to support zipped Python eggs

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
# 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

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.

import require
localimport = require('./localimport')

with localimport('res/modules') as _importer:
  # ...

Building a minified version

In many cases it doesn’t make much sense to use localimport as yet another Python package, thus you might want to include an inlined and minified version of it into your codebase. The setup.py script allows you to generate a base64 blob and minified version. It is recommended to install pyminifier before using these commands (optional for make_b64, required for make_min).

python setup.py make_b64 --lw=79 --outfile=localimport.b64.py
python setup.py make_min --outfile=localimport.min.py

The --lw and --outfile parameters are optional. If no output file is specified, the result will be printed to stdout.

You can find pre-minified versions here.

Changelog

v1.5

  • add setup.py
  • add make_min and make_b64 commands to setup.py
  • fix possible error when localimport(parent_dir) parameter is not specified and the __file__ of the Python module that uses localimport is in the current working directory

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

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.

Release History

Release History

1.5

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.4.16

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
localimport-1.5.tar.gz (8.9 kB) Copy SHA256 Checksum SHA256 Source Aug 9, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting