pythonpython-3.xtkintertkinter-canvastkinter-entry

tcl issue while running a python script from another python app (converted to exe)


I made an app in tkinter which helps creating tkinter app/scripts, (when I export the file it is stored as a .py)

I want to run this .py script from my app so that we can preview it immediately after export.

I used subprocess.run method and it works perfectly within the python app. But when I converted the app to exe with pyinstaller then the preview thing doesn't work because of a tcl version error.

init.tcl: version conflict for package "Tcl": have 8.6.10, need exactly 8.6.9
version conflict for package "Tcl": have 8.6.10, need exactly 8.6.9
    while executing
"package require -exact Tcl 8.6.9"
    (file "C:/----/_MEI170162/tcl/init.tcl" line 19)
    invoked from within
"source {C:/-----/_MEI170162/tcl/init.tcl}"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list source $tclfile]"


This probably means that Tcl wasn't installed properly.

I tried subprocess.run, subprocess.startfile, os.system, and even webbroswer.open methods to run the exported .py tkinter app, but the same error is showing. Note that I have compiled the app with an older version of python, so the tcl version is also different. In my main system the python version is set to latest one, so subprocess is using that tcl version, but I don't want to have any connection with the main app's tcl version.

As the user may install different versions of python, so there will be different tcl versions too, and this error will be shown again in that case.

I tried the exec(open(file).read()) method, although it works but there are issues with some exports like pyimages missing errors.

Is there any other way to run a python script which is more standalone?


Solution

  • Finally got an answer, we can delete the tcl environment variables before running the subprocess

    import os
    import subprocess
    
    file_path = "C:/----/test.py"
    
    def run_script():
        env = os.environ.copy()
        if 'TCL_LIBRARY' in env:
            del env['TCL_LIBRARY']
        if 'TK_LIBRARY' in env:
            del env['TK_LIBRARY']
        subprocess.run(["python",file_path], shell=True, env=env)