Jak skompilować aplikację do pliku wykonywalnego .exe?

0

Witam Szanownych użytkowników.

Napisałem program wykorzystujący bibliotekę import pyautogui. Pod PyCharm wszystko śmiga bez żadnych kłopotów, jednak kiedy kompiluje plik *.exe owszem otrzymuje skompilowany exec ale po jego uruchomieniu w towarzystwie niezbędnych plików, katalogów otrzymuje krytyczny błąd, załączam zrzut ekranu....
ScreenH0300.jpg

Kilka słów o środowisku:
win11, python 3.11,
PyCharm 2023.1.2 (Community Edition)
Build #PC-231.9011.38, built on May 16, 2023
Runtime version: 17.0.6+10-b829.9 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1514M
Cores: 4
ScreenH0301.jpg

W moim programie chce zrzucić ekran do pliku graficznego...przykładowy program poniżej, u mnie na pytonie działa, exec zgłasza błąd krytyczny
W internecine nie doszukałem się żadnego rozwiązania tematu, za wszelką pomoc dziękuje...

Pozdrawiam J

P.S.
Program który generuje błąd krytyczny to program okienkowy pod Windows, tu załączyłem przykładowy program na konsole, w przypadku tego programu pod pytonem śmiga, generuje katalog oraz plik.png w tym katalogu, uruchomiony z exe nie generuje katalogu, nie generuje plik.png.
Auto Py to Exe:

ScreenH0302.jpg
Wszystkie poprzednie wersje programu, które nie wykorzystują import pyautogui, działają bez zarzutu czy z exec czy pod python.

Program testowy:

import os
import pyautogui
from datetime import datetime

def take_screenshot():
    # Pobranie dzisiejszej daty
    today = datetime.now().strftime("%d%m%Y")

   # Sprawdzenie istnienia katalogu "history" i jego utworzenie, jeśli nie istnieje
    directory = "history"
    if not os.path.exists(directory):
        os.makedirs(directory)

    # Początkowa wartość liczby
    number = 1

    # Generowanie nazwy pliku
    filename = f"{directory}/A{number}_{today}.png"

    # Sprawdzenie, czy plik istnieje i aktualizacja liczby w nazwie, jeśli tak
    while os.path.exists(filename):
        number += 1
        filename = f"{directory}/A{number}_{today}.png"

    # Zrzut ekranu całego ekranu
    screenshot = pyautogui.screenshot()
    screenshot.save(filename)

# Wywołanie procedury
take_screenshot()    
2

zgaduje ze pyinstaller nie zaimportował bibliotek, spróbuj użyć

--hidden-import=pyautogui
0
DarekRepos napisał(a):

zgaduje ze pyinstaller nie zaimportował bibliotek, spróbuj użyć

--hidden-import=pyautogui

Witam, mam taka linie aktywacji:

auto-py-to-exe: pyinstaller --noconfirm --onefile --windowed --hidden-import=pyautogui "C:/Users/linex/Documents/Jan/bin/hookmemory006.py"

niestety mam ten sam objaw, z pythona działa absolutnie sprawnie bez żadnych problemów, uruchomienie z .exe file jest zakończone błędem krytycznym, jak w temacie

2

Z ciekawości odpaliłem ten przykładowy kod który podałeś.
Odpaliłem konsole. Wszedłem do folderu projektu. zainstalowalem pyinstaler oraz pyautogui bibliotekę
W konsoli wpisałem pyinstaller --noconfirm --onefile --windowed --hidden-import=pyautogui i zrobił się exe . Jak kilkam na ten exe to screenshot się robi w folderze history. Natomiast jako że próbowałem odtworzyć ten błąd to zrobiłem to na Windowsie (bo zazwyczaj używam linux na wsl, bo jest wygodniejszy) to po drodze cały czas musiałem odblokowywać plik w antywirusie bo mi automatycznie blokował wszystko. Więc może spróbuj wyłączyć na chwile antywirusa albo wyłączyć opcje automatycznej izolacji tylko na czas robienia skryptu. Nic innego mi do głowy nie przychodzi. Mam nadzieje, że to jednak pomoże.

0
DarekRepos napisał(a):

