简体   繁体   English

使用 Cx-Freeze 编译 python 项目

[英]Compiling python project with Cx-Freeze

I'm trying to make and executable from a project I'm working on, to test it on a different computer, without requiring python installed.我正在尝试从我正在处理的项目中制作和执行,以在另一台计算机上对其进行测试,而无需安装 python。

I want to use cx freeze to build and exe file, and this is my project tree:我想使用 cx freeze 来构建和 exe 文件,这是我的项目树:

- Project
   - objects
         blocks.py
         enemy.py
         item.py
         player.py
         __init__.py
   - scripts
         camera.py
         chunk_manager.py
         __init__.py
   - textures
       - items
       - player
       - terrain
       - UI
       - void
         index.py
         __init__.py
     main.py
     settings.py
     setup.py
     map.json

As you can probably tell, this is a game.您可能会说,这是一场游戏。

Anyways, what I have done is executing just cxfreeze main.py --target-dir=./ dist and it generated a build directory with a lot of stuff in it.无论如何,我所做的只是执行cxfreeze main.py --target-dir=./ dist并且它生成了一个包含很多东西的构建目录。 It generated a linux executable, but thats fine, I want to test if I can make it python-independent first, and I'll solve the.exe part later.它生成了一个 linux 可执行文件,但没关系,我想先测试是否可以使其独立于 python,稍后我将解决 .exe 部分。

So, I executed the file, and nothing happened.所以,我执行了文件,什么也没发生。 Then I tried running from the terminal and it said that it was missing the camera.py file.然后我尝试从终端运行,它说它缺少 camera.py 文件。 I went into the generated directory and I found out that cxfreeze had not copied that file.我进入生成的目录,发现 cxfreeze 没有复制该文件。 I tried moving in in there myself, but it did not work.我试着自己搬进去,但没有用。

I started checking for other files, and I found out that only the textures had been copied.我开始检查其他文件,我发现只有纹理被复制了。 I thought this was just because of the one line command I used, so I am now trying to make a setup.py file (as you can see in the file tree) but I am lost in how to import my custom packages (objects, scripts, and textures) and the files in the same directory as main.py我认为这只是因为我使用了一行命令,所以我现在正在尝试制作一个 setup.py 文件(正如您在文件树中看到的那样),但是我迷失了如何导入我的自定义包(对象,脚本和纹理)以及与 main.py 位于同一目录中的文件

This is how my setup.py file looks like:这就是我的 setup.py 文件的样子:

import sys
from cx_Freeze import setup, Executable


options = {
    'build_exe': {
        'includes': ['camera', 'chunk_manager'], 'path': sys.path + ['scripts']
    }
}

setup(
    name = "Void Boats",
    version = "alpha0.1",
    options = options,
    executables = [Executable("main.py")])

I am not sure how would I put all the packages in the 'include' section, and I can't find anything on the internet that uses more than a simple file like helloworld.py, and the samples of cxfreeze on their github only show how to import files from one directory.我不确定如何将所有包放在“包含”部分中,而且我在互联网上找不到任何使用比 helloworld.py 之类的简单文件更多的内容,并且其 github 上的 cxfreeze 示例仅显示如何从一个目录导入文件。 Do I have to move everything into one single package and put all the files in the same 'include'?我是否必须将所有内容移至一个 package 并将所有文件放在同一个“包含”中? Or can I have one include for each package I have?或者我可以为我拥有的每个 package 包含一个吗? Any help would be pretty much appreciated.任何帮助将不胜感激。

Edit: I'm running on ParrotSec 4.9 (Debian based)编辑:我在 ParrotSec 4.9(基于 Debian)上运行

Use "python -m setup.py build" on cmd, that will build exe.在 cmd 上使用“python -m setup.py build”,这将构建 exe。

Example setup with extra folders:带有额外文件夹的示例设置:

from cx_Freeze import setup, Executable
import sys

version = "0.0.6"

build_options = {
    "packages": [],
    "excludes": [],
    "build_exe": "X:\\builds\\",
    "include_files": ["Sequence_Sample/", "icons/"],
}

base = "Win32GUI" if sys.platform == "win32" else None

executables = [Executable("MainUIController.py", base=base, targetName="pym")]

setup(
    name="Python Image Macro Project",
    version=version,
    description="Image Based macro project.",
    options={"build_exe": build_options},
    executables=executables,
)

Python 3.8 and later has problem with cx_freeze 6.1 - not copying python dll. Python 3.8 及更高版本与 cx_freeze 6.1 存在问题- 不复制 python dll。 cx_freeze 6.2 is strongly recommended if that's the case.如果是这种情况,强烈建议使用 cx_freeze 6.2。

You'll have to clone cx_freeze and build it, then install it to use 6.2.您必须克隆 cx_freeze 并构建它,然后安装它以使用 6.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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