Freezing Python program - Imports are always skipped - python

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.

Related

Python 3.9 Import Works in Linux, Not Windows

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.

Saving a Python File via Atom Results in Module Import Error - Saving from Anywhere Else Causes No Problem

I recently cloned a github repository (https://github.com/lpbsscientist/YeaZ-GUI.git) to begin understanding the code and making edits to it. I have set up a conda virtual environment with all the dependencies required and have been running GUI_main.py successfully via the command line.
In an attempt to gain a better understanding of what the script does, I opened it in Atom and began making edits (not saving them). I inserted several print statements and additional comments. With the path to the virtual environment saved as profile on the Atom script package, I was able to successfully run GUI_main.py from Atom.
So far, so good.
I then saved the current version of GUI_main.py. I tried to run it again, but this time, I got the following error:
File "GUI_main.py", line 55, in <module> import neural_network as nn ModuleNotFoundError: No module named 'neural_network
I got the same error message running the new saved version of GUI_main.py from the command line, atom, and the python 3.6.8 IDE.
I have made no edits to the file structure in GitHub repo. GUI_main.py is located in the directory YeaZ-GUI. This script imports several modules in the subdirectory YeaZ-GUI/unet, and the script appends their paths correctly (as far as I know - see initialization commands below). GUI_main.py imports numerous modules in the following important statement:
#!/usr/bin/env python3
import sys
import numpy as np
import pandas as pd
import h5py
import skimage
# For writing excel files
#from openpyxl import load_workbook
#from openpyxl import Workbook
# Import everything for the Graphical User Interface from the PyQt5 library.
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDialog, QSpinBox,
QMessageBox, QPushButton, QCheckBox, QAction, QStatusBar, QLabel)
from PyQt5 import QtGui
#Import from matplotlib to use it to display the pictures and masks.
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from sklearn.decomposition import PCA
import imageio
from PIL import Image, ImageDraw
#append all the paths where the modules are stored. Such that this script
#looks into all of these folders when importing modules.
sys.path.append("./unet")
sys.path.append("./disk")
sys.path.append("./icons")
sys.path.append("./init")
sys.path.append("./misc")
from PlotCanvas import PlotCanvas
import Extract as extr
from image_loader import load_image
from segment import segment
import neural_network as nn #this is the statement causing the error
Curious as to what was going on, I deleted the current version of GUI_main.py and redownloaded the GUI_main.py script from the Github repo mentioned above and placed it into the same location it was before.
This time, I made the same edits via the python IDE, and I was then able to successfully run the script from the command line with no errors.
However, After opening this edited version of the file in Atom and saving it (making no edits), I got the exact same error message as before - after running from Atom as well as the command line.
Interestingly, repeating this same procedure for the neural_network.py module as well as other modules (i.e editing it, saving it in Atom, and then running it) gives no error message.
It seems like the problem is resulting from the action of saving GUI_main.py on Atom? Could this be possible? Please offer any insight.
So, I did some more experimenting. Turns out that I had downloaded an auto-formatting package (auto pep8) through Atom that was changing the order of my import statements upon saving.
The package's auto-formating moved the import neural_networks as nn to a position before the sys.path.append statements and thus the subdirectory unet where neural_network.py is located was not being searched for importable modules.

After I convert my script to executable file, it gives this error : MatplotlibDeprecationWarning

I have an error which's complete form is
MatplotlibDeprecationWarning: The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3
I converted my script into executable file. After conversion, it never opened. When I execute script with IDEs(VS Code, Linux shell, and Spyder), it works. Somehow it does not open after I converted it to exe. I wrote some lines to avoid from this error, but simply it did not work. For example;
Python/matplotlib : getting rid of matplotlib.mpl warning
I used pyinstaller and auto-py-to-exe to convert my script into exe.
This is the beginning of my code:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QDate, pyqtSlot
from PyQt5.QtGui import QIcon
from datetime import datetime
import calendar
import sys
import numpy as np
import pandas as pd
from scipy.signal import find_peaks
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.dates as mdates
matplotlib.use('Qt5Agg')
myFmt = mdates.DateFormatter('%d')
It is very long to put all the code here. Thank you very much for your interest.
Arda, I've found out after trial and error that pyinstaller has a conflict with the latest version of matplotlib. In order to produce an executable of your script with pyinstaller you should downgrade matplotlib to 3.2.2 version. I have also found some problems with the latest (1.19.4) version of numpy which went away when I downgraded numpy to 1.19.3.
You can check my repository
https://github.com/matiasleoni/COVID19_plotter
where I created a simple script to plot COVID19 global data using matplotlib package. After many failed attempts I was also able to create an executable version of it as I described above (see also the README.md of that repository).

transform python tkinter GUI to EXE file

