如何包含模块--cx_Freeze

时间:2017-11-04 04:42:44

标签: python 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)])
  

我的剧本

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()

2 个答案:

答案 0 :(得分:0)

我已经浏览了您提供的两个脚本,并且发现了一些错误,我发现他们可能正在输入错误/真正的错误我不知道但是他们会阻止您的脚本正常工作。

  • 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

答案 1 :(得分:-1)

此脚本将包含文件夹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)])