The following SOF question How to run script in Pyspark and drop into IPython shell when done? tells how to launch a pyspark script:
%run -d myscript.py
But how do we access the existin spark context?
Just creating a new one does not work:
----> sc = SparkContext("local", 1)
ValueError: Cannot run multiple SparkContexts at once; existing
SparkContext(app=PySparkShell, master=local) created by <module> at
/Library/Python/2.7/site-packages/IPython/utils/py3compat.py:204
But trying to use an existing one .. well what existing one?
In [50]: for s in filter(lambda x: 'SparkContext' in repr(x[1]) and len(repr(x[1])) < 150, locals().iteritems()):
print s
('SparkContext', <class 'pyspark.context.SparkContext'>)
i.e. there is no variable for a SparkContext instance
from pyspark import SparkContext?sparkSessionwhich you can create without a clash has asparkContextproperty to access the original context.