I have been working on developing a GUI application using tkinter which is 100% working properly when running it from pycharm... and now I can't convert my script into an EXE file.
I have tried pyinstaller, cxfreeze and py2exe but everyone of them has a problem. pyinstaller show me 'Fatal error'... and cxfreeze seems to have a problem with MATPLOTLIB
Here are the libraries I am using:
import numpy as np
import tkinter
from random import randint
from tkinter import *
import tkinter as tk
from math import sqrt
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from math import sqrt
from scipy import stats
from scipy.stats import beta
I don't want to post my code due to confidientality issues...
Does anybody has another idea ot do this ?
EDIT:
I reinstalled python as admin and reinstalled pyintaller
enter image description here
and when I tried to convert the script here is the command error it shows:
'pyinstaller' is not recongnized as an internal command or external ...
also I need to mention that the exe file of pyintaller in not founded in the directory of installation of python in C:programfiles/python37

Py2exe errors when building executable [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Outputed py2exe exe won't run only when signed: ImportError
I asked a similar question previously (Creating executable with Py2exe and matplotlib errors) that dealt with matplotlib errors. However, I have gotten past this stage. Now when I try to build my executable, none of my packages/code seem to import. For example, my code imports the following:
import os
import csv
import wx
import time
import math
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.pyplot import figure,show
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from numpy.random import rand
from datetime import datetime
import wx.calendar as cal
import numpy as npy
from pylab import *
import numpy as np
import matplotlib
import adodbapi
import sqlparse
import pylab
import annote_new
import cPickle as pickle
I get a log error when I run my executable that it "No Module Named os". I get an error for every module I have in my code (if i change the order in which things are imported). Why aren't any of my modules importing? My Py2exe code looks like:
import os
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
import glob
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.pyplot import figure,show
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from numpy.random import rand
from datetime import datetime
import wx.calendar as cal
import numpy as npy
from pylab import *
import numpy as np
import matplotlib
import adodbapi
import sqlparse
import pylab
import annote_new
import cPickle as pickle
import wx
setup(
windows=[{'script': r'Scout_Tool.py'}],
data_files = [(r'mpl-data', glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\*.*')),
(r'mpl-data', [r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),
(r'mpl-data\images',glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl- data\images\*.*')),
(r'mpl-data\fonts',glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl- data\fonts\*.*'))],
#matplotlib.get_py2exe_datafiles(),
options={
'py2exe':{
'includes': [
'matplotlib',
'matplotlib.backends.backend_wx',
'matplotlib.pyplot',
'mpl_toolkits.basemap',
'matplotlib.figure',
'numpy.random',
'wx.calendar',
'mpl_toolkits',
'numpy',
'datetime',
'wx',
'pylab',
'adodbapi',
'sqlparse',
'annote_new',
'cPickle',
'pylab'
],
'dll_excludes': ['MSVCP90.dll'],
}
},
)
Any thoughts on why my modules aren't importing after I run the py2exe? BTW, I get no errors when running the py2exe code -- only when i try to run the produced executable. Thanks!
EDIT
Okay, here is what i have done. I have taken out some of the modules that I weren't using and removed the duplicates. I also fixey my setup.py file to look like:
from distutils.core import setup
import py2exe
import matplotlib
import glob
setup(
windows=[{'script': r'Scout_Tool.py'}],
data_files = matplotlib.get_py2exe_datafiles(),
options={
'py2exe':{
'includes': [
'matplotlib',
'matplotlib.backends.backend_wx',
'matplotlib.pyplot',
'mpl_toolkits.basemap',
'matplotlib.figure',
'wx.calendar',
'mpl_toolkits',
'datetime',
'wx',
'adodbapi',
'sqlparse',
'annote_new',
'cPickle',
'pylab'
],
}
},
)
After this, I cleared out my entire 'dist' folder to be sure anything wasn't kept from before. Then I ran the following in CMD Prompt: C:\Python27\python setup.py py2exe. This ran with no errors.
Then when i go to run Scout_Tool.exe, I first get a MatPlotLib data error. I am not sure why i am getting this, but to fix it, I do the following: I unzip "library.zip", then add the "data" folder from Mpl-toolkits - basemap - data, then re-zip the library folder.
Then, when I try running Scout_Tool.exe, it comes up with the error that "No module named os" exists. This is true if I put any module first in my Scout_Tool.py code.
Hopefully this helps with where i am at? Thanks!
I compiled your program (the imports) and it runs OK for me.
the py2exe missed module report is not relevant if you are not using those modules (I got the same list as that you show).
Remember that the executable will run while you execute it within the dist module py2exe creates (and not from a copy in your desktop, for example. For that you need to make a direct access link).
Not going to take credit for this but do either of these help your problem?
http://www.python-forum.org/pythonforum/viewtopic.php?f=3&t=6659
Outputed py2exe exe won't run only when signed: ImportError
This question is also a continuation of
py2exe doesn't import the os module?

Categories

Resources