Z ciekawości odpaliłem ten przykładowy kod który podałeś.
Odpaliłem konsole. Wszedłem do folderu projektu. zainstalowalem pyinstaler oraz pyautogui bibliotekę
W konsoli wpisałem pyinstaller --noconfirm --onefile --windowed --hidden-import=pyautogui i zrobił się exe . Jak kilkam na ten exe to screenshot się robi w folderze history. Natomiast jako że próbowałem odtworzyć ten błąd to zrobiłem to na Windowsie (bo zazwyczaj używam linux na wsl, bo jest wygodniejszy) to po drodze cały czas musiałem odblokowywać plik w antywirusie bo mi automatycznie blokował wszystko. Więc może spróbuj wyłączyć na chwile antywirusa albo wyłączyć opcje automatycznej izolacji tylko na czas robienia skryptu. Nic innego mi do głowy nie przychodzi. Mam nadzieje, że to jednak pomoże.

Załączam output z auto-py-to-exe, może tutaj cos można zauważyć

Running auto-py-to-exe v2.36.0
Building directory: C:\Users\linex\AppData\Local\Temp\tmplqdfksx0
Provided command: pyinstaller --noconfirm --onefile --windowed --hidden-import=pyautogui "C:/Users/linex/Documents/Jan/bin/hookmemory006.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onefile --windowed --hidden-import=pyautogui C:/Users/linex/Documents/Jan/bin/hookmemory006.py --distpath C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\application --workpath C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\build --specpath C:\Users\linex\AppData\Local\Temp\tmplqdfksx0

53011 INFO: PyInstaller: 5.12.0
53027 INFO: Python: 3.11.4
53065 INFO: Platform: Windows-10-10.0.22000-SP0
53078 INFO: wrote C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\hookmemory006.spec
53110 INFO: UPX is not available.
53126 INFO: Extending PYTHONPATH with paths
['C:\\Users\\linex\\Documents\\Jan\\bin']
54000 INFO: checking Analysis
54003 INFO: Building Analysis because Analysis-00.toc is non existent
54018 INFO: Initializing module dependency graph...
54036 INFO: Caching module graph hooks...
54090 INFO: Analyzing base_library.zip ...
57720 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
58243 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
60984 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
63523 INFO: Caching module dependency graph...
63706 INFO: running Analysis Analysis-00.toc
63732 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\linex\AppData\Local\Programs\Python\Python311\python.exe
64049 INFO: Analyzing C:\Users\linex\Documents\Jan\bin\hookmemory006.py
64414 INFO: Loading module hook 'hook-platform.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
64708 INFO: Loading module hook 'hook-PIL.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
64859 INFO: Loading module hook 'hook-PIL.Image.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
66017 INFO: Loading module hook 'hook-pycparser.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
66690 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
67410 INFO: Loading module hook 'hook-distutils.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
67493 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
67553 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
68133 INFO: Loading module hook 'hook-setuptools.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
71521 INFO: Loading module hook 'hook-pkg_resources.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
73235 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
75167 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
76343 INFO: Loading module hook 'hook-numpy.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
78864 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
81420 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
81993 INFO: Processing module hooks...
82426 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
82618 INFO: Processing pre-safe import module hook win32com from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
84328 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
86070 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks'...
86075 INFO: checking Tree
86091 INFO: Building Tree because Tree-00.toc is non existent
86095 INFO: Building Tree Tree-00.toc
86247 INFO: checking Tree
86262 INFO: Building Tree because Tree-01.toc is non existent
86278 INFO: Building Tree Tree-01.toc
86300 INFO: checking Tree
86303 INFO: Building Tree because Tree-02.toc is non existent
86324 INFO: Building Tree Tree-02.toc
86402 INFO: Looking for ctypes DLLs
86606 INFO: Analyzing run-time hooks ...
86630 INFO: Including run-time hook 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
86643 INFO: Including run-time hook 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
86661 INFO: Including run-time hook 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
86679 INFO: Including run-time hook 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_setuptools.py'
86684 INFO: Including run-time hook 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
86714 INFO: Including run-time hook 'C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
86765 INFO: Looking for dynamic libraries
88804 INFO: Looking for eggs
88822 INFO: Using Python library C:\Users\linex\AppData\Local\Programs\Python\Python311\python311.dll
88828 INFO: Found binding redirects: 
[]
88855 INFO: Warnings written to C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\build\hookmemory006\warn-hookmemory006.txt
88980 INFO: Graph cross-reference written to C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\build\hookmemory006\xref-hookmemory006.html
89070 INFO: checking PYZ
89074 INFO: Building PYZ because PYZ-00.toc is non existent
89095 INFO: Building PYZ (ZlibArchive) C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\build\hookmemory006\PYZ-00.pyz
90668 INFO: Building PYZ (ZlibArchive) C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\build\hookmemory006\PYZ-00.pyz completed successfully.
90764 INFO: checking PKG
90768 INFO: Building PKG because PKG-00.toc is non existent
90774 INFO: Building PKG (CArchive) hookmemory006.pkg
95325 INFO: Building PKG (CArchive) hookmemory006.pkg completed successfully.
95371 INFO: Bootloader C:\Users\linex\Documents\venv\Lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
95389 INFO: checking EXE
95404 INFO: Building EXE because EXE-00.toc is non existent
95420 INFO: Building EXE from EXE-00.toc
95424 INFO: Copying bootloader EXE to C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\application\hookmemory006.exe.notanexecutable
95444 INFO: Copying icon to EXE
95453 INFO: Copying icons from ['C:\\Users\\linex\\Documents\\venv\\Lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-windowed.ico']
95468 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
95474 INFO: Writing RT_ICON 1 resource with 3752 bytes
95481 INFO: Writing RT_ICON 2 resource with 2216 bytes
95497 INFO: Writing RT_ICON 3 resource with 1384 bytes
95500 INFO: Writing RT_ICON 4 resource with 38188 bytes
95513 INFO: Writing RT_ICON 5 resource with 9640 bytes
95528 INFO: Writing RT_ICON 6 resource with 4264 bytes
95544 INFO: Writing RT_ICON 7 resource with 1128 bytes
95565 INFO: Copying 0 resources to EXE
95575 INFO: Embedding manifest in EXE
95592 INFO: Updating manifest in C:\Users\linex\AppData\Local\Temp\tmplqdfksx0\application\hookmemory006.exe.notanexecutable
95598 INFO: Updating resource type 24 name 1 language 0
95612 INFO: Appending PKG archive to EXE
95641 INFO: Fixing EXE headers
95978 INFO: Building EXE from EXE-00.toc completed successfully.

