如何将Python(具有结构文件夹)编写的应用程序发布给用户

How to release an application write by Python (which has structure folder) to user

我是 Python 的新手,我编写了 Pycharm 测试应用程序。 我的应用程序有结构文件夹,我想将手动测试器的构建包发布到 运行 它。 我的文件夹结构如下:

Actions\Common.py
Actions\DB.py
Form\DualTestObject.py
Configuration\Interface.txt
Configuration\Setting.txt
Testcase\ImageCapture.py
Testcase\OCRTestCase.py
Runbat\ImageCaptureTest.bat
Runbat\OCR.bat
Runbat\...
.main.py
I use command below to build file exe.
pyinstaller --paths = <pathtofoler>\Actions;<pathtofolder>\Form;<pathtofolder>\Testcase; C:\HHS\Runbat main.py

构建过程成功完成后。我 运行 文件夹 dist\main 中的文件 main.exe,程序可以 运行 但它抛出如下错误:

C:\HHS\dist\main>main.exe
Which testcase do you want to test
1. Image Capture
2. Reset Base
3. OCRTestcase
4. USBOEMCommand (remember turn off Vietnamese):
Please enter that number: 1
**Traceback (most recent call last):
  File "main.py", line 19, in <module>
  File "subprocess.py", line 349, in call
  File "subprocess.py", line 951, in __init__
  File "subprocess.py", line 1420, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
[22080] Failed to execute script 'main' due to unhandled exception!
C:\HHS\dist\main>**

虽然我 运行 这些来源来自 Pycharm,但 运行 没问题。

我的 main.py 文件的内容是:

import subprocess
import os
print("Which testcase do you want to test")
print("1. Image Capture")
print("2. Reset Base")
print("3. OCRTestcase")
print("4. USBOEMCommand (remember turn off Vietnamese):")
option = input("Please enter that number: ")
current_folder = currentfolder = os.getcwd()
runbatfolder = current_folder + "\Runbat\"
if(option == "1"):
    filebat = runbatfolder + "ImageCaptureTest.bat"
elif(option == "2"):
    filebat = runbatfolder + "ResetBase.bat"
elif(option == "3"):
    filebat = runbatfolder + "OCR.bat"
elif(option == "4"):
    filebat = runbatfolder + "USBOEMCommand.bat"
subprocess.call(filebat)

那么如何解决这个问题,我认为我导入子流程模块的方式有问题,但我不知道如何解决。

C:\HHS\dist\main>main.exe

current_folder = currentfolder = os.getcwd()
runbatfolder = current_folder + "\Runbat\"
filebat = runbatfolder + "ImageCaptureTest.bat"

(下次做:os.path.join(..)。它更稳定并确保正确创建字符串。)

这将产生:

current_folder = "C:\HHS\dist\main"
runbatfolder = "C:\HHS\dist\main\Runbat"
filebat = "C:\HHS\dist\main\Runbat\ImageCaptureTest.bat"

然而看着:

pyinstaller --paths = <pathtofoler>\Actions;<pathtofolder>\Form;<pathtofolder>\Testcase; C:\HHS\Runbat main.py

您的 Runbat 文件夹位于 C:\HHS\Runbat 而不是 C:\HHS\dist\main\Runbat


os.getcwd() return是您当前的工作目录。当您从 C:\HHS 运行 main.py 时,它将 return C:\HHS.
当您 运行 从 C:\HHS\dist\main 创建 main.exe 时,它将 return C:\HHS\dist\main.

因此,将 main.exe 移动到 C:\HHS\Runbat 应该可行,或者您可以处理路径字符串。

如您所说,您 运行 来自 dist 文件夹的 main.exe。确保从脚本所在的位置复制所有子文件夹。

举个例子:如果你有这样的脚本:

-D:/myScript/main.py
-D:/myScript/someOtherPythonFiles/script1.py
-D:/myScript/someOtherPythonFiles/script2.py

你从 main.py 到脚本的路径是 current directory + someOtherPythonFiles + name of the script 并且你得到 D:/myScript(current location)/someOtherPythonFiles/script1.py(name of the script).

如果您创建一个可执行文件,并且该可执行文件位于 dist/main 文件夹中,您的脚本路径如下所示:
D:/myScript/dist/main(current location)/someOtherPythonFiles/script1.py(name of the script) -> 但你没有它,除非你把它复制到那里。
所以最后的比较:

来自 main.py 的位置:D:/myScript/someOtherPythonScrips/script1.py
来自 dist/main/main.exe 的位置:D:/myScript/dist/main/someOtherPythonScripts/script1.py
-> 确保你有这个