Skip to content

Instantly share code, notes, and snippets.

@gauteh
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauteh/2f8f49f082f1d09b3db0 to your computer and use it in GitHub Desktop.
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)
# 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
@dgerod
Copy link

dgerod commented Feb 8, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment