Last active
August 29, 2015 14:13
-
-
Save gauteh/2f8f49f082f1d09b3db0 to your computer and use it in GitHub Desktop.
add %mrun and %addpath magics that searches pythonpath and allows adding custom paths to an %run like command (%mrun)
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
# based on ipython issue #101 | |
# | |
# author: Gaute Hope <gaute.hope@nersc.no> / 2015 | |
# | |
# references: | |
# | |
# https://github.com/ipython/ipython/issues/101 | |
# http://ipython.org/ipython-doc/dev/config/custommagics.html | |
# | |
import os | |
import sys | |
from IPython.utils.path import filefind | |
_custom_paths = [] | |
def find_in_pythonpath (filename): | |
global _custom_paths | |
try: | |
path_dirs = ['.'] + _custom_paths + os.environ['PYTHONPATH'].split(':') | |
except: | |
path_dirs = ['.'] + _custom_paths | |
return filefind(filename, path_dirs) | |
def find_in_syspath(filename): | |
return filefind(filename, sys.path) | |
from IPython.core.magic import line_magic, Magics, magics_class, register_line_magic | |
@register_line_magic | |
def mrun (line): | |
ip = get_ipython() | |
ip.magics_manager.magics['line']['run'](line, file_finder=find_in_pythonpath) | |
del mrun | |
@register_line_magic | |
def addpath (line): | |
global _custom_paths | |
_custom_paths.append(line) | |
del addpath | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I was looking for, thanks a lot!
I have taken your code and done some small modifications, you could see it in https://gist.github.com/dgerod/549273a7bf5114d008b6