forked from holdenk/spark-testing-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
49 lines (40 loc) · 1.4 KB
/
Copy pathutils.py
File metadata and controls
49 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Utils function.
"""
import sys
import os
import logging
from glob import glob
def add_pyspark_path_if_needed():
"""Add PySpark to the library path based on the value of SPARK_HOME if
pyspark is not already in our path"""
try:
from pyspark import context
except ImportError:
# We need to add PySpark, try findspark if we can but it has an
# undeclared IPython dep.
try:
import findspark
findspark.init()
except ImportError:
add_pyspark_path()
def add_pyspark_path():
"""Add PySpark to the library path based on the value of SPARK_HOME."""
try:
spark_home = os.environ['SPARK_HOME']
sys.path.append(os.path.join(spark_home, 'python'))
py4j_src_zip = glob(os.path.join(spark_home, 'python',
'lib', 'py4j-*-src.zip'))
if len(py4j_src_zip) == 0:
raise ValueError('py4j source archive not found in %s'
% os.path.join(spark_home, 'python', 'lib'))
else:
py4j_src_zip = sorted(py4j_src_zip)[::-1]
sys.path.append(py4j_src_zip[0])
except KeyError:
print("""SPARK_HOME was not set. please set it. e.g.
SPARK_HOME='/home/...' ./bin/pyspark [program]""")
exit(-1)
def quiet_py4j():
logger = logging.getLogger('py4j')
logger.setLevel(logging.INFO)