zero323 commented on a change in pull request #29806: URL: https://github.com/apache/spark/pull/29806#discussion_r494054241
########## File path: python/docs/source/user_guide/python_packaging.rst ########## @@ -0,0 +1,201 @@ +.. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + + +========================= +3rd Party Python Packages +========================= + +When you want to run your PySpark application on a cluster such as YARN, Kubernetes, Mesos, etc., you need to make +sure that the your code and all used libraries are available on the executors. + +As an example let's say you may want to run the `Pandas UDF's examples <arrow_pandas.rst#series-to-scalar>`_. +As it uses pyarrow as an underlying implementation we need to make sure to have pyarrow installed on each executor +on the cluster. Otherwise you may get errors such as ``ModuleNotFoundError: No module named 'pyarrow'``. + +Here is the script ``app.py`` from the previous example that will be executed on the cluster: + +.. code-block:: python + + import pandas as pd + from pyspark.sql.functions import pandas_udf + from pyspark.sql import SparkSession + + def main(spark): + df = spark.createDataFrame( + [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)], + ("id", "v")) + + @pandas_udf("double") + def mean_udf(v: pd.Series) -> float: + return v.mean() + + print(df.groupby("id").agg(mean_udf(df['v'])).collect()) + + + if __name__ == "__main__": + main(SparkSession.builder.getOrCreate()) + + +There are multiple ways to ship the dependencies to the cluster: + +- Using PySpark Native Features +- Using Zipped Virtual Environment +- Using PEX + + +Using PySpark Native Features +----------------------------- + +PySpark allows to upload Python files (``.py``), zipped Python packages (``.zip``), and Egg files (``.egg``) +to the executors by setting the configuration setting ``spark.submit.pyFiles`` or by directly +calling :meth:`pyspark.SparkContext.addPyFile`. + +This is an easy way to ship additional custom Python code to the cluster. You can just add individual files or zip whole +packages and upload them. Using :meth:`pyspark.SparkContext.addPyFile` allows to upload code +even after having started your job. + +Note that it doesn't allow to add packages built as `Wheels <https://www.python.org/dev/peps/pep-0427/>`_ and therefore doesn't +allowing to include dependencies with native code. + + +Using Zipped Virtual Environment +-------------------------------- + +The idea of zipped environments is to zip your whole `virtual environment <https://docs.python.org/3/tutorial/venv.html>`_, +ship it to the cluster, unzip it remotly and target the Python interpreter from inside this zipped environment. Note that this +is currently supported *only for YARN*. + +Zip Virtual Environment Review comment: Could we emphasize here, that these solutions work only on YARN? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org