ホームページ  >  記事  >  バックエンド開発  >  Python コードを exe ファイルにパッケージ化する python3 メソッド

Python コードを exe ファイルにパッケージ化する python3 メソッド

不言
不言オリジナル
2018-04-09 11:56:206863ブラウズ

この記事では、python3 が Python コードを exe ファイルにパッケージ化する方法について説明します。必要な友人は参照してください

基本構成:

Anaconda 3 4.2.0 (python3.5)

注:

1 . コードはすべて英語のディレクトリに保存されます。

2. コンピューター マネージャーなどのセキュリティ ソフトウェアが一時的に閉じられます (リリースされた exe ファイルは実行可能ファイルであるため、コンピューター マネージャーはリリースされたファイルがウイルスであると判断し、自動的に削除される可能性があります)。


具体的な手順は次のとおりです:

1. 記述された Python コードをすべて英語のディレクトリに保存します:

import keras
from keras.models import Sequential
import numpy as np
import pandas as pd
from keras.layers import Dense
import random
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from tkinter import filedialog
import tkinter.messagebox #这个是消息框,对话框的关键
file_path = 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((28, 28)), cmap='Greys_r')
plt.show()

x_train = mnist.train.images
y_train = mnist.train.labels
x_test = mnist.test.images
y_test = mnist.test.labels

#reshaping the x_train, y_train, x_test and y_test to conform to MLP input and output dimensions
x_train = np.reshape(x_train, (x_train.shape[0], -1))
x_test = np.reshape(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 test
y_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 [10 #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 metric
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
# fitting model and performing validation
model.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. コマンドラインから、pyinstaller

pip install pyinstaller

3 を実行します。 . コマンドラインパッケージングファイル

まず、Pythonコードが配置されているディレクトリにパスを切り替え、ステートメントを実行します:

pyinstaller -F -w xxx.py

4.パッケージ化が完了するまで待ちます。 、ビルド フォルダーが生成され、dist フォルダーに exe 実行可能ファイルが含まれます。プログラムがリソースを参照する場合、リソース ファイルは exe の正しい相対ディレクトリに配置される必要があります。

5. exeファイルを実行します。

ファイルの実行時にエラーが発生する場合があります。この場合は、下の図に示されているフォルダーをexeファイルがあるディレクトリにコピーする必要があります


正常に実行されました!

関連する推奨事項:

Python のフォルダーのパッケージ化方法 (zip、tar、tar.gz など) のまとめ

Python パッケージ化ツール (py2exe) の紹介

以上がPython コードを exe ファイルにパッケージ化する python3 メソッドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。