gpt4 book ai didi

python - pyinstaller创建的exe文件,运行时找不到自定义模块

转载 作者:太空狗 更新时间:2023-10-29 20:10:46 26 4
gpt4 key购买 nike

我创建了两个python文件,目录/文件关系如下:

mytest---
|---mycommon.py
|---myMainDir---
|----myMain.py

在 mycommon.py 中:

def myFunc(a):
...

在 myMain.py 中:

import sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath('__file__')), '..'))
import mycommon.py
mycommon.myFunc("abc")

然后我使用 pyinstaller 创建了 exe:

pyinstall.py -F mytest\myMainDir\myMain.py

创建了MyMain.exe,但是运行时提示找不到mycommon模块。

最佳答案

PyInstaller的官方手册describes这个问题:

Some Python scripts import modules in ways that PyInstaller cannot detect: for example, by using the __import__() function with variable data, or manipulating the sys.path value at run time. If your script requires files that PyInstaller does not know about, you must help it.

它也是suggests在这种情况下应该怎么做:

If Analysis recognizes that a module is needed, but cannot find that module, it is often because the script is manipulating sys.path. The easiest thing to do in this case is to use the --paths= option to list all the other places that the script might be searching for imports:

pyi-makespec --paths=/path/to/thisdir --paths=/path/to/otherdir myscript.py

这些路径将在分析过程中添加到当前的sys.path

因此,请在构建应用程序时指定--paths 参数。使用说明书states指定 -p 参数是等效的:

-p dir_list, --paths=dir_list

Set the search path(s) for imported modules (like using PYTHONPATH). Use this option to help PyInstaller to search in the right places when your code modifies sys.path for imports. Give one or more paths separated by ; (under Windows) or : (all other platforms), or give the option more than once to give multiple paths to search.

关于python - pyinstaller创建的exe文件,运行时找不到自定义模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com