Pyinstaller – SQLAlchemy with SQLServer

huangapple go评论72阅读模式
英文:

Pyinstaller - SQLAlchemy with SQLServer

问题

我有一个命令行脚本,我正在尝试使用pyinstaller构建成一个.exe文件。我正在使用SQLAlchemy来连接数据库。

import sys
from urllib.parse import quote_plus
from sqlalchemy import create_engine

# Press the green button in the gutter to run the script.
username = sys.argv[1]
password = sys.argv[2]
hostname = sys.argv[3]
engine = 'mssql+pytds'
port = sys.argv[4]
technical_name = sys.argv[5]
uri = f"{engine}://{username}:{quote_plus(password)}@{hostname}:{port}/{technical_name}"
pool = create_engine(uri)

我运行了

pyinstaller --onefile main.py

但是当我尝试在dist/文件夹中执行生成的main.exe时,我收到了错误信息:

> sqlalchemy.exc.NoSuchModuleError: 无法加载插件:sqlalchemy.dialects:mssql.pytds

如何为SQLAlchemy包含mssql.pytds?我在我的环境和requirements.txt中安装了sqlalchemy-pytds和python-tds。

编辑:当我简单运行以下代码时,上述代码成功执行:

python main.py user pw 127.0.0.1 1433 dbname

英文:

I have a command line script I'm trying to build to an .exe file using pyinstaller. I'm using SQLAlchemy to connect to a database.

import sys
from urllib.parse import quote_plus
from sqlalchemy import create_engine

# Press the green button in the gutter to run the script.
username = sys.argv[1]
password = sys.argv[2]
hostname = sys.argv[3]
engine = 'mssql+pytds'
port = sys.argv[4]
technical_name = sys.argv[5]
uri = f"{engine}://{username}:{quote_plus(password)}@{hostname}:{port}/{technical_name}"
pool = create_engine(uri)

I run

pyinstaller --onefile main.py

But when I try to execute the resulting main.exe in the dist/ folder, I get the error:

> sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mssql.pytds

How can I include mssql.pytds for sqlalchemy? I have sqlalchemy-pytds and python-tds installed in my environment and in requirements.txt

Edit: The above code executes successfully executes when I simply run

python main.py user pw 127.0.0.1 1433 dbname

答案1

得分: 1

我刚刚弄清楚了。最后,我将所有的导入都明确地添加到Python脚本中。我一次处理一个错误消息,添加它所说缺少的内容。

供后人参考,以下是导入部分的版本,允许我构建和运行程序作为exe:

import sys
from urllib.parse import quote_plus
from sqlalchemy import create_engine
import sqlalchemy_pytds
import sqlalchemy.sql.default_comparator
import sqlalchemy.engine.default
import sqlalchemy
import sqlalchemy_pytds.dialect

我猜想可能有更好的方法来做这件事,但我会在这里分享并使用这个方法,因为它对我有效。如果有任何更"干净"的解决方案/最佳实践,请继续分享,我愿意接受其他答案。

英文:

Just figured it out. I ended up added all imports to the python script explicitly. I worked my way through it one error message at a time, adding whatever it said was missing.

For posterity, the version of the import section that allowed me to build and run the program as an exe:

import sys
from urllib.parse import quote_plus
from sqlalchemy import create_engine
import sqlalchemy_pytds
import sqlalchemy.sql.default_comparator
import sqlalchemy.engine.default
import sqlalchemy
import sqlalchemy_pytds.dialect

Guessing there is a a better way to do this, but sharing here and will use this since it works for me. Would still interested if there are any 'cleaner' solutions/best practices here, and happy to accept another answer.

huangapple
  • 本文由 发表于 2023年6月18日 17:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76499872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定