如何包含模块 - cx_Freeze

How include modules - cx_Freeze

我有一个使用 random 模块的文件,如何在我的 setup.py 文件中包含该模块?

这是我的代码:

import sys
from cx_Freeze import setup, Executable

build_exe_options = {'packages': ['sys'], 'excludes': ['tkinter'],
                      'includes': ['random']}

base = None
if sys.platform == 'win32':
base = 'Win32GUI'

setup(name = 'name',
      verison = '0.1',
      description = 'description',
      options = {'build_exe': build_exe_options},
      executables = [Executable('fileName.py', base = base)])

My Script

import random
choices = ['rock', 'paper', 'scissors']
i = randome.randint(0, 2)
title = input('Rock, Paper, Scissor\nPress Enter to continue...')
instructions = input('Please read the instructions carefully...')
end = input('Please type \'done\' to end the game...')
enjoy = input('Enjoy!')
done = False
while not done:
    You = input('\nRock, paper, or scissors? \n')
    Computer = choices[i]
    if You == Computer:
        print('Tie')
    if You == 'rock' and Computer == 'paper':
        print('Computer wins!')
    if You == 'paper' and Computer == 'scissors':
        print('Computer wins!')
    if You == 'scissors' and Computer == 'rock':
        print('Computer wins!')
    if Computer == 'rock' and You == 'paper':
        print('You win')
    if Computer == 'paper' and You == 'scissors':
        print('You win')
    if Computer == 'scissors' and You = 'rock':
        print('You win')
    if You == 'done':
        exit()

我已经检查了你提供的两个脚本,我发现有很多错误,它们可能是输入错误/我不知道的真正错误,但它们会阻止你的脚本正常工作。

  • if Computer == 'scissors' and You = 'rock':应该是and You == 'rock':(两个等号)

  • i = randome.randint(0, 2)应该是i = random.randint(0, 2)random末尾没有e)

现在开始安装脚本。

verison = '0.1', 应该是 version = '0.1',(版本不是 verison)

出现此错误是因为您试图在不存在 GUI 时隐藏控制台。

base = None
if sys.platform == 'win32':
   base = 'Win32GUI'

base = None 表示控制台出现。 base = 'Win32GUI'表示控制台是隐藏的。您不能这样做,因为没有 GUI(例如 Tkinter)可以使用它。

要解决此问题,只需删除:

if sys.platform == 'win32':
   base = 'Win32GUI'

来自您的脚本。

如果你这样做,你也不需要 import sys

此脚本将包括文件夹 tcl8.6tk8.6 以外的所有内容。您需要手动将它们复制到您的构建文件夹中。

from cx_Freeze import setup, Executable
import os

files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path to Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}

os.environ['TCL_LIBRARY'] = "<Path to Python>/Python36-32/tcl/tk8.6"
os.environ['TK_LIBRARY'] = "<Path To Python>/Python36-32/tcl/tk8.6"

base = "Win32GUI"

setup(
    name = "Name of app",
    version = "0.1",
    author = "The author",
    options = {'build_exe': files},
    description = "Enter Description Here",
    executables = [Executable("tk_ex.py", base=base)])

编辑:

此脚本无需复制和粘贴任何内容即可运行。

from cx_Freeze import setup, Executable
import os
import sys
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR,'tcl','tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
files = {"include_files": ["<Location to Python>/Python36-32/DLLs/tcl86t.dll", "<Location to Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}

setup(
name = "Name of app",
version = "0.1",
author = "The author",
options = {'build_exe': files},
description = "Enter Description Here",
executables = [Executable("tk_ex.py", base=base)])