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?
Related
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 am working over a project using colour library, using python 3.9 32 bit and pycharm is working, but when I convert it to exe using pyinstaller the exe does not work. To debug I created a simple program, the exe of this program does not work
my system is windows 10 64
converting command
pyinstaller simple_colour.py
import colour
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class mainwindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.label = QLabel("Colour module is loaded")
self.setCentralWidget(self.label)
app = QApplication([])
window = mainwindow()
window.show()
app.exec_()
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 installed PyQt5 with conda this way: conda install -c inso pyqt5=5.6. I have python 3.5. When I run a simple program that uses PyQt5, the program gets stuck on PyQt5 import and there is no error message. It gets stuck for about 5 seconds, then the program just terminates. See the example below that I found on google. The program gets stuck on the line from PyQt5.QtWidgets import QApplication, QWidget. I can see the packcage in C:\Users\user\Anaconda3\Lib\site-packages\PyQt5. What might be causing suck a behavior?
"""
ZetCode PyQt5 tutorial
In this example, we create a simple
window in PyQt5.
author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
First comment here solved my problem. DLL load failed when importing PyQt5
I uninstalled pyqt5 and reinstalled wtih conda install --channel https://conda.anaconda.org/bpentz pyqt5
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