I am writing an app that needs to work both in Windows and in Linux. I have been developing it in Linux and am just now working on the details to make it work on both platforms. The issue I am running into is with regards to importing other python files from another directory. The app, which runs without issue in Linux (Ubuntu 18.04), has the following folder structure:
- data
- images
- lib
---- configTools.py
---- dbTools.py
---- osciloscopeTools.py
---- peakFindingTools.py
---- poissonTools.py
---- plottingTools.py
---- spacingTools.py
- src
---- config.py
---- interface.py
The program begins with running interface.py. The beginning of the code that I have at the moment is written as follows:
import pandas as pd
import os
import sys
sys.path.append('../lib')
#sys.path.append(os.path.realpath('../lib'))
import platform
import subprocess as subprocess
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QFileDialog, QLabel, QCheckBox, QLineEdit, QGridLayout
from PyQt5.QtGui import QIcon, QPixmap
#from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import QtCore, QtWidgets
import matplotlib
import time
import sqlite3
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
#import os
import matplotlib.pyplot as plt
import math
import numpy as np
import imageio
import csv
# Import local files
import configTools as ct
import dbTools as dbt
import oscilloscopeTools as ot
import peakFindingTools as pft
import poissonTools as pt
import config
import plottingTools as pltools
The issue when running in Windows (Visual Studio 2019) is the block at the bottom where I attempt to import my other python files. An exception is thrown saying "No module named 'configTools'". All of the files in that block are similarly underlined in the IDE, saying "unresolved import ___" where ___ is the filename of each. My best guess is that the line executing "sys.path.append('../lib')" is not accomplishing the desired effects in Windows. Permanently altering environment variables is not a good option for me since this app needs to be portable once it's done. It works fine in Linux, so I am under the impression that this is valid Python, but I cannot find the correct way to accomplish the task at hand in Windows. Further, I really do not want to put everything in the same folder; even if that would be the easiest thing for the python files, the data and images folders will populate with hundreds of files over the course of execution, and I suspect that I will run into similar problems when trying to read and write from those folders.
Related
I created a GUI with python and now I want to create an exe file, which can be used on every computer which has not python installed.Im using macOS ...
Is this possible?
The following modules I used:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from PyQt5.QtGui import QIntValidator
import pandas
import folium
import numpy as np
import networkx
I've been trying for a few days to freeze a Python program. I've tried py2app, PyInstaller and cx_freeze. Of all 3, cx_freeze seemed to be the most effective.
py2app and PyInstaller seems to skip all my imports whichever settings I'm using. The modules needed are always found in "Modules not found (unconditional imports)" with py2app. With PyInstaller, everything seems to run fine, but the program fails to launch due to the fact that no modules is imported, even when specified as hidden import.
For cx_freeze, some imports seems to be done, as it is at least trying to import PyQt5.
But cx_freeze failed at this step with this error:
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/QtCore.framework/Versions/5/QtCore'
For information:
Python version: 3.7.2, installed with Anaconda3.
List of imports:
import sys
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from PyQt5 import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
OS used: MacOS 10.11.16
Targeted OS for freezing: MacOS (various versions)
At this point, I don't really know what to do to freeze my programe. If anyone has an idea, that would be awesome.
I keep struggling with importing the pyqtgraph module. It's really only about the import:
pip3 install pyqtgraph is not working.
My code:
from PyQt5 import QtGui
import PyQt5
import numpy as np
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
import pyqtgraph as pg
import time
leaves:
<ipython-input-37-14dc06d126e4> in <module>
31 import numpy as np
32 from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
---> 33 import pyqtgraph as pg
34 import time
35 import sys
ModuleNotFoundError: No module named 'pyqtgraph'
I am guessing that your module gets installed to another Python version than what PYTHONPATH points to.
Try pip3 list -v | grep "pyqtgraph" on Unix-like OSs, or pip3 list -v | findstr "pyqtgraph" on Windows.
The directory location that will be shown will be different to your PYTHONPATH. Set your location to PYTHON3.7 dir and it should solve your problem. See here on how you can do that.
The following code is the module I used in my pyqt widget, but after I use pyinstaller to build exe file, the file size is 233MB large.
How can I reduce the file size?
I tried to create a new virtual environment, but there is no improvement, and I also tried to add excludes=['mkl','whl'] in my pyinstaller spec file, but no improve as well.
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication, QFileDialog
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import Qt, pyqtSignal, QEvent
import sys
from glob import glob
from numpy import array as nparray
from PIL.Image import open as imopen
from win32gui import GetWindowText, GetForegroundWindow
from MainWindow import Ui_MainWindow, resource_path
from qimage2ndarray import array2qimage
from shutil import move
from os import makedirs, chdir, getcwd
from os import path as ospath
MainWindow is the UI code I build with qtdesigner, the module it use is :
from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QStatusBar
from PyQt5.QtCore import QRect, Qt, QSize, QMetaObject, QCoreApplication
from PyQt5.QtGui import QFont, QIcon, QPixmap
import sys
from os.path import join, abspath
For my app I was able to reduce size from 80mb to 15mb
Here is what I did :-
First Use pyinstaller in one directory mode
The created folder has a lot of garbage and unrequired stuff so first delete the DLL files which you know you have not used in application.
Then for rest of files use hit-and-trial method, delete one file and see if app is still working properly, if not then restore that file and try with some other file.
After you are done, use UPX (in lzma mode) on all .pyd files.
Most of DLLs can also be compressed with UPX except some (like VCRUNTIME.dll), so you will have to do hit and trail for DLL files too.
Finally you can compress any images (if any) on your app.
Done (◠‿◕)
Finally I reduce my exe file size from 233MB to 64MB by solution provided here :
Create a new conda environment pyinstaller-env
install numpy with conda install conda-forge::numpy "blas=*=openblas"
Switch environment to pyinstaller-env and package my QT application.
I'm developping a small graphic application using Python 3 and PyQt5.
On the first computer I use, where only PyQt5 is installed, everything in my code is fine. But when I want to run my code on my other laptop, where both PyQt4 and PyQt5 are installed, I get the following error:
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
Python interpreter locates the error in the file "ViewWindow.py", called from the main file.
As I have both PyQt4 and PyQt5 on this laptop, and because I can't uninstall PyQt4 (it would be too easy...), I wonder if it's possible to force use of PyQt5.QtCore, or something else to avoid this problem.
My configuration on this laptop: Debian 8, Python3.4, PyQt4 and 5 (without special configuration, installed from Debian repos), IDE = Spyder.
I put there first lines of my files main.py and ViewWindow.py.
# main.py
import sys
import sqlite3
import ViewWindow
from DataWindow import DataWindow
from PyQt5.QtCore import QObject # I tried adding this line, but nothing changed...
from PyQt5.QtWidgets import (QApplication,
QWidget,
QGridLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QTextEdit,
QVBoxLayout
)
class MainWindow(QWidget):
# Some cool stuff
# ViewWindow.py
import sys
import sqlite3
from PyQt5.QtCore import QObject # same thing than above, adding this line doesn't change the output.
from PyQt5.QtWidgets import (QApplication,
QWidget,
QGridLayout,
QLabel,
QPushButton,
QVBoxLayout
)
class ViewWindow(QWidget):
Does someone knows how to make this code run ?
Thanks,
Jerry M.
Edit: I tried to run that script forcing use of Python3, and it worked... It seems that problem comes from iPython3.
Thanks for your help.
A RuntimeError with message
the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
is raised the moment you try to import PyQt5.QtCore while PyQt4.QtCore was already imported before.
This error is raised within SIP, which is used to connect to Qt. Like it states, it's only allowed to have one module claiming to wrap QObject. Thus the error just tells you, that you're using PyQt4 and PyQt5 at once.
So you need to find the module loading PyQt4 to configure it to use PyQt5 instead. Alternatively you could try to put from PyQt5.QtCore import QObject before any other import and hope, that the module, which usually imports from PyQt4, is adaptable and able to use PyQt5 as fallback.