Pyinstaller 冻结时无法找到模块

问题描述 投票:0回答:1

这就是我所拥有的:

├── demo
│   ├── mypkg
│   │   └── __main__.py
│   │   └── api.py
│   │   └── startserver.py
│   └── readme.md

api.py 有=>

import hug
@hug.get('/ping')
def ping():
    return {"response": "pong"}

startserver.py 有=>

def start():
    
  try:
    currentpath = Path(__file__)
    print(f'Currently executing from {currentpath}')
    apipath = os.path.join(currentpath.parent, 'api.py')
    print(f'parse api path is {apipath}')
    print('inside startserver start()')
    with open('testapi.log', 'w') as fd:
        subprocess.run(['hug', '-f', apipath], stdout=fd , stderr=subprocess.STDOUT, bufsize=0)
  except Exception:
     print(traceback.format_exc())

__main.py__
有=>

import traceback
from mypkg.startserver import start

def main():
    try:
        start()
    except Exception:
        print(traceback.format_exc())

if __name__ == "__main__":
    print('... inside name == main ...')
    main()

当从 vscode 终端执行“python -m mypkg”时,效果很好,拥抱 Web 服务器正在运行,浏览器可以在本地主机中看到 /ping 结果

但是当我使用 pyinstaller 创建可执行文件(Windows 10 上的单文件 exe)时,出现此错误:

Currently executing from 

C:\Users\JOHN~1.KOL\AppData\Local\Temp\_MEI442282\mypkg\startserver.pyc
parse api path is C:\Users\JOHN~1.KOL\AppData\Local\Temp\_MEI442282\mypkg\api.py
inside startserver start()
Traceback (most recent call last):
  File "mypkg\startserver.py", line 16, in start
  File "subprocess.py", line 548, in run
  File "subprocess.py", line 1026, in __init__
  File "subprocess.py", line 1538, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified

好像没有

C:\Users\JOHN~1.KOL\AppData\Local\Temp\_MEI442282\mypkg\api.py
我什至尝试将 api.py 更改为 api.pyc 仍然没有成功。我如何获得对 api.py 的引用,这是启动 Hug Web 服务器所必需的?我在这里看到了其他针对丢失配置文件和二进制文件的解决方案,但这个是需要的 .py 文件本身。

非常感谢任何帮助,谢谢!

python pyinstaller hug
1个回答
0
投票

Command

hug
是单独的脚本,它不在 Python 文件夹中 - 因此
pyinstaller
可能找不到它,并且无法将其添加到
exe
。您可能需要手动添加它(可能使用文件
spec

在 Linux 上,我在

hug
中有
/usr/local/bin/hug

我用Linux命令找到了它

which hug


但是文件

hug
是普通的Python脚本,它导入模块
hug
并运行它。

from hug import development_runner 
development_runner.hug.interface.cli()

您可以在不使用

subprocess
的情况下执行相同的操作,并且无需将文件
hug
添加到
exe

您可能只需将

-f
apipath
附加到
sys.argv

这对我来说是

hug
的开始:

import os
import sys
from pathlib import Path
from hug import development_runner 

currentpath = Path(__file__)
apipath = os.path.join(currentpath.parent, 'api.py')
    
sys.argv.append('-f')
sys.argv.append(apipath)

development_runner.hug.interface.cli()
© www.soinside.com 2019 - 2024. All rights reserved.