【Mac】PySparkでデフォルトのPythonバージョンを3系にする

2021.01.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部の若槻です。

今回は、MacでPySparkのデフォルトのPythonバージョンを3系にする方法を確認してみました。

環境

コンソールはMacのTerminalを利用します。

% sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H15

% echo $SHELL
/bin/zsh

% pyspark --version
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:/usr/local/Cellar/apache-spark/3.0.1/libexec/jars/spark-unsafe_2.12-3.0.1.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of org.apache.spark.unsafe.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 3.0.1
      /_/
                        
Using Scala version 2.12.10, OpenJDK 64-Bit Server VM, 11.0.9
Branch HEAD
Compiled by user ubuntu on 2020-08-28T08:58:35Z
Revision 2b147c4cd50da32fe2b4167f97c8142102a0510d
Url https://gitbox.apache.org/repos/asf/spark.git
Type --help for more information.

結論

下記を実行すればPySparkのデフォルトのPythonバージョンを3系にすることができました。

% echo 'export PYSPARK_PYTHON=python3' >> ~/.zshrc

現状

Python3は導入済みですが、Python自体の既定のバージョンは2系となっています。

% python3 -V
Python 3.8.6
% python -V
Python 2.7.16

PySparkで使われるPythonのバージョンは2系となっています。

% pyspark

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:/usr/local/Cellar/apache-spark/3.0.1/libexec/jars/spark-unsafe_2.12-3.0.1.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of org.apache.spark.unsafe.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
21/01/04 12:25:56 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
/usr/local/Cellar/apache-spark/3.0.1/libexec/python/pyspark/context.py:225: DeprecationWarning: Support for Python 2 and Python 3 prior to version 3.6 is deprecated as of Spark 3.0. See also the plan for dropping Python 2 support at https://spark.apache.org/news/plan-for-dropping-python-2-support.html.
  DeprecationWarning)
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 3.0.1
      /_/

Using Python version 2.7.16 (default, Jun  5 2020 22:59:21)
SparkSession available as 'spark'.

検証

検証1

~/.zshrcに以下を追記して、pythonのエイリアスがPython3になるようにしました。

~/.zshrc

alias python=/usr/local/bin/python3

Python自体の既定のバージョンが3系となりました。

% python -V
Python 3.8.6

PySparkで使われるPythonのバージョンは2系のままです。

% pyspark

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21)

検証2

下記ドキュメントによるとPySparkのPythonバージョンを制御したい場合はPYSPARK_PYTHON環境変数を設定すればよいとのことです。

そこで~/.zshrcに以下を追記しました。検証1で追記したPythonエイリアスの記述は削除しました。

~/.zshrc

#alias python=/usr/local/bin/python3
export PYSPARK_PYTHON=python3

Python自体の既定のバージョンは2系となりました。

% python -V
Python 2.7.16

PySparkで使われるPythonのバージョンは3系となりました。

% pyspark
Python 3.8.6 (default, Oct  8 2020, 14:06:32)

検証3

PYSPARK_PYTHONではPythonエイリアスの指定はできるのか確認してみます。

そこで~/.zshrcに以下を追記しました。検証2で追記したPYSPARK_PYTHONの指定の記述は削除しました。

~/.zshrc

alias python=/usr/local/bin/python3
#export PYSPARK_PYTHON=python3
export PYSPARK_PYTHON=python

Python自体の既定のバージョンは3系となりました。

% python -V
Python 3.8.6

PySparkで使われるPythonのバージョンは2系となりました。

% pyspark

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21)

Pythonエイリアスの指定はPYSPARK_PYTHON環境変数には反映されないようです。

まとめ

  • 既定では、PySparkで使われるPythonは2系となる
  • 環境変数PYSPARK_PYTHON=python3を指定すれば、PySparkで使われるPythonは3系となる

よって、PySparkのデフォルトのPythonバージョンを3系にしたい場合は下記を実行すれば良さそう。

% echo 'export PYSPARK_PYTHON=python3' >> ~/.zshrc

PySparkをPython2系で使っちゃだめなの?

ケースバイケースかと思いますが、私の場合はDataFrameオブジェクトで日本語などのダブルバイト文字を使うとコンソール出力が文字化けしてしまうなどで困りました。下記はPySparkをPython2系で使った場合です。

>>> df = spark.createDataFrame(
  [
    (1, 'しまむら'),
    (2, 'しぶや'),
    (3, 'ほんだ')
  ],
  ['id', 'name']
)
>>> df.show()
+---+------------+                                                              
| id|        name|
+---+------------+
|  1|しまむら|
|  2|   しぶや|
|  3|   ほんだ|
+---+------------+

uでエスケープすると文字化けせずに表示されます。

>>> df = spark.createDataFrame(
  [
    (1, u'しまむら'),
    (2, u'しぶや'),
    (3, u'ほんだ')
  ],
  ['id', 'name']
)
>>> df.show()
+---+--------+
| id|    name|
+---+--------+
|  1|しまむら|
|  2|  しぶや|
|  3|  ほんだ|
+---+--------+

なのでちゃんとPython3系を使うようにしたいですね。

おわりに

MacでPySparkのデフォルトのPythonバージョンを3系にする方法を確認してみました。

PySparkを導入した際はセットで行っておきたい設定ですね。

参考

以上