【问题标题】:PySpark: An error occurred while calling o51.showString. No module named XXXPySpark:调用 o51.showString 时出错。没有名为 XXX 的模块
【发布时间】:2018-07-08 08:38:58
【问题描述】:

我的 pyspark 版本是 2.2.0。我遇到了一个奇怪的问题。我尝试将其简化为以下内容。文件结构:

|root
|-- cast_to_float.py
|-- tests
    |-- test.py

cast_to_float.py,我的代码:

from pyspark.sql.types import FloatType
from pyspark.sql.functions import udf

def cast_to_float(y, column_name):
    return y.withColumn(column_name, y[column_name].cast(FloatType()))

def cast_to_float_1(y, column_name):
    to_float = udf(cast2float1, FloatType())
    return y.withColumn(column_name, to_float(column_name))

def cast2float1(a):
    return 1.0

test.py:

from pyspark.sql import SparkSession
import os
import sys
parentPath = os.path.abspath('..')
if parentPath not in sys.path:
    sys.path.insert(0, parentPath)

from cast_to_float import *
spark = SparkSession.builder.appName("tests").getOrCreate()
df = spark.createDataFrame([
            (1, 1),
            (2, 2),
            (3, 3),
        ], ["ID", "VALUE"])
df1 = cast_to_float(df, 'ID')
df2 = cast_to_float_1(df, 'ID')

df1.show()
df1.printSchema()
df2.printSchema()
df2.show()

然后我在测试文件夹中运行测试,我收到错误消息,它来自最后一行,说:

+---+-----+
| ID|VALUE|
+---+-----+
|1.0|    1|
|2.0|    2|
|3.0|    3|
+---+-----+

root
 |-- ID: float (nullable = true)
 |-- VALUE: long (nullable = true)

root
 |-- ID: float (nullable = true)
 |-- VALUE: long (nullable = true)

    Py4JJavaError                             Traceback (most recent call last)
<ipython-input-4-86eb5df2f917> in <module>()
     19 df1.printSchema()
     20 df2.printSchema()
---> 21 df2.show()
...
Py4JJavaError: An error occurred while calling o257.showString.
...
ModuleNotFoundError: No module named 'cast_to_float'
...

好像cast_to_float被导入了,不然我连df1都拿不到。

如果我把test.py放在cast_to_float.py的同一个目录下,然后在那个目录下运行,就可以了。有任何想法吗?谢谢!


我使用了@user8371915 __file__的方法,发现在root文件夹中运行就可以了。

【问题讨论】:

    标签: python apache-spark pyspark pyspark-sql


    【解决方案1】:

    就目前而言,结果将取决于您调用脚本的工作目录。

    如果您在 root 中,这将添加其父级。您应该使用相对于__file__ 的路径(参见what does the __file__ variable mean/do?):

    parentPath = os.path.join(
        os.path.abspath(os.path.dirname(__file__)), 
        os.path.pardir
    )
    

    但我会建议使用正确的包结构。

    注意

    这仅涵盖本地模式和驱动程序路径,即使在本地模式下,工作程序路径也不受驱动程序路径的影响。

    要处理执行程序路径(更改后您会得到执行程序异常),您仍应将模块分发给工作人员How to use custom classes with Apache Spark (pyspark)?

    spark = SparkSession.builder.appName("tests").getOrCreate()
    spark.sparkContext.addPyFile("/path/to/cast_to_float.py")
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2021-08-05
      • 2021-01-18
      相关资源
      最近更新 更多