Moving project to: C:\Users\linex\Documents\output
Complete.
0
DarekRepos napisał(a):

Z ciekawości odpaliłem ten przykładowy kod który podałeś.
Odpaliłem konsole. Wszedłem do folderu projektu. zainstalowalem pyinstaler oraz pyautogui bibliotekę
W konsoli wpisałem pyinstaller --noconfirm --onefile --windowed --hidden-import=pyautogui i zrobił się exe . Jak kilkam na ten exe to screenshot się robi w folderze history. Natomiast jako że próbowałem odtworzyć ten błąd to zrobiłem to na Windowsie (bo zazwyczaj używam linux na wsl, bo jest wygodniejszy) to po drodze cały czas musiałem odblokowywać plik w antywirusie bo mi automatycznie blokował wszystko. Więc może spróbuj wyłączyć na chwile antywirusa albo wyłączyć opcje automatycznej izolacji tylko na czas robienia skryptu. Nic innego mi do głowy nie przychodzi. Mam nadzieje, że to jednak pomoże.

Sukces!
Witam ponownie, DarekRepos dzięki wielkie, wyłączenie defendera (antywirusa) wygenerowało sprawny plik .exe, dowodów nie załączam, ale to trafna uwaga. Problem rozwiązany, dziękuje za zaangażowanie oraz poświęcony cenny czas.

Pozdrawiam użytkowników oraz @DarekRepos

0

P.S.
Sprawdziłem kompilacja pliku bez dyrektywy: --hidden-import=pyautogui, również zakończona sukcesem!

1 użytkowników online, w tym zalogowanych: 0, gości: 1