Skip to content
Snippets Groups Projects
Commit 2d98fff0 authored by Josh Rosen's avatar Josh Rosen
Browse files

Add IPython support to pyspark-shell.

Suggested by / based on code from @MLnick
parent 1dca0c51
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,9 @@ interacting with Java processes. It can be installed from ...@@ -38,6 +38,9 @@ interacting with Java processes. It can be installed from
https://github.com/bartdag/py4j; make sure to install a version that https://github.com/bartdag/py4j; make sure to install a version that
contains at least the commits through b7924aabe9. contains at least the commits through b7924aabe9.
PySpark requires the `argparse` module, which is included in Python 2.7
and is is available for Python 2.6 through `pip` or `easy_install`.
PySpark uses the `PYTHONPATH` environment variable to search for Python PySpark uses the `PYTHONPATH` environment variable to search for Python
classes; Py4J should be on this path, along with any libraries used by classes; Py4J should be on this path, along with any libraries used by
PySpark programs. `PYTHONPATH` will be automatically shipped to worker PySpark programs. `PYTHONPATH` will be automatically shipped to worker
......
""" """
An interactive shell. An interactive shell.
""" """
import argparse # argparse is avaiable for Python < 2.7 through easy_install.
import code import code
import sys import sys
from pyspark.context import SparkContext from pyspark.context import SparkContext
def main(master='local'): def main(master='local', ipython=False):
sc = SparkContext(master, 'PySparkShell') sc = SparkContext(master, 'PySparkShell')
print "Spark context available as sc." user_ns = {'sc' : sc}
code.interact(local={'sc': sc}) banner = "Spark context avaiable as sc."
if ipython:
import IPython
IPython.embed(user_ns=user_ns, banner2=banner)
else:
print banner
code.interact(local=user_ns)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: parser = argparse.ArgumentParser()
master = sys.argv[1] parser.add_argument("master", help="Spark master host (default='local')",
else: nargs='?', type=str, default="local")
master = 'local' parser.add_argument("-i", "--ipython", help="Run IPython shell",
main(master) action="store_true")
args = parser.parse_args()
main(args.master, args.ipython)
...@@ -4,3 +4,4 @@ ...@@ -4,3 +4,4 @@
# install Py4J from git once https://github.com/pypa/pip/pull/526 is merged. # install Py4J from git once https://github.com/pypa/pip/pull/526 is merged.
# git+git://github.com/bartdag/py4j.git@b7924aabe9c5e63f0a4d8bbd17019534c7ec014e # git+git://github.com/bartdag/py4j.git@b7924aabe9c5e63f0a4d8bbd17019534c7ec014e
argparse
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment