title: Numpy error in printing a RDD in Spark with Ipython
body: <p>I am trying to print a <code>RDD</code> using <code>Spark</code> in <code>Ipython</code> and when I do that I get this error:</p>
  
  <pre><code>---------------------------------------------------------------------------
  Py4JJavaError                             Traceback (most recent call last)
  &lt;ipython-input-4-77015cd18335&gt; in &lt;module&gt;()
  
  ---&gt; 24 print inputData.collect()
       25 
       26 
  
  /home/vagrant/spark-1.5.0-bin-hadoop2.6/python/pyspark/rdd.pyc in collect(self)
      771         """
      772         with SCCallSiteSync(self.context) as css:
  --&gt; 773             port = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
      774         return list(_load_from_socket(port, self._jrdd_deserializer))
      775 
  
  /home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/py4j-0.8.2.1-src.zip/py4j/java_gateway.py in __call__(self, *args)
      536         answer = self.gateway_client.send_command(command)
      537         return_value = get_return_value(answer, self.gateway_client,
  --&gt; 538                 self.target_id, self.name)
      539 
      540         for temp_arg in temp_args:
  
  /home/vagrant/spark-1.5.0-bin-hadoop2.6/python/pyspark/sql/utils.pyc in deco(*a, **kw)
       34     def deco(*a, **kw):
       35         try:
  ---&gt; 36             return f(*a, **kw)
       37         except py4j.protocol.Py4JJavaError as e:
       38             s = e.java_exception.toString()
  
  /home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/py4j-0.8.2.1-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
      298                 raise Py4JJavaError(
      299                     'An error occurred while calling {0}{1}{2}.\n'.
  --&gt; 300                     format(target_id, '.', name), value)
      301             else:
      302                 raise Py4JError(
  
  Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
  : org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 7.0 failed 1 times, most recent failure: Lost task 0.0 in stage 7.0 (TID 56, localhost): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/worker.py", line 98, in main
      command = pickleSer._read_with_length(infile)
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/serializers.py", line 164, in _read_with_length
      return self.loads(obj)
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/serializers.py", line 421, in loads
      return pickle.loads(obj)
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/mllib/__init__.py", line 25, in &lt;module&gt;
  ImportError: No module named numpy
  
      at org.apache.spark.api.python.PythonRDD$$anon$1.read(PythonRDD.scala:138)
      at org.apache.spark.api.python.PythonRDD$$anon$1.&lt;init&gt;(PythonRDD.scala:179)
      at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:97)
      at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:297)
      at org.apache.spark.rdd.RDD.iterator(RDD.scala:264)
      at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
      at org.apache.spark.scheduler.Task.run(Task.scala:88)
      at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  
  Driver stacktrace:
      at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1280)
      at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1268)
      at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1267)
      at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
      at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
      at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1267)
      at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:697)
      at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:697)
      at scala.Option.foreach(Option.scala:236)
      at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:697)
      at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1493)
      at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1455)
      at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1444)
      at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)
      at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:567)
      at org.apache.spark.SparkContext.runJob(SparkContext.scala:1813)
      at org.apache.spark.SparkContext.runJob(SparkContext.scala:1826)
      at org.apache.spark.SparkContext.runJob(SparkContext.scala:1839)
      at org.apache.spark.SparkContext.runJob(SparkContext.scala:1910)
      at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:905)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:147)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:108)
      at org.apache.spark.rdd.RDD.withScope(RDD.scala:306)
      at org.apache.spark.rdd.RDD.collect(RDD.scala:904)
      at org.apache.spark.api.python.PythonRDD$.collectAndServe(PythonRDD.scala:373)
      at org.apache.spark.api.python.PythonRDD.collectAndServe(PythonRDD.scala)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
      at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:379)
      at py4j.Gateway.invoke(Gateway.java:259)
      at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
      at py4j.commands.CallCommand.execute(CallCommand.java:79)
      at py4j.GatewayConnection.run(GatewayConnection.java:207)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/worker.py", line 98, in main
      command = pickleSer._read_with_length(infile)
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/serializers.py", line 164, in _read_with_length
      return self.loads(obj)
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/serializers.py", line 421, in loads
      return pickle.loads(obj)
    File "/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/mllib/__init__.py", line 25, in &lt;module&gt;
  ImportError: No module named numpy
  
      at org.apache.spark.api.python.PythonRDD$$anon$1.read(PythonRDD.scala:138)
      at org.apache.spark.api.python.PythonRDD$$anon$1.&lt;init&gt;(PythonRDD.scala:179)
      at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:97)
      at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:297)
      at org.apache.spark.rdd.RDD.iterator(RDD.scala:264)
      at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
      at org.apache.spark.scheduler.Task.run(Task.scala:88)
      at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      ... 1 more
  </code></pre>
  
  <p>My current code is: </p>
  
  <pre><code>from pyspark.mllib.regression import LabeledPoint
  from pyspark.mllib.tree import DecisionTree, DecisionTreeModel
  from pyspark.mllib.util import MLUtils
  import os.path
  import numpy as np
  
  print np.version.version
  
  def extract(line):
      return (line[1])
  
  inputPath = os.path.join('file1.csv')
  fileName = os.path.join(inputPath)
  Data = sc.textFile(fileName).zipWithIndex().filter(lambda (line,rownum): rownum&gt;0).map(lambda (line, rownum): line)
  
  
  inputData = (Data
      .map(lambda line: line.split(";"))
      .filter(lambda line: len(line) &gt;1 )
      .map(extract)) # Map to tuples
  
  # error comes a this line
  print inputData.collect()
  </code></pre>
  
  <p>I have <code>numpy</code> already installed (<code>sudo apt-get install python-numpy</code>) and can print <code>numpy version</code> in Ipython using <code>numpy.version.version</code></p>
  
  <p>Why is this error coming and how to resolve it?</p>
  
  <p><strong>NOTE 1:</strong> My current bash_profile:</p>
  
  <pre><code># Set the Spark Home as an environment variable.
  export SPARK_HOME="$HOME/spark-1.5.0-bin-hadoop2.6"
  
  # Define your Spark arguments for when running Spark.
  export PYSPARK_SUBMIT_ARGS="--master local[2]"
  
  # IPython alias for the use with SPARK.
  alias IPYSPARK='PYSPARK_DRIVER_PYTHON=ipython PYSPARK_DRIVER_PYTHON_OPTS="notebook --profile=pyspark --ip=0.0.0.0" $SPARK_HOME/bin/pyspark'
  </code></pre>
  
  <p>I have also added following to my <code>spark-env.sh.template</code> file:</p>
  
  <pre><code>#!/usr/bin/env bash
  
  # This file is sourced when running various Spark programs.
  export PYSPARK_PYTHON=/usr/bin/python2.7       
  export PYSPARK_DRIVER_PYTHON=/usr/bin/ipython
  </code></pre>
  
  <p><strong>NOTE 2:</strong> I am launching the Ipython notebook from inside a virtual environment.</p>
  
  <p><strong>NOTE 3:</strong> <code>I have Spark 1.5.0</code> and <code>numpy 1.8.2</code> </p>
  
  <p><strong>UPDATE:</strong> output from <code>sc.parallelize([],1).mapPartitions(lambda _: [(sys.executable, sys.path)]).first()</code></p>
  
  <pre><code>('/home/vagrant/pyEnv/bin/python2.7', ['', u'/tmp/spark-dbbcfd0b-413e-4406-8bd5-37de29d3fcc5/userFiles-6296ba2d-4ec5-4956-9904-828bda0c6424', '/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/pyspark.zip', '/home/vagrant/spark-1.5.0-bin-hadoop2.6/python/lib/py4j-0.8.2.1-src.zip', '/home/vagrant/spark-1.5.0-bin-hadoop2.6/lib/spark-assembly-1.5.0-hadoop2.6.0.jar', '/home/vagrant/spark-1.5.0-bin-hadoop2.6/python', '/home/vagrant', '/home/vagrant/pyEnv/lib/python2.7', '/home/vagrant/pyEnv/lib/python2.7/plat-x86_64-linux-gnu', '/home/vagrant/pyEnv/lib/python2.7/lib-tk', '/home/vagrant/pyEnv/lib/python2.7/lib-old', '/home/vagrant/pyEnv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/vagrant/pyEnv/local/lib/python2.7/site-packages', '/home/vagrant/pyEnv/lib/python2.7/site-packages'])
  </code></pre>