从另一个文件导入本地函数和变量时 Pyinstaller 不工作

发布于 2025-01-10 03:12:20 字数 702 浏览 3 评论 0原文

标题。 我有3个文件。 1 个主文件,用于运行脚本。 1 个函数文件,保存脚本使用的函数。以及 1 个数据文件,用于保存脚本访问的变量。 当我使用 pyinstaller 将 main 函数编译为 exe 时,尽管尝试了许多不同的选项(--onefile、--add-data 等),但我无法弄清楚如何修复它。 关于我的设置的更详细视图:

# data file
my_bool = False
# functions file
def my_func():
    from data_file import my_bool
    my_bool = not my_bool
# main script
from data_file import my_bool
from functions_file import my_func
print(my_bool)
my_func()
print(my_bool)

对我来说,这实际上在尝试编译文件之前完全完成了它应该做的事情。当然,这不是我的实际设置,但在检查后它确实像我预期的那样工作,输出:

False
True

所以在所有解释之后,我只是想知道如何解决这个问题。我认为这与我导入所有内容的方式有关,但我应该如何解决这个问题?我总是可以,但我不想因为一个可能可以解决的小问题而被迫重写整个项目。 提前致谢

Title.
I have 3 files.
1 main file, to run the script. 1 function file, that holds the functions that the script uses. And 1 data file, that holds the variables that the script accesses.
When I use pyinstaller to compile the main function to an exe, despite many attempts with a bunch of different options, (--onefile, --add-data, etc.) I haven't been able to figure out how to fix it.
More detailed view on my setup:

# data file
my_bool = False
# functions file
def my_func():
    from data_file import my_bool
    my_bool = not my_bool
# main script
from data_file import my_bool
from functions_file import my_func
print(my_bool)
my_func()
print(my_bool)

For me, this actually does what it's supposed to do perfectly fine before the files are attempted to be compiled. Of course this isn't my actual setup but it does work like I expected after checking it, outputting:

False
True

So after all that explanation, I'm just wondering how I can fix this issue. I assume it has something to do with how I import everything, but how should I fix that? I always could, but I don't want to be forced to rewrite this whole project over one small, probably fixable issue.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2025-01-17 03:12:20

首先,Pyinstaller 非常挑剔。大多数较新的版本与某些模块不兼容,即最新的 Pyinstaller 无法与 Pygame 字体一起使用,至少根据我的经验,因此请考虑检查日志文件或它创建的错误消息。

其次,为了让 Pyinstaller 处理多个文件,它们都需要位于同一目录中,甚至是同一子目录中。 TechwithTim 有一个很棒的视频解释了所有这些,但总而言之,您需要将所有文件和依赖项置于同一级别,并具有相同的访问级别。

除此之外,我看不出它有什么问题。代码专家不仅看起来非常简单,而且我已经成功地完成了更繁重的工作,没有任何问题,这让我认为这可能是您使用的模块和 Pyinstaller 的兼容性问题。

希望这会有所帮助,我真的推荐我之前提到的视频。

First of all, Pyinstaller is very Finicky. Most newer versions are not compatible with some modules, i.e. the latest Pyinstaller will not work with Pygame Fonts, at least in my experience, so consider checking the log files, or error messages it creates.

Secondly, for Pyinstaller to work with multiple files, they all need to be in the same directory, even the same subdirectory. TechwithTim has a great video explaining all of this, but the long and short of it is, that you need to have all your files and dependencies on the same level, and with the same access level.

Other than that, I cannot see anything wrong with it per see. Not only has the code expert look pretty simple, but I have managed to do heavier stuff with no issue, which makes me think it's probably a compatibility issue with the Modules your using and Pyinstaller.

Hope this helps, and I do really recommend that video I mentioned earlier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文