2017-04-16 63 views
0

我試圖執行下面的代碼,但它沒有運行。錯誤日誌粘貼在下面。此問題可能是由於導入PG引起的,但我不知道如何解決此問題。我該如何解決這個python錯誤? self.stdin.write(輸入)TypeError:寫入()參數必須是str,而不是字節

import pandas as pd 
from sqlalchemy import create_engine, MetaData, Table, select, engine 
from sqlalchemy.orm import sessionmaker 
import datetime 
import cx_Oracle 
import dateutil.relativedelta 
import configparser 
import sys, os 
from boto3.s3.transfer import S3Transfer 
import boto3 
import pg 

try: 
    options = 'keepalives=1 keepalives_idle=200 keepalives_interval=200 keepalives_count=5' 
    connection_string = "host=%s port=%s dbname=%s user=%s password=%s %s" % (
     'somehost', '8192', 'somedb', 'someuser', 'somepassword', options) 
    print(connection_string) 
    con = pg.connect(dbname=connection_string) 
    print('Connected to Redshift.Connection Detail : %s' % con) 

except Exception as e: 
    print('Unable to Connect to Redshift') 
    print(e.message) 
    print('No') 
    sys.exit(1) 



C:\Users\olaa\Desktop\CWB> python .\my.py Traceback (most recent call last): File ".\my.py", line 11, in <module> 
    import pg File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\site-packages\pg\__init__.py", line 1, in <module> 
    from .core import ( File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\site-packages\pg\core.py", line 6, in <module> 
    from . import glfw File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\site-packages\pg\glfw.py", line 138, in <module> 
    ['', '/usr/lib', '/usr/local/lib', 'C:/Users/olaa/AppData/Local/Programs/Python/Python36/Lib/site-packages/pg/'], 
_g lfw_get_version) File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\site-packages\pg\glfw.py", line 74, in _load_library 
    version = version_check_callback(filename) File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\site-packages\pg\glfw.py", line 130, in _glfw_get_versi on 
    out = process.communicate(_to_char_p(filename))[0] File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 836, in communicate 
    stdout, stderr = self._communicate(input, endtime, timeout) File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 1076, in _communicate 
    self._stdin_write(input) File "C:\Users\olaa\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 776, in _stdin_write 
    self.stdin.write(input) TypeError: write() argument must be str, not bytes 

Traceback (most recent call last): 
    File "<string>", line 28, in <module> 
EOFError: EOF when reading a line 
+0

你的代碼簡單地在'import pg'失敗。該模塊從哪裏來? –

+0

我使用PIP安裝它 PS C:\ Users \ olaa \ Desktop \ CWB> pip install pg 要求已經滿足:p:在c:\ users \ olaa \ appdata \ local \ programs \ python \ python36 \ lib \ site -package 要求已滿足:在c:\ users \ olaa \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages中的枕頭 要求已滿足:PyOpenGL在c:\ users \ olaa \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages(從 pg) 要求已經滿足:olefile在c:\ users \ olaa \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages(來自P illow-> pg) –

回答

1
import pg 

這不是一個Postgres庫。這是一個OpenGL graphics library

即使是進口的工作,它會失敗在con = pg.connect( ...

你試圖連接到紅移/ Postgres的,所以儘量實際的Postgres客戶之一

In general, Python users want to use psycopg2 unless they have a strong reason to try another driver

https://wiki.postgresql.org/wiki/Python

+0

謝謝。這對我有效。但是,我仍然想知道爲什麼python通過提供錯誤消息而感到困惑 - 「self.stdin.write(input)TypeError:write()參數必須是str,而不是字節」 –

+0

因爲Python 3中的庫損壞了? –

相關問題