python 导入错误:没有名为psycopg2.extensions的模块在Cloud DataFlow上运行代码时

dnph8jn4  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(203)

我正在尝试从云shell运行我的云数据流代码我正在使用以下代码

from __future__ import absolute_import
import argparse
import logging
import apache_beam as beam
from apache_beam.io import ReadFromText
from apache_beam.io import WriteToText
from apache_beam.options.pipeline_options import PipelineOptions
import csv
import psycopg2
from io import StringIO
import argparse
import logging
import sys
import re
import csv
conn = psycopg2.connect("dbname='db_bio' user='postgres' host='*****' port='5432' password='poui19956'") 

class scrip_val(beam.DoFn):
    def process(self, element):
        f = StringIO(element)
        print(type(f))
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            cur.execute("insert into bio values(%s,%s,%s,%s,%s)",row)
            conn.commit()
        return [len(element)]
def run():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input',
                        dest='input',
                        default='gs://pydataflow',
                        help='Input file to process.')
   
    known_args, pipeline_args = parser.parse_known_args()
    
    with beam.Pipeline(options=PipelineOptions(pipeline_args)) as p:
        lines = p | 'read' >> ReadFromText(known_args.input)
        (lines
        | 'words'>> beam.ParDo(scrip_val())
        )
if __name__ == '__main__':
    logging.getLogger().setLevel(logging.INFO)
    run()`

但当我尝试使用以下命令从云shell运行此代码时

python -m  bulksumlog --input [Bucket_location]  --runner DataflowRunner --project [Project_id] --temp_locat
ion [temo_Bucket_location] --save_main_session True

出现以下错误

return dill.load_session(file_path)
  File "/usr/local/lib/python2.7/dist-packages/dill/_dill.py", line 402, in load_session
    module = unpickler.load()
  File "/usr/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1096, in load_global
    klass = self.find_class(module, name)
  File "/usr/local/lib/python2.7/dist-packages/dill/_dill.py", line 465, in find_class
    return StockUnpickler.find_class(self, module, name)
  File "/usr/lib/python2.7/pickle.py", line 1130, in find_class
    __import__(module)
ImportError: No module named psycopg2.extensions

我安装了所有与psycopg2相关的python库

cbeh67ev

cbeh67ev1#

我们应该用psycopg 2-binary运行。在我的例子中,我安装了psycopg 2-binary(使用pip),仍然出现这个错误。我在Fernando Munoz的答案here中找到了帮助。我的步骤是:a)pip卸载psycopg 2,B)pip卸载psycopg 2-二进制,c)sudo apt安装build-dep python-psycopg 2(在Debian上的先决条件),d)pip安装psycopg 2-二进制

相关问题