Py2exe Error: Namespace packages not yet supported : Skipping package 'snowflake'

末鹿安然 提交于 2019-12-11 01:33:23

问题


I have been trying to generate an .exe for the following script using py2exe :

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)

The code is pretty straightforward and works like a charm.

I wish to turn it into a .exe with using py2exe but at that moment I got the following error :

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 is a package that allow me to connect to my DB (a snowflake database, rather new)

My os is : Windows 10, Python version 3.5.1

My setup.py for py2exe :

from distutils.core import setup

import snowflake.connector
import py2exe, sys, os


sys.argv.append('py2exe')

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

I looked around and couldn't find any answer to this problem. If anyone have an idea.

Thanks in advance for your help.


回答1:


snowflake is a namespace package and not an actual package. Hence, the importer doesn't reach snowflake.connector. To resolve the package issue, you will need to add __init__.py in the Snowflake directory. Despite this fix, it appears there are other issues in creating an executable with py2exe. I suggest using 'pyinstaller' which works fine in creating an executable.




回答2:


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()

Above code will help you with connecting Snowflake. Use of snowflake.connector package would solve your problem



来源:https://stackoverflow.com/questions/36191922/py2exe-error-namespace-packages-not-yet-supported-skipping-package-snowflake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!