Skip to content

@fccoelho /test_virtualenvmagic.py forked from minrk/shebang.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Prototype of an extension to provide acess to virtual python environment on an IPython notebook cell
# -*- coding: utf-8 -*-
"""
Tests for the virtualenvmagic extension
Author: Flávio Codeço Coelho - @fccoelho
"""
import os
import nose.tools as nt
def setup():
ip = get_ipython()
ip.extension_manager.load_extension('virtualenvmagic')
def test_virtualenv():
ip = get_ipython()
result = ip.run_cell_magic('virtualenv','pypyEnv','import sys;print sys.version')
nt.assert_true('PyPy' in result)
# -*- coding: utf-8 -*-
"""
Virtualenv magic
Requires virtualenv and virtualenv Wrapper
Author: Flávio Codeço Coelho - @fccoelho
Thanks to @turicas for helpful tips
"""
import sys
import os
import shlex
from subprocess import Popen, PIPE
from IPython.core.magic import cell_magic
@cell_magic('virtualenv')
def virtualenv(line, cell):
"""
This magic enables the execution of code in a cell on a
pre-existing virtual env. It Requires Virtualenv and VirtualenvWrapper
to be installed in the system.
The virtual env to be used must be created in advance.
Usage
=====
To activate this magic just write at the top of the cell:
%%virtualenv my_env
import sys
print sys.version
"""
if not os.path.exists(os.environ['WORKON_HOME']+'/'+line):
print >> sys.stderr, "Environment {} does no exist.".format(line)
return
env_activate_cmd = 'bash -c "source {}/{}/bin/activate && python -"'.format(os.environ['WORKON_HOME'],line)
cmd = shlex.split(env_activate_cmd)
p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
out,err = p.communicate(cell)
p.wait()
if err:
print >> sys.stderr, err
return out
_loaded = False
def load_ipython_extension(ip):
"""Load the extension in IPython."""
global _loaded
if not _loaded:
if 'WORKON_HOME' in os.environ:
ip.register_magic_function(virtualenv, 'cell')
_loaded = True
else:
print >> sys.stderr, "You must have Virtualenv and VirtualenvWrapper installed."
@fccoelho
Owner

@minrk : Would this be a start of a virtualenv cell?

it requires virtualenv to be installed and the env to exist prior to using this

@takluyver

Rather than calling get_ipython() or InteractiveShell(), an IPython extension should offer a load_ipython_extension(ip) method, which will be called with the shell object when it's loaded. Docs: http://ipython.org/ipython-doc/stable/config/extensions/index.html

(The rationale for this is that we have a long term goal to allow more than one InteractiveShell object in the same process - so we're trying not to treat it like a singleton)

@fccoelho
Owner

I'll study other extensions and modify my code to conform.
thanks @takluyver

@fccoelho
Owner

Thanks @turicas for the tip on how to make the bash command work...

@turicas

@fccoelho, PEP8, please! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.