my python application looks like:
test.py
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtSql
import sys
import atexit
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
db = QtSql.QSqlDatabase.addDatabase('QODBC')
sys.exit(app.exec_())
If i run this application, everything works fine. However, if I create an executable with cx_freeze, I always get the following error:
QSqlDatabase: QODBC driver not loaded
QSqlDatabase: available drivers:
I use the following command to create the executable:
C:\Python27\Scripts\cxfreeze.bat test.py --target-dir C:\Test --include-path="C:\Python27\Lib\site-packages\PyQt4"
If I look into C:\Test (the location where cx_freeze created the executable), I see a bunch of *.dll files with the word 'sql' in it (qsqlodbc4.dll, QtSql4.dll...)
In the past, I created a few PyQT applications with cx_freeze and it always worked really well. However together with the QtSql module, I always get the error message above.
My operating system: Windows 7
Do you guys have any ideas on how to resolve the problem?
edit: Okay, I got it. I copied the contents of PyQt4\plugins\sqldrivers to C:\Test\sqldrivers and now it works.
Kind Regards
Bernhard
This woks only in your developer machine, but when you create a exe file and run this file in other machine it didn't work.
I use pyinstaller, and solve the problem.
only use:
pyinstaller --onefile --windowed myscript.py
Related
I have a python code that I wrote, I want to run this code on other computers, and those computers do not have python installed, nor do they have library that I use in the script.
For example script.py
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
import sys
def main():
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(200,200,300,300)
win.setWindowTitle("My first window!")
label = QLabel(win)
label.setText("my first label")
label.move(50, 50)
win.show()
sys.exit(app.exec_())
main() # make sure to call the function
My solution was to convert my script to an exe file with pyinstaller, but when I try to run the exe file on other computers, it didn't work there.
pyinstaller --onefile -w script.py
script.exe file works for my computer, but on another computer that doesn't have python it doesn't work, and also I cant install PyQt5 library on other computers.
Is there a way to run a python script on computers without python installed?
I keep getting errors when I use python installers (my primary installer contains various libraries such as QSci and other libraries that are part of PyQt5). For example, when I try to convert a simple project with the code below to an executable, it errors out saying ModuleNotFoundError: No module named 'PyQt5.QtPrintSupport'. The projects do compile and run well though. I wonder what is wrong? Thank you in advance!
import sys
import os
import PyQt5
from PyQt5 import QtWidgets, Qsci
from PyQt5 import QtPrintSupport
app = QtWidgets.QApplication(sys.argv)
editor = Qsci.QsciScintilla()
lexer = Qsci.QsciLexerPython(editor)
editor.setLexer(lexer)
## setup autocompletion
api = Qsci.QsciAPIs(lexer)
# import the desired api file
pyqt_path = os.path.dirname(PyQt5.__file__)
api.load(os.path.join(pyqt_path, "Qt/qsci/api/python/Python-3.6.api"))
api.prepare()
editor.setAutoCompletionThreshold(1)
editor.setAutoCompletionSource(Qsci.QsciScintilla.AcsAll)
editor.show()
editor.setText(open(sys.argv[0]).read())
sys.exit(app.exec_())
I'm trying to package a PyQt5 Python app using PyInstaller.
When I package it normally, without using UPX, it works fine. When I start using UPX, though, I start running into a lot of problems. I have to use --upx-exclude "vcruntime140.dll" to keep that file from being corrupted.
Then, I run into this problem.
Traceback (most recent call last):
File "main.py", line 3, in <module>
ImportError: DLL load failed while importing QtWidgets: The parameter is incorrect.
[26400] Failed to execute script main
Here's the beginning of main.py:
import sys
import PyQt5
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
I read here (similar problem, I think) that I might be having trouble with hidden imports, so I run the PyInstaller command with --hidden-import "PyQt5", then with --hidden-import "PyQt5" --hidden-import "QtWidgets". But I keep getting the same error, DLL load failed while importing QTWidgets.
The full PyInstaller command I'm using is:
pyinstaller -n "[exe name]" -i "[icon file path].ico" --upx-dir "[path to UPX]\upx-3.96-win64" --upx-exclude "vcruntime140.dll" --hidden-import "PyQt5" --hidden-import "QtWidgets" --clean main.py
What can I do to fix this error?
I had the exact same problem earlier this week.
I fixed it by disabling UPX entirely. You could go through adding more and more of the QT DLLs to the UPX exclude list to figure out which ones specifically it's corrupting if you still want to compress some of them, but for now I'm ok with just having it disabled entirely.
I tried many things - different versions of PyQT5, different versions of other libraries, none of that had any effect.
If you have a .spec file for folder based build, be sure to disable UPX in both the exe and coll sections.
I'm just starting out writing a program with the PyQt5 gui framework. I have a file, resource_loader.py, which is responsible for loading images etc.
The problem
Python crashes at the line where the image is loaded. I'm using IDLE (3.5) and after I run the program there is no output except for:
=============================== RESTART: Shell ===============================
Code - pretty much copy/pasted from a tutorial, file is called resource_loader.py
from PyQt5 import QtGui
import os
file_image = QtGui.QPixmap("file.png")
Things I've tried
file.png definitely is in the same directory as resource_loader.py
Changing up the variable names, just in case.
Moving the file to a location with no spaces in the path
Extra information
I am running resource_loader.py directly
I am using Linux Mint (18.3) Xfce if that's any use.
Thanks in advance.
It is required to place your code within a QApplication instance as follows:
import sys
import os
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication, QWidget)
app = QApplication(sys.argv)
file_image = QtGui.QPixmap("file.png")
sys.exit(app.exec_())
I have written a python application with using pyttsx library . its working without any issue from tt.py file. I compiled this tt.py file using py2exe module. after compilation i tried to run .exe file. then its shows error like given image
my
setup.py file like this
from distutils.core import setup
import py2exe, sys, os
#includes =['drivers','drivers.sapi5'] #this tried. but making error
sys.argv.append('py2exe')
setup(
options = {'py2exe': {}},
console=['tt.py'],
)
i compiled by this command
python setup.py py2exe install
i am importing following modules in tt.py
import pyttsx
import pyttsx.drivers.sapi5
import win32com
from time import sleep
How can i fix this?
Looking at your imports you need to add import time
If your problem consist use cx freeze it is like py to exe and use a code like this.
It might also be a good idea to upgrade to python 3.3. This may sort some problems.
This might help it is a article on your error. https://mail.python.org/pipermail/python-win32/2006-January/004184.html
import pyttsx
import pyttsx.drivers.sapi5
import win32com
from time import sleep
import sys
from cx_Freeze import setup, Executable
setup(
name = "tt.py",
version = "0.1",
description = "your discription",
executables = [Executable("The file name", base = "Win32GUI")])
here's the link to cx freeze http://cx-freeze.sourceforge.net/
Here's the tutorial on how to use it http://cx-freeze.readthedocs.org/en/latest/overview.html
I tried cx_Freeze for a couple of minutes, but when it didn't work out immediately I tried some more with py2exe and got this working:
from distutils.core import setup
import py2exe
py2exe_options = { 'includes': ['pyttsx.drivers.sapi5', 'win32com.gen_py.C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x4'],
'typelibs': [('{C866CA3A-32F7-11D2-9602-00C04F8EE628}', 0, 5, 4)] }
setup(console=['tt.py'], options = {'py2exe': py2exe_options})
Note though that this requires you to run the same version (v5.4 in my case) on both machines. If you want to circumvent that you probably need to try something more advanced.