2

我一直在尝试使用 py2exe 为以下脚本生成 .exe :

import snowflake.connector
import os
import sys


# Setting your account information
ACCOUNT = '########'
USER = '#######'
PASSWORD = '#######'

ROLES=[]
DATABASE=[]

ROLES.append(sys.argv[1])
DATABASE.append(sys.argv[2])

print(ROLES)
print(DATABASE)

cnx = snowflake.connector.connect(
  user=USER,
  password=PASSWORD,
  account=ACCOUNT,

)


cur = cnx.cursor()
list_of_grants=[]

for rl in ROLES:
    print("Role: "+rl )
    db_res=cur.execute("SHOW DATABASES").fetchall()
    for db in DATABASE:
        print("Database: "+ db)
        use_db_query = "USE DATABASE " + db
        print(use_db_query)
        cur.execute(use_db_query)
        schema_res= cur.execute("SHOW SCHEMAS").fetchall()
        for sch in schema_res:
            print("schema: "+ sch[1])
            list_of_grants.append("GRANT ALL ON SCHEMA "+db+"."+sch[1]+" to role "+rl+" with grant option")
            use_db_query = "USE SCHEMA " + sch[1]
            cur.execute(use_db_query)
            tables_res=cur.execute("SHOW TABLES IN "+sch[1]).fetchall()
            for tbl in tables_res:
                print("table: "+ tbl[1])
                list_of_grants.append("GRANT ALL ON TABLE "+db+"."+sch[1]+"."+tbl[1]+" to role "+rl+" with grant option")
for grant in list_of_grants:
     cur.execute(grant)

代码非常简单,就像一个魅力。

我希望使用 py2exe 将其转换为 .exe,但当时我收到以下错误:

running py2exe
Error: Namespace packages not yet supported: Skipping package 'snowflake'
Traceback (most recent call last):
  File "setup.py", line 13, in <module>
    'packages': ['snowflake']
  File "C:\Anaconda3\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Anaconda3\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Anaconda3\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
    self._run()
  File "C:\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
    builder.analyze()
  File "C:\Anaconda3\lib\site-packages\py2exe\runtime.py", line 168, in analyze
    mf.import_package(modname)
  File "C:\Anaconda3\lib\site-packages\py2exe\mf3.py", line 92, in import_package
    self.import_hook(name)
  File "C:\Anaconda3\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
    module = self._gcd_import(name)
  File "C:\Anaconda3\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
    return self._find_and_load(name)
  File "C:\Anaconda3\lib\site-packages\py2exe\mf3.py", line 337, in _find_and_load
    raise ImportError(name)
ImportError: snowflake

Snowflake 是一个允许我连接到我的数据库的包(一个雪花数据库,相当新)

我的操作系统是:Windows 10,Python 版本 3.5.1

我的 py2exe 的 setup.py :

from distutils.core import setup

import snowflake.connector
import py2exe, sys, os


sys.argv.append('py2exe')

setup(
    console=['grants.py'],
    options = {
        'py2exe': {
            'packages': ['snowflake']
        }
    }
)

我环顾四周,找不到这个问题的任何答案。如果有人有想法。

在此先感谢您的帮助。

4

2 回答 2

1

snowflake是一个命名空间包,而不是一个实际的包。因此,进口商没有达到snowflake.connector. 要解决包问题,您需要__init__.py在 Snowflake 目录中添加。尽管有此修复,但使用 py2exe 创建可执行文件似乎还有其他问题。我建议使用'pyinstaller',它可以很好地创建可执行文件。

于 2016-03-29T18:24:21.873 回答
0
import snowflake.connector

def createSnowflakeConn():
    print("Creating snowflake connection")
    __conn = snowflake.connector.connect(
    user="your login name",
    password="user password",
    account="account id",
    role="login role",
    warehouse="warehouse to query"
)
return __conn

class WarehouseCursor(object):
    def __init__(self):
        self.__cursor = None
        self.__conn = None
        self.__conn = createSnowflakeConn()

上面的代码将帮助您连接雪花。使用 snowflake.connector 包可以解决您的问题

于 2019-10-27T03:17:20.053 回答