杰瑞科技汇

Python可执行文件如何创建?

Of course! The term "Python executable" can refer to a few different things. Let's break it down from the most common meaning to more advanced topics.

The Python Interpreter (The Most Common Meaning)

This is the program that actually runs your Python code. When you write a script (e.g., my_script.py), it's just a text file. The Python interpreter reads this file, understands the Python syntax, and executes the instructions.

On most systems, this executable is simply named python.

How to Find and Use It

a) Finding the Executable Path

You need to know where on your computer the python executable is located.

  • On macOS and Linux: Open your terminal and run:

    which python
    # or for Python 3 specifically
    which python3

    This will output the full path, for example: /usr/bin/python3.

  • On Windows: Open Command Prompt or PowerShell and run:

    where python
    # or for Python 3 specifically
    where python3

    This will output the path, for example: C:\Users\YourUser\AppData\Local\Programs\Python\Python39\python.exe.

b) Running a Script with the Executable

Once you know the path, you can run your Python script from the command line by pointing to the executable and your script file.

# The basic syntax
/path/to/python your_script.py

Example on Linux/macOS:

# Let's say 'which python3' returned '/usr/bin/python3'
/usr/bin/python3 hello.py

Example on Windows:

# Let's say 'where python3' returned 'C:\Python39\python.exe'
C:\Python39\python.exe my_app.py

Important Note on python vs. python3:

  • On modern macOS and Linux systems, python often points to an older version of Python (like Python 2), while python3 points to the current standard (Python 3). It's a best practice to use python3 to ensure you're running the correct version.
  • On Windows, the installer usually adds python.exe and python3.exe to your system's PATH, so you can often just type python or python3 directly.

Creating an Executable from a Python Script (Packaging)

Often, you want to share your Python application with someone who doesn't have Python installed. You can package your script and its dependencies into a single, standalone executable file. The user can then run it like any other program (e.g., MyApp.exe on Windows or MyApp on macOS/Linux).

The most popular tool for this is PyInstaller.

Step-by-Step Guide with PyInstaller

Step 1: Install PyInstaller Open your terminal or command prompt and install it using pip:

pip install pyinstaller

Step 2: Create a Simple Python Script Let's say you have a file named app.py:

# app.py
import sys
import time
def main():
    print("Hello from my awesome Python app!")
    for i in range(5):
        print(f"Counting: {i}")
        time.sleep(1)
    print("Done.")
if __name__ == "__main__":
    main()

Step 3: Run PyInstaller Navigate to the directory containing app.py in your terminal and run one of the following commands:

  • To create a single executable file:

    pyinstaller --onefile app.py

    This will create a single app.exe (on Windows) or app (on macOS/Linux) file. It's convenient but might be slower to start because it has to extract dependencies on the fly.

  • To create a folder with the executable and all its dependencies (recommended for performance):

    pyinstaller app.py

    This creates a dist folder containing app (or app.exe), along with all the necessary .dll or .so files. This is faster and more robust.

Step 4: Run Your Executable After the build is complete, you'll find your executable in the dist folder. You can run it directly without needing Python installed.

# On macOS/Linux
./dist/app
# On Windows
.\dist\app.exe

The Executable Itself (A Technical Look)

When you download Python from python.org, the installer places a binary file on your system. This file is the Python interpreter executable.

  • On Windows: It's python.exe. This is a standard Windows PE (Portable Executable) file.
  • On macOS: It's a Mach-O binary file.
  • On Linux: It's an ELF (Executable and Linkable Format) binary file.

This executable is responsible for:

  1. Parsing: Reading your .py file.
  2. Compiling: Translating your human-readable Python code into bytecode (.pyc files). This is an intermediate step that is faster for the interpreter to run.
  3. Executing: Running the bytecode within the Python Virtual Machine (PVM).

When you run a script, you are essentially telling the operating system to "execute this program (the Python interpreter) with this file (your script) as an argument."

Summary Table

Term Meaning How it's Used
Python Executable (Interpreter) The program that runs Python code (e.g., python.exe, python3). Found on your system. Used to run scripts: /usr/bin/python3 my_script.py
Creating an Executable Packaging a Python script and its dependencies into a standalone file. Done with tools like PyInstaller: pyinstaller --onefile my_app.py
The Executable File The binary interpreter program itself (e.g., the python.exe file you download). It's the result of a Python installation and the target of your commands.
分享:
扫描分享到社交APP
上一篇
下一篇