Table of Contents
Freezing Your Code
Installing PyInstaller
Use PyInstaller
Introduction to the principle of PyInstaller
Home Backend Development Python Tutorial How to package a Python script into an executable file? (detailed)

How to package a Python script into an executable file? (detailed)

Sep 27, 2018 pm 03:30 PM
python script

This article brings you how to package a Python script into an executable file? (Details), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Python is a scripting language that is interpreted and executed by the interpreter. Its release method:

  • .py file: For open source projects or where the source code is not that important, the source code is provided directly, and the user needs to install Python and various dependent libraries by themselves. (This is what Python official installation packages do)

  • .pyc file: Some companies or individuals do not want the source code to be seen by the operator due to confidentiality or various reasons. You can Publish using pyc files. The pyc file is a binary code that can be recognized by the Python interpreter, so it is cross-platform after publishing. Users need to install the corresponding version of Python and dependent libraries.

  • Executable file: For non-coders or novice users, it would be a disaster if you ask them to install Python and also have to deal with a bunch of dependent libraries. For such users, the easiest way is to provide an executable file and just tell them how to use it. What is more troublesome is that different executable files need to be packaged for different platforms (Windows, Linux, Mac,...).

This article mainly introduces the last method. Both .py and .pyc are relatively simple, and Python itself can handle it. There are many ways to package Python scripts into executable files. This article focuses on PyInstaller, and the others are only for comparison and reference.

Freezing Your Code

The comparison of various packaging tools is as follows (from the article Freezing Your Code):

##bbFreezeyesyesyesnoMITnoyesyesyespy2exeyesnonoyesMITyesyesnonopyInstalleryesyes yesnoGPLyesnoyesnocx_FreezeyesyesyesyesPSFnoyesyesnopy2appnonoyesyesMITnoyesyes yes

PS. Among them, pyInstaller and cx_Freeze are both good. Some people on stackoverflow also suggested using cx_Freeze, saying that it is more convenient. The new version of pkg_resources seems to be supported by pyInstaller.

Installing PyInstaller

For those users whose network is relatively stable and can use the pip source address smoothly, just run the following command:

pip install pyinstaller
Copy after login

Usually we will download the source code package, then enter the package directory and execute the following command (setuptools needs to be installed):

python setup.py install
Copy after login

After installation , check whether the installation is successful:

pyinstaller --version
Copy after login

After the installation is successful, you can use the following command:

  • pyinstaller: The main command for packaging executable files. Detailed usage will be introduced below.

  • pyi-archive_viewer : View the file list in the executable package.

  • pyi-bindepend : View the dynamic libraries (.so or .dll files) that the executable file depends on

  • pyi-... : Wait.

Use PyInstaller

pyinstaller's syntax:

pyinstaller [options] script [script ...] | specfile
Copy after login

Most Simple usage, execute the command in the same directory as myscript.py:

pyinstaller mycript.py
Copy after login

Then you will see that two new directories, build and dist, have been added, below dist The file is an executable file that can be published. For the above command, you will find a bunch of files under the dist directory, including dynamic library files and myscrip executable files. Sometimes this feels troublesome. You need to package everything under dist before publishing. If you lose a dynamic library, it will not run. Fortunately, pyInstaller supports single file mode. You only need to execute:

pyinstaller -F mycript.py
Copy after login

You will find that there is only one executable file under dist. This single file can be published and can run under a system similar to the operating system you are using.

Of course, pyinstaller also has various options, including general options, such as the -d option for debugging and understanding the execution process of pyInstaller; there are also some options for different platforms, specific usage You can visit the PyInstaller official WIKI.

When executing the pyInstaller command, a .spec file will be generated in the same directory as the script. This file will tell pyinstaller how to process all your scripts. , which also contains command options. Generally we don't need to pay attention to this file. If we need to package data files, or add some Python runtime options to the packaged binary... some advanced packaging options, we need to manually edit the .spec file. You can use:

pyi-makespec options script [script ...]
Copy after login

to create a .spec file. For manually edited .spec files, we can use any of the following commands:

pyinstaller specfile
pyi-build specfile
Copy after login

Introduction to the principle of PyInstaller

PyInstaller actually packages the python parser and your own script into an executable file and compiles it into real machine code They are completely different things, so don't expect that packaging into an executable file will improve operating efficiency. On the contrary, it may reduce operating efficiency. The advantage is that there is no need to install Python and the libraries your script depends on on the runner's machine. Under the Linux operating system, it mainly uses the ldd and objdump commands in the binutil tool package.

