问题
I know there have been a bunch of questions already asked regarding this but none of them really helped me. Let me explain the whole project scenario so that I provide a better clarity to my problem. The directory structure is somewhat like this shown below:
Project Directory Layout
I need to convert this whole GUI based project (The main file is using Tkinter module to create GUI) into main.exe which I can share with others while making sure that all the additional files work exactly the same way it is working now when I run this main.py via Command Prompt. When I use this command with pyinstaller -
"pyinstaller --onefile --noconsole main.py"
It creates main.exe which shows "Failed to execute script" on running. Please provide me a detailed explanation on what should I do to achieve what I have stated above. Thank you in advance.
回答1:
pyinstaller uses a few dirty tricks to compress a bunch of files into one
I recommend using cx_Freeze instead along with inno setup installer maker
do pip install cx_Freeze
to install that and go here for inno setup
then copy the following into a file named setup.py in the same folder as your project
from cx_Freeze import setup, Executable
setup(name = "YOUR APP NAME" ,
version = "1.0.0" ,
description = "DESCRIPTION" ,
executables = [Executable("PYTHON FILE", base = "Win32GUI")]
)
lastly run python setup.py build
if you want as onefile download this file here
just edit the file a bit and use inno compiler to make into installer
回答2:
Create Executable from Python Script using Pyinstaller:
Step 1: Add Python to Windows Path
Step 2: Open the Windows Command Prompt
Step 3: Install the Pyinstaller Package
In the Windows Command Prompt, type the following command to install the pyinstaller package (and then press Enter):
pip install pyinstaller
Step 4: Save your Python Script
I then saved the Python script in the following folder:
C:\Users\Ron\Desktop\MyPython
Where I named the Python script as ‘hello’
Step 5: Create the Executable using Pyinstaller
Now you’ll be able to create the executable from the Python script using pyinstaller.
Simply go to the Command Prompt, and then type:
cd followed by the location where your Python script is stored
In my case, I typed the following in the command prompt:
cd C:\Users\Ron\Desktop\MyPython
Next, use the following template to create the executable:
pyinstaller --onefile pythonScriptName.py
Since in our example, the pythonScriptName is ‘hello‘, then the command to create the executable is:
pyinstaller --onefile hello.py
Once you’re done, press Enter for the last time.
Step 6: Run the Executable
Your executable should now get created at the location that you specified.
In my case, I went back to the location where I originally stored the ‘hello’ script (C:\Users\Ron\Desktop\MyPython).
Few additional files got created at that location. To find the executable file, open the dist folder:
Now you’ll see the executable file:
来源:https://stackoverflow.com/questions/62255467/how-to-convert-a-python-project-into-an-executable-file-with-all-additional-scri