M
M
motya882021-12-03 13:01:26
Building projects
motya88, 2021-12-03 13:01:26

How to solve the problem with running a third-party script from an exe file built with pyinstaller?

I'm trying to build an exe file with pyinstaller
Below is a simplified example.
The main file that I am converting to exe is test.py its code is below

from tkinter import messagebox
from tkinter import *
import subprocess

import os
import sys


pid_list = []
chk_chrome = []


def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.dirname(__file__)
    return os.path.join(base_path, relative_path)





def on_close():
    if messagebox.askokcancel('Выход', 'Действительно хотите закрыть окно?'):

        root.destroy()




def retrieve():
    subprocess.Popen(['sys.executable', resource_path('test2.py'), id_entry.get()])




root = Tk()
root.title("TEST. Тест")

width = 350
heigh = 230
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width, heigh, (screenwidth - width) / 2, (screenheight - heigh) / 2))

# можно ли изменять размер окна - нет
root.resizable(False, False)

id = StringVar()


id_label = Label(text="Введите ID:")

id_label.grid(row=0, column=0, sticky="w", padx=30, pady=15)



id_entry = Entry(validate='key', textvariable=id, width=20)


id_entry.grid(row=0, column=1, padx=30, pady=10)


start_button = Button(text="Поехали!", command=retrieve, width=10)
start_button.grid(row=5, column=1, padx=30, pady=10, sticky="e")




# вставка начальных данных
id_entry.insert(0, "306310073")

root.protocol('WM_DELETE_WINDOW', on_close)

root.mainloop()

The problem occurs when I try to run another script on a button click via the command
subprocess.Popen(['sys.executable', resource_path('test2.py'), id_entry.get()])

The script just doesn't run

the test2.py file itself
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
import os
import sys
import time
import traceback


def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.dirname(__file__)
    return os.path.join(base_path, relative_path)


option = webdriver.ChromeOptions()
option.add_argument("window-size=1280,800")
option.add_argument('--disable-blink-features=AutomationControlled')
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
s = Service(resource_path('./driver/chromedriver.exe'))

browser = webdriver.Chrome(service=s, options=option)
wait = WebDriverWait(browser, 60)

while True:
    try:
        browser.get('http://ya.ru/')
        time.sleep(4)
    except Exception as e:
        print('ОШИБКА: ' + str(e))
        traceback.print_exc()
        browser.quit()


test.spec file
# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(['test.py'],
             pathex=[],
             binaries=[('./driver/chromedriver.exe', './driver')],
             datas=[('test2.py', '.')],
             hiddenimports=[],
             hookspath=[],
             hooksconfig={},
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,  
          [],
          name='test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )


Please help to build an exe in which a script with selenium will be launched on clicking the button

Tested on a PC where python is not installed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andy_U, 2021-12-03
@motya88

Do you have sys.executable in quotes for a reason?

D
Dima Pautov, 2015-05-18
@bootd

I think this logo font is hand drawn!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question