我正在尝试运行使用 psycopg2 操作 Redshift 实例的代码。我尝试导入一个wheel文件,因为我发现它们在Glue python作业中受支持。我在运行时看到该库已安装在端点中,但随后出现错误:
import boto3
import psycopg2
Aug 4, 2020, 1:24:06 PM Pending execution
Processing ./glue-python-libs-92ng4pcb/psycopg2-2.8.5-cp36-cp36m-win_amd64.whl
Installing collected packages: psycopg2
Successfully installed psycopg2-2.8.5
Considering file without prefix as a python extra file s3://gluelibraries/boto3.zip
WARNING: The directory '/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
2020-08-04T13:24:44.831+02:00
Traceback (most recent call last):
File "/tmp/runscript.py", line 123, in <module>
runpy.run_path(temp_file_path, run_name='__main__')
File "/usr/local/lib/python3.6/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "/usr/local/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/tmp/glue-python-scripts-1t08aq9n/postloading.py", line 6, in <module>
File "/glue/lib/installation/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ModuleNotFoundError: No module named 'psycopg2._psycopg'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/runscript.py", line 142, in <module>
raise e_type(e_value).with_traceback(new_stack)
File "/tmp/glue-python-scripts-1t08aq9n/postloading.py", line 6, in <module>
File "/glue/lib/installation/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ModuleNotFoundError: No module named 'psycopg2._psycopg'
理论上,Python 中的 Glue 作业(与 pyspark 作业相反)应该支持非纯 Python 库
基于https://stackoverflow.com/a/58305654/4725074
将 psycopg2-binary 安装到目录中并压缩该目录的内容:
mkdir psycopg2-binary
cd psycopg2-binary
pip install psycopg2-binary -t .
# in case using python3:
# python3 -m pip install --system psycopg2-binary -t .
zip -r9 psycopg2.zip *
然后,我将 psycopg2.zip 复制到 S3 存储桶,并将其作为额外的 Python 库添加到 Glue Spark 作业的“Python 库路径”下。
然后,我使用以下脚本启动作业,以验证 psycopg2 是否存在(zip 文件将由 Glue 下载到作业脚本所在的目录中)
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
import sys
import os
import zipfile
## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
zip_ref = zipfile.ZipFile('./psycopg2.zip', 'r')
print(os.listdir('.'))
zip_ref.extractall('/tmp/packages')
zip_ref.close()
sys.path.insert(0, '/tmp/packages')
import psycopg2
print(psycopg2.__version__)
job.commit()
这对我有用。
我在 psycopg2 软件包中也遇到过类似的问题。这与访问 psycopg2 模块的 Python 运行时的兼容性有关。
关注此主题。希望您能找到解决方案。 使用 psycopg2 和 Lambda 来更新 Redshift (Python)
尝试使用 pg8000 代替 psycopg2,它易于安装并且没有 C 依赖项。此外,亚马逊在大多数内部项目中都使用它。
尝试使用带有 Python 端点的 pg8000 后,出现以下错误:
Traceback (most recent call last):
File "/tmp/runscript.py", line 123, in <module>
runpy.run_path(temp_file_path, run_name='__main__')
File "/usr/local/lib/python3.6/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "/usr/local/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/tmp/glue-python-scripts-j7khvbvv/postloading.py", line 7, in <module>
ModuleNotFoundError: No module named 'pg8000'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/runscript.py", line 142, in <module>
raise e_type(e_value).with_traceback(new_stack)
File "/tmp/glue-python-scripts-j7khvbvv/postloading.py", line 7, in <module>
ModuleNotFoundError: No module named 'pg8000'
使用 pyspark 端点时,我的 pg8000 没有这个问题
我从此链接下载了名为 psycopg2-2.9.1-cp36-cp36m-linux_x86_64.whl 的轮子,问题得到了解决。 谢谢
您的 python 版本会发生变化,取决于您使用的 GLue 版本。 在此输入图片描述 此处链接参考:https://docs.aws.amazon.com/glue/latest/dg/release-notes.html
如果您使用的是 GLUE 4.0,则使用的 python 版本是 3.1.0,因此您需要下载与 GLUE 中的 python 版本兼容的 psycopg2 版本。
我建议您在这里下载 de whl https://pypi.org/project/psycopg2-binary/#files
当找到您的glue正在使用的python版本的whl时,下载它并上传到s3路径中以在您的glue中使用。