forked from imageio/imageio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageio.proxy.py
52 lines (38 loc) · 1.53 KB
/
imageio.proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
# Copyright (c) 2014, Almar Klein
# (new) BSD License.
"""
This module provides an easy way to enable importing a package event
if it's not on sys.path. The main use case is to import a package
from its developmment repository without having to install it.
This module is sort of like a symlink for Python modules.
To install:
1) Copy this file to a directory that is on the PYTHONPATH
2) Rename the file to "yourpackage.py".
If the real package is in "yourpackage/yourpackage" relative to this file,
you're done. Otherwise modify PARENT_DIR_OF_MODULE.
"""
import os
import sys
# Determine directory and package name
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
MODULE_NAME = __name__
# Override if necessary
PARENT_DIR_OF_MODULE = os.path.join(THIS_DIR, MODULE_NAME)
# Insert in sys.path, so we can import the *real* package
if PARENT_DIR_OF_MODULE not in sys.path:
sys.path.insert(0, PARENT_DIR_OF_MODULE)
# Remove *this* module from sys.modules, so we can import that name again
# (keep reference to avoid premature cleanup)
_old = sys.modules.pop(MODULE_NAME, None)
# Import the *real* package. This will put the new package in
# sys.modules. Note that the new package is injected in the namespace
# from which "import package" is called; we do not need to import *.
try:
__import__(MODULE_NAME, level=0)
except Exception as err:
sys.modules[MODULE_NAME] = _old # Prevent KeyError
raise
# Clean up after ourselves
if PARENT_DIR_OF_MODULE in sys.path:
sys.path.remove(PARENT_DIR_OF_MODULE)