Created
October 8, 2015 23:10
-
-
Save adamheins/6ea490795618776e8412 to your computer and use it in GitHub Desktop.
Python function for importing modules from hidden directories and files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imp | |
import os | |
def secret_import(path): | |
""" Alternative way to import a module that allows importing of hidden | |
directories and files. """ | |
if os.path.isdir(path): | |
name = path.split(os.path.sep)[-1] | |
if name[0] == '.': | |
name = name[1:] | |
path = os.path.join(path, '__init__.py') | |
else: | |
name = path.split(os.path.sep)[-1].split('.')[-2] | |
if name == '__init__': | |
name = path.split(os.path.sep)[-2] | |
if name[0] == '.': | |
name = name[1:] | |
with open(path, 'rb') as fp: | |
return imp.load_module(name, fp, path, ('.py', 'rb', imp.PY_SOURCE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment