Skip to content

Instantly share code, notes, and snippets.

@dyng
Created December 5, 2012 12:03
Show Gist options
  • Save dyng/4215016 to your computer and use it in GitHub Desktop.
Save dyng/4215016 to your computer and use it in GitHub Desktop.
通过绝对路径import模块
def import_path(fullpath):
"""
Import a file with full path specification. Allows one to
import from anywhere, something __import__ does not do.
"""
path, filename = os.path.split(fullpath)
filename, ext = os.path.splitext(filename)
sys.path.append(path)
module = __import__(filename)
reload(module) # Might be out of date
del sys.path[-1]
return module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment