从sqlalchemy import * vs导入sqlalchemy

问题描述 投票:-1回答:1

我在代码中使用sqlalchemy,每次必须使用它的某些功能时,我都需要先导入它,然后再使用它。 import sqlalchemy不能解决我的问题,from sql sqlalchemy import *也不能解决我的代码-

def populateLoaderData(dataFrame):
    try:
        sqlalchemy_conn_string="postgresql://{0}:{1}@{2}:{3}/{4}".format(dbuser,password,endpoint,port,database)
        engine=create_engine(sqlalchemy_conn_string)
        coltype =  {'eccid': String, 
                    'ims_org_id': String,
                    'host_name': String,
                    'packages': JSONB,
                    'environment': String,
                    'topology': String,
                    'topology_type': String,
                    'instance_name': String,
                    'customer_name': String,
                    'build': Integer,
                    'build_version': String,
                    'rds_db': String,
                    'rds_endpoint': String,
                    'hosted_location': String,
                    'tenant_id': String,
                    'apache_version': String,
                    'jdk_version': String,
                    'os_type': String,
                    'provisioning_package': String}
        dataFrame.to_sql('temp_data', con = engine, schema ='loader',dtype=coltype, if_exists = 'replace', index = False, chunksize = 500)
        print("Inserted records in temp_data")
    except:
        print("Error while inserting data into loader table")
        raise
    return

为此,我需要编写三个导入语句

from sqlalchemy import *

from sqlalchemy.types import *

from sqlalchemy.dialects.postgresql import JSONB

如果以上导入语句中的任何一个丢失,都会给我导入错误,我认为这不是一个好的解决方案。有没有一种更好的方法一次性导入所有sqlalchemy库?

python pandas dataframe import sqlalchemy
1个回答
0
投票

存在进口分割的概念是有原因的。它减少了您的依赖链,并且不使用不需要的东西,从而减少了内存使用率,等等。同样,在保持系统清洁的情况下,您不需要编译/安装每个依赖项,因为您的程序不需要这样做。因此,分别导入所有内容都是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.