Python3 How to package Python code as an EXE file

Source: Internet
Author: User
Tags keras
This article is to share the content is Python3 Python code to package exe file method, the need for friends can refer to

Basic configuration:

Anaconda 3 4.2.0 (python3.5)

Attention:

1, the code is stored in the full English directory;

2, the computer Steward and other security software temporarily shut down (because the published EXE file belongs to the executable file, the computer butler may think that the published file is virus, automatically deleted)


The following are the steps:

1. Write the Python code and store it in the full English directory:

Import kerasfrom keras.models import sequentialimport numpy as Npimport pandas as Pdfrom keras.layers import Denseimport R Andomimport Matplotlib.pyplot as Pltfrom tensorflow.examples.tutorials.mnist import input_datafrom tkinter Import Filedialogimport tkinter.messagebox #这个是消息框, Key file_path of dialog box = filedialog.askdirectory () mnist = Input_data.read_data_ Sets (File_path, validation_size=0) #随机挑选其中一个手写数字并画图num = Random.randint (1, Len (mnist.train.images)) img = Mnist.train.images[num]plt.imshow (Img.reshape (), cmap= ' Greys_r ') plt.show () X_train = Mnist.train.imagesy_  Train = Mnist.train.labelsx_test = Mnist.test.imagesy_test = mnist.test.labels#reshaping The X_train, Y_train, X_test and Y_test to conform to MLP input and output Dimensionsx_train = Np.reshape (X_train, (X_train.shape[0],-1)) X_test = Np.resh Ape (X_test, (X_test.shape[0],-1)) Y_train = Pd.get_dummies (y_train) y_test = pd.get_dummies (y_test) #performing One-hot Encoding on target variables for train and Testy_train=np.array (Y_train) Y_test=np.array (y_test) #defining model with one input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0 .4 and 1 output layer [ten #neurons]model=sequential () model.add (dense (784, input_dim=784, activation= ' Relu ')) Keras.layers.core.Dropout (rate=0.4) model.add (Dense (10,input_dim=784,activation= ' Softmax ')) # Compiling model using Adam Optimiser and Accuracy as metricmodel.compile (loss= ' categorical_crossentropy ', optimizer= "Adam", metrics=[' Accuracy '] # fitting model and performing Validationmodel.fit (X_train, Y_train, epochs=20, batch_size=200, Validation_ Data= (X_test, y_test)) Y_test1 = PD. DataFrame (Model.predict (X_test, batch_size=200)) Y_pre = Y_test1.idxmax (axis = 1) result = PD. DataFrame ({' Test ': y_test, ' Pre ': Y_pre}) tkinter.messagebox.showinfo (' Message ', ' completed! ')

2, through the command line, according to Pyinstaller

Pip Install Pyinstaller

3. command line package file

First, switch the path to the directory where Python code is located, execute the statement:

Pyinstaller-f-W xxx.py

4, waiting for packaging to complete, will generate a build folder and a dist folder, exe executable file in the Dist folder, if the program refers to a resource, you want to put the resource file in this exe correct relative directory.

5. Run exe file.

Sometimes it is wrong to run the file, you need to copy the folder shown to the directory where the exe file is located


Run successfully!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.