I was trying to convert a python file into an exe file using pyinstaller but it wouldn't work. I am using matplotlib but the problem doesn't seem to be with it, as I am getting an os error.
I tried "
pyinstaller --onefile pythonScriptName.py
but it din't work(I had followed all steps, and I was using pycharm). It gave
6212 INFO: PyInstaller: 5.3
6212 INFO: Python: 3.10.4
6297 INFO: Platform: Windows-10-10.0.19045-SP0
6428 INFO: wrote C:\Users\manis\PycharmProject\Math\Collatzgui.spec
6567 INFO: UPX is not available.
6714 INFO: Extending PYTHONPATH with paths
['C:\\Users\\manis\\PycharmProject\\Math']
11366 INFO: checking Analysis
11366 INFO: Building Analysis because Analysis-00.toc is non existent
11366 INFO: Initializing module dependency graph...
11466 INFO: Caching module graph hooks...
12215 INFO: Analyzing base_library.zip ...
56140 INFO: Processing pre-find module path hook distutils from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_pa
th\\hook-distutils.py'.
56187 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\manis\\AppData\\Local\\Programs\\Python\\Python310\\lib'
78002 INFO: Caching module dependency graph...
78340 INFO: running Analysis Analysis-00.toc
78503 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:\Users\manis\AppData\Local\Programs\Python\Python310\python.exe
78988 INFO: Analyzing C:\Users\manis\PycharmProject\Math\Collatzgui.py
95005 INFO: Processing pre-find module path hook site from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\h
ook-site.py'.
95036 INFO: site: retargeting to fake-dir 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\fake-modules'
153102 INFO: Processing pre-safe import module hook gi from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module
\\hook-gi.py'.
167390 INFO: Processing pre-safe import module hook six.moves from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import
_module\\hook-six.moves.py'.
180837 INFO: Processing module hooks...
180837 INFO: Loading module hook 'hook-difflib.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
180868 INFO: Loading module hook 'hook-distutils.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
180868 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
180868 INFO: Loading module hook 'hook-encodings.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
181793 INFO: Loading module hook 'hook-heapq.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
181808 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
182426 INFO: Loading module hook 'hook-matplotlib.backends.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
182457 INFO: Matplotlib backend selection method: automatic discovery of used backends
187758 INFO: Trying determine the default backend as first importable candidate from the list: ['Qt5Agg', 'Gtk3Agg', 'TkAgg', 'WxAgg']
195934 INFO: Selected matplotlib backends: ['TkAgg']
197437 INFO: Loading module hook 'hook-matplotlib.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
198956 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
198972 INFO: Loading module hook 'hook-numpy.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
200592 INFO: Import to be excluded not found: 'f2py'
200624 INFO: Loading module hook 'hook-numpy._pytesttester.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
200639 INFO: Loading module hook 'hook-packaging.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
200661 INFO: Loading module hook 'hook-pickle.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
200693 INFO: Loading module hook 'hook-PIL.Image.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
208145 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
208145 INFO: Loading module hook 'hook-PIL.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
208230 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
208245 INFO: Loading module hook 'hook-pkg_resources.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
211407 INFO: Processing pre-safe import module hook win32com from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\p
re_safe_import_module\\hook-win32com.py'.
214986 INFO: Loading module hook 'hook-platform.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
215002 INFO: Loading module hook 'hook-setuptools.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
220694 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
220725 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
223178 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
223216 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
223216 INFO: Loading module hook 'hook-xml.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
223216 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
223965 INFO: checking Tree
223965 INFO: Building Tree because Tree-00.toc is non existent
223965 INFO: Building Tree Tree-00.toc
225824 INFO: checking Tree
225824 INFO: Building Tree because Tree-01.toc is non existent
225824 INFO: Building Tree Tree-01.toc
226426 INFO: checking Tree
226426 INFO: Building Tree because Tree-02.toc is non existent
226426 INFO: Building Tree Tree-02.toc
226488 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
226658 INFO: Looking for ctypes DLLs
226927 INFO: Analyzing run-time hooks ...
226958 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
227105 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_subprocess.py'
227259 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
227412 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
227544 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
227691 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
227908 INFO: Including run-time hook 'c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_mplconfig.py'
228014 INFO: Looking for dynamic libraries
c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\building\build_main.py:157: UserWarning: The numpy.array_api submodule is still experimental. See N
EP 47.
__import__(package)
237490 INFO: Looking for eggs
237490 INFO: Using Python library C:\Users\manis\AppData\Local\Programs\Python\Python310\python310.dll
237490 INFO: Found binding redirects:
[]
237506 INFO: Warnings written to C:\Users\manis\PycharmProject\Math\build\Collatzgui\warn-Collatzgui.txt
237729 INFO: Graph cross-reference written to C:\Users\manis\PycharmProject\Math\build\Collatzgui\xref-Collatzgui.html
238509 INFO: checking PYZ
238509 INFO: Building PYZ because PYZ-00.toc is non existent
238509 INFO: Building PYZ (ZlibArchive) C:\Users\manis\PycharmProject\Math\build\Collatzgui\PYZ-00.pyz
240883 INFO: Building PYZ (ZlibArchive) C:\Users\manis\PycharmProject\Math\build\Collatzgui\PYZ-00.pyz completed successfully.
241015 INFO: checking PKG
241015 INFO: Building PKG because PKG-00.toc is non existent
241015 INFO: Building PKG (CArchive) Collatzgui.pkg
280038 INFO: Building PKG (CArchive) Collatzgui.pkg completed successfully.
280201 INFO: Bootloader c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
280201 INFO: checking EXE
280201 INFO: Building EXE because EXE-00.toc is non existent
280201 INFO: Building EXE from EXE-00.toc
280217 INFO: Copying bootloader EXE to C:\Users\manis\PycharmProject\Math\dist\Collatzgui.exe.notanexecutable
280386 INFO: Copying icon to EXE
280439 INFO: Copying icons from ['c:\\users\\manis\\pycharmproject\\math\\venv\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
280455 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
280455 INFO: Writing RT_ICON 1 resource with 3752 bytes
280455 INFO: Writing RT_ICON 2 resource with 2216 bytes
280455 INFO: Writing RT_ICON 3 resource with 1384 bytes
280455 INFO: Writing RT_ICON 4 resource with 37019 bytes
280455 INFO: Writing RT_ICON 5 resource with 9640 bytes
280455 INFO: Writing RT_ICON 6 resource with 4264 bytes
280455 INFO: Writing RT_ICON 7 resource with 1128 bytes
then it crashed and gave
Traceback (most recent call last):
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 35, in pywin32error
yield
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\win32ctypes\pywin32\win32api.py", line 229, in EndUpdateResource
_resource._EndUpdateResource(handle, discard)
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\win32ctypes\core\ctypes\_util.py", line 64, in check_false
raise make_error(function, function_name)
OSError: [WinError 5] Access is denied.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\manis\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\manis\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\manis\PycharmProject\Math\venv\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\__main__.py", line 178, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\__main__.py", line 59, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\building\build_main.py", line 934, in main
build(specfile, distpath, workpath, clean_build)
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\building\build_main.py", line 856, in build
exec(code, spec_namespace)
File "C:\Users\manis\PycharmProject\Math\Collatzgui.spec", line 24, in <module>
exe = EXE(
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\building\api.py", line 536, in __init__
self.__postinit__()
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\building\datastruct.py", line 173, in __postinit__
self.assemble()
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\building\api.py", line 641, in assemble
icon.CopyIcons(build_name, self.icon)
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\utils\win32\icon.py", line 213, in CopyIcons
return CopyIcons_FromIco(dstpath, [srcpath])
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\PyInstaller\utils\win32\icon.py", line 158, in CopyIcons_FromIco
win32api.EndUpdateResource(hdst, 0)
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\win32ctypes\pywin32\win32api.py", line 228, in EndUpdateResource
with _pywin32error():
File "C:\Users\manis\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "c:\users\manis\pycharmproject\math\venv\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (5, 'EndUpdateResourceW', 'Access is denied.')
It created a file named collatzautogui.exe.notanexecutable. My code is,
import matplotlib.pyplot as plt
# Import the required Libraries
from tkinter import *
from tkinter import ttk
# Create an instance of Tkinter frame
win = Tk()
win.title("Collatz map")
# Set the geometry of Tkinter frame
win.geometry("750x650")
warning = """ Read the following
1) The dots in the sequence are commas
2) Close the graph of a number's collatz before typing another number
3) To get a graph comparing two numbers don't close the graph window"""
label = Label(win, text= warning, font="Lucida 16 italic")
label.pack()
def display_text():
global entry
global string
string = entry.get()
label.configure(text=string)
def collatz_conjecture():
num = float(string)
sequence = [num]
while num != 1:
if num % 2 == 0:
num = num // 2
else:
num = (num * 3) + 1
sequence.append(num)
pr = Text(win)
for x in sequence:
pr.insert(END, x)
pr.pack()
plt.plot(sequence, "r--")
plt.ylabel('Numbers in sequence')
plt.title('Collatz graph!')
plt.show()
# Initialize a Label to display the User Input
label = Label(win, text="", font="Courier 22 bold")
label.pack()
# Create an Entry widget to accept User Input
entry = Entry(win, width=40)
entry.focus_set()
entry.pack()
# Create a Button to validate Entry Widget
ttk.Button(win, text="Okay", width=20, command=lambda:[display_text(),collatz_conjecture()]).pack(pady=20)
win.mainloop()
please help
You need to add an import execution guard to your script.
import matplotlib.pyplot as plt
# Import the required Libraries
from tkinter import *
from tkinter import ttk
# Create an instance of Tkinter frame
win = Tk()
win.title("Collatz map")
# Set the geometry of Tkinter frame
win.geometry("750x650")
warning = """ Read the following
1) The dots in the sequence are commas
2) Close the graph of a number's collatz before typing another number
3) To get a graph comparing two numbers don't close the graph window"""
label = Label(win, text= warning, font="Lucida 16 italic")
label.pack()
def display_text():
global entry
global string
string = entry.get()
label.configure(text=string)
def collatz_conjecture():
num = float(string)
sequence = [num]
while num != 1:
if num % 2 == 0:
num = num // 2
else:
num = (num * 3) + 1
sequence.append(num)
pr = Text(win)
for x in sequence:
pr.insert(END, x)
pr.pack()
plt.plot(sequence, "r--")
plt.ylabel('Numbers in sequence')
plt.title('Collatz graph!')
plt.show()
if __name__ == "__main__": # <---- this is an import execution guard.
# Initialize a Label to display the User Input
label = Label(win, text="", font="Courier 22 bold")
label.pack()
# Create an Entry widget to accept User Input
entry = Entry(win, width=40)
entry.focus_set()
entry.pack()
# Create a Button to validate Entry Widget
ttk.Button(win, text="Okay", width=20, command=lambda:[display_text(),collatz_conjecture()]).pack(pady=20)
win.mainloop()
Then create a new virtual environment: py -m venv venv
Then activate it: venv\scripts\activate
Then install deps and pyinstaller: py -m pip install --upgrade pip pyinstaller matplotlib
Then run: pyinstaller -F pythonScriptName.py
Then you should be able to run: dist\pythonScriptName.exe
And bob's your uncle!