PyInstaller enters the script you specified, first analyzes other scripts that the script depends on, then searches, copies, collects all related scripts, including the Python parser, and then puts these files in a directory Download it, or package it into an executable file.

You can directly publish the files in the entire output folder, or the generated executable file. You only need to tell users that your application is self-contained and does not require the installation of other packages or a certain version of Python before it can be run directly.

It should be noted that the executable files packaged by PyInstaller can only be used in the same environment as the packaging machine system. In other words, it is not portable and must be packaged for that platform if it needs to run on a different system.

Solution Windows Linux OS X Python 3 License One-file mode Zipfile import Eggs pkg_resources support

The above is the detailed content of How to package a Python script into an executable file? (detailed). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python script to create random jokes using pyjokes Python script to create random jokes using pyjokes Sep 13, 2023 pm 08:25 PM

Do you want to add some humor to your Python script or application? Whether you're building a chatbot, developing a command line tool, or just want to entertain yourself with random jokes, the pyjokes library can help. With pyjokes you can easily generate jokes in various categories and customize them to your liking. In this blog post, we will explore how to create random jokes in Python using the pyjokes library. We'll cover the installation process, generating different categories of jokes, customizing jokes, displaying them in a console application or web page, and handling any potential errors that may occur. Install pyjokes Before we start using pyjokes to create random jokes, we need

Do you know some reasons why crontab scheduled tasks are not executed? Do you know some reasons why crontab scheduled tasks are not executed? Mar 09, 2024 am 09:49 AM

Summary of some reasons why crontab scheduled tasks are not executed. Update time: January 9, 2019 09:34:57 Author: Hope on the field. This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

Python script automatically refreshes Excel spreadsheet Python script automatically refreshes Excel spreadsheet Sep 09, 2023 pm 06:21 PM

Python and Excel are two powerful tools that when combined can open up a world of automation. Python has versatile libraries and user-friendly syntax that enable us to write scripts to perform various tasks efficiently. Excel, on the other hand, is a widely used spreadsheet program that provides a familiar interface for data analysis and manipulation. In this tutorial, we will explore how to leverage Python to automate the process of refreshing Excel spreadsheets, saving us time and effort. Do you find yourself spending valuable time manually refreshing your Excel spreadsheet with updated data? This is a repetitive and time-consuming task that can really kill productivity. In this article we will guide you through using Py

Exploring Orange3: Opening up a new world of data mining and machine learning! Exploring Orange3: Opening up a new world of data mining and machine learning! Mar 04, 2024 pm 08:16 PM

Orange3 is a powerful open source data visualization and machine learning tool. It has rich data processing, analysis and modeling functions, providing users with simple and fast data mining and machine learning solutions. This article will briefly introduce the basic functions and usage of Orange3, and combine it with actual application scenarios and Python code cases to help readers better master the usage skills of Orange3. The basic functions of Orange3 include data loading, data preprocessing, feature selection, model establishment and evaluation, etc. Users can use the intuitive interface to drag and drop components to easily build data processes. At the same time, more complex data processing and modeling tasks can also be completed through Python scripts. Below we will go through a practical

PyCharm Advanced Tutorial: Use PyInstaller to package code into EXE format PyCharm Advanced Tutorial: Use PyInstaller to package code into EXE format Feb 20, 2024 am 09:34 AM

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Python script for monitoring website changes Python script for monitoring website changes Aug 29, 2023 pm 12:25 PM

In today's digital age, being aware of the latest changes on your website is crucial for a variety of purposes, such as tracking updates on your competitors' websites, monitoring product availability, or staying informed of important information. Manually checking your website for changes can be time-consuming and inefficient. This is where automation comes into play. In this blog post, we will explore how to create a Python script to monitor website changes. By leveraging the power of Python and some handy libraries, we can automate the process of retrieving website content, comparing it to previous versions, and notifying us of any changes. This allows us to remain proactive and react promptly to updates or modifications to the sites we monitor. Setting up the environment Before we start writing scripts to monitor website changes, we need to set up P

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to read excel data in pycharm How to read excel data in pycharm Apr 03, 2024 pm 08:42 PM

How to read Excel data using PyCharm? The steps are as follows: install the openpyxl library; import the openpyxl library; load the Excel workbook; access a specific worksheet; access cells in the worksheet; traverse rows and columns.

See all articles