简体   繁体   中英

Converting a VPython program to an exe using cx_Freeze

I have coded a VPython simulation but I can't compress it into an exe using cx_Freeze. I think it is because of VPython module since cx_Freeze works when I compress other programs that don't use VPython.

Detailed steps:

This is my test simulation program:

from vpython import *

### Simulation ###
def run_simulation(r):
    ball = sphere(radius=r)

run_simulation(5)

Here is my setup file:

from cx_Freeze import setup, Executable

setup(name='Test',
      version='0.1',
      description='Parse stuff',
      executables=[Executable('Test.py')])

I go to the folder which these two are in, hold shift and right click, then press on Windows PowerShell. In the shell I type: python setup.py build

The supposed outcome is a new folder called 'build' (that has the executable) is created in the already existing folder, but instead I get this error on the PowerShell:

KeyError: 'TCL_LIBRARY'

For Simon's answer: Here is a picture of the error I get when I try to run it, along with my new code in my set up file.

Error: PowerShell上的语法错误

from cx_Freeze import setup, Executable
import os

os.environ['TCL_LIBRARY'] = C:\Users\mohamed-tayeh\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6

setup(name='Test',
    version='0.1',
    description='Parse stuff',
    executables=[Executable('Test.py')])

For some reason your script requires tkinter (or Vpython might) because it is is missing the "TCL_LIBRARY" and "TK_LIBRARY" (which always go together) which are just a few folders (note that this is not the actual name of the folder containing the tkinter widgets).

So what do I do?

Quite simply add the missing folders.

The easiest way you can do this is by either:

  • Coping them in the build folder manually
  • Using the environ function in the os module.

Note that the first is only suitable if you are only creating your app in a build folder and not an msi. If you want to do it correctly then go for the second!


I don't know your Python version or path so I'm going to leave making the file paths to you.


TCL_LIBRARY is is actually located in tcl and is a folder called tcl8.6 and TK_LIBRARY is named tk8.6 . Just include those in your build folder.

You need to use os.environ to copy it into your build folder:

os.environ['TCL_LIBRARY'] = '<path to Python>\\Python36-32\\tcl\\tcl8.6'
os.environ['TK_LIBRARY'] = '<path to Python>\\Python36-32\\tcl\\tk8.6'

So what should my script look like now?

Your script should look almost the same except you import os and use environ:

from cx_Freeze import setup, Executable
import os

os.environ['TCL_LIBRARY'] = '<path to Python>\\Python36-32\\tcl\\tcl8.6'
os.environ['TK_LIBRARY'] = '<path to Python>\\Python36-32\\tcl\\tk8.6'

setup(name='Test',
    version='0.1',
    description='Parse stuff',
    executables=[Executable('Test.py')])

There. The error should disappear now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM