cx_freeze模块依赖性 [英] cx_freeze module dependency

查看:122
本文介绍了cx_freeze模块依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的python脚本依赖于我创建的另一个模块。该模块读取一个文本文件。从源代码运行时,脚本,模块及其读取的文件通常位于同一目录中,并且一切正常。

So my python script depends on another module I created. This module reads in a text file. The script, the module, and the file it reads are normally in the same directory when I run from source and everything works great.

我用cx_freeze编译,当我运行它,导入的模块失败。该模块尝试读取文件,并说找不到该文件,并且一切都在那里停止。

I compiled with cx_freeze, and when I run it, the imported module fails. The module tries to read the file and says it can't find it, and everything stops right there.

该文本文件包含在library.zip和build文件夹中(可能是不必要的,但我认为它不会造成伤害)。我决定在读取文件之前先在模块中打印工作目录,以查看发生了什么,看来工作目录不是构建文件夹,而是我的用户主目录。

The text file is included in the library.zip, and the build folder (probably unnecessarily, but I figured it couldn't hurt). I decided to print the working directory in the module before reading the file to see what was happening, and it looks like the working directory isn't the build folder, but my user home directory.

当然,文本文件不在用户的主目录中。我该如何解决?

Of course the text file isn't in my user's home directory. How can I fix this?

具体来说,这是一个例子。所有文件都在同一目录中。

Just to be concrete, here's an example. All files are in the same directory.

script.py:

script.py:

import hello

hello.py

import os
print(os.getcwd())
f = open('hello.txt','r')
print(f.read())
f.close()

hello.txt

hello.txt

hello!

setup.py

import sys
import os
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.

includes = ['hello.txt']
zip_inc = ['hello.txt']

build_exe_options = {"packages": ["os"], "include_files": includes, "zip_includes": zip_inc}

setup(  name = "test",
        version = "0.1",
        description = "test",
        options = {"build_exe": build_exe_options},
        executables = [Executable("script.py")])

我使用以下命令构建:

python setup.py build

然后我在构建目录中运行了名为script的文件。如果有的话,我在Mac OS X中。输出如下:

Then I ran the file called script in the build directory. I'm in Mac OS X if it matters. The output is as follows:

/Users/pianowow
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/cx_Freeze/initscripts/Console3.py", line 27, in <module>
    exec(code, m.__dict__)
  File "script.py", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py", line 1558, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py", line 1525, in _find_and_load_unlocked
    loader.load_module(name)
  File "/Users/pianowow/Desktop/test/hello.py", line 3, in <module>
    f = open('hello.txt','r')
FileNotFoundError: [Errno 2] No such file or directory: 'hello.txt'


推荐答案

我有一个请求请求来更新文档-这是有关使用数据文件的更新部分:

I've got a pull request to update the docs - here's the updated section on using data files:

应用程序除代码外还经常需要数据文件,例如图标。使用安装脚本,您可以在build_exe的include_files选项中列出数据文件或目录。它们将被复制到可执行文件旁边的构建目录中。然后使用以下代码查找它们:

Applications often need data files besides the code, such as icons. Using a setup script, you can list data files or directories in the include_files option to build_exe. They'll be copied to the build directory alongside the executable. Then to find them, use code like this:

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

另一种方法是将数据嵌入代码中,例如使用Qt的资源系统。

An alternative is to embed data in code, for example by using Qt's resource system.

[来自此文件]

这篇关于cx_freeze模块依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