我用几个方法构建了一个简单的类,以便在使用Python将数据加载到Postgres时使我的生活更轻松一些。我也尝试打包它,以便可以点安装它(只是做实验,以前从未做过)。
import psycopg2
from sqlalchemy import create_engine
import io
class py_psql:
engine = None
def engine(self, username, password, hostname, port, database):
connection = 'postgresql+psycopg2://{}:{}@{}:{}/{}'.format(ntid.lower(), pw, hostname, port, database)
self.engine = create_engine(connection)
def query(self, query):
pg_eng = self.engine
return pd.read_sql_query(query, pg_eng)
def write(self, write_name, df, if_exists='replace', index=False):
mem_size = df.memory_usage().sum()/1024**2
pg_eng = self.engine
def write_data():
df.head(0).to_sql(write_name, pg_eng, if_exists=if_exists,index=index)
conn = pg_eng.raw_connection()
cur = conn.cursor()
output = io.StringIO()
df.to_csv(output, sep='\t', header=False, index=False)
output.seek(0)
contents = output.getvalue()
cur.copy_from(output, write_name, null="")
conn.commit()
if mem_size > 100:
validate_size = input('DataFrame is {}mb, proceed anyway? (y/n): '.format(mem_size))
if validate_size == 'y':
write_data()
else:
print("Canceling write to database")
else:
write_data()
我的软件包目录如下:
py_psql
py_psql.py
__init__.py
setup.py
我的 init .py为空,因为我在其他地方读到我能够做到这一点。我不是这里的专家...
我能够pip安装该软件包并将其导入,如果我将此类粘贴到python shell中,我将能够执行类似
的操作test = py_psql()
test.engine(ntid, pw, hostname, port, database)
并创建sqlalchemy引擎。但是,当我在安装pip之后导入它时,我什至无法初始化py_psql对象:
>>> test = py_psql()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> py_psql.engine(ntid, pw, hostname, port, database)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'py_psql' has no attribute 'engine'
我确定我在这里弄乱了一些明显的东西,但是我发现在研究这个过程时包装过程相当混乱。我在做什么错?
答案 0 :(得分:1)
确定要在安装pip后正确导入软件包吗?
例如:
from py_psql.py_psql import py_psql
test = py_psql()
test.engine(ntid, pw, hostname, port, database)