Python packaging tool Pyintealler packaging py file is windows exe file process and stepping on pit record + practical example

Posted by korion on Thu, 27 Jan 2022 02:03:49 +0100

Python packaging tool Pyintealler packaging py file is windows exe file process and stepping on pit record + practical example

catalogue

Python packaging tool Pyintealler packaging py file is windows exe file process and stepping on pit record + practical example

install

Model building code

Packing and pit stepping records

install

#Install Pyinstaller

#A packaging tool similar to pyinstall is Freeze py2exe Pyintealler. The author uses pyinstaller here

pip install PyInstaller

perhaps

pip install pyinstaller

#Package the program

# you_model.py is the python program code you need to package

pyinstaller -F you_model.py

#PyInstaller bundles a Python application and all its dependencies into a single package.

Model building code

#Load the required packages and libraries

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
import pickle
import os
# from sklearn.externals import joblib

#Load data

#Set as_frame model, so the loaded data is in pandas dataframe format, otherwise the model is in numpy array format;

# Load the iris data
iris = datasets.load_iris(as_frame = True)
# Create a matrix, X, of features and a vector, y.
X, y = iris.data, iris.target

type(X),type(y)
(pandas.core.frame.DataFrame, pandas.core.series.Series)

#

df = pd.concat([X,y],axis = 1)

df.head()

df.iloc[:,:-1].head()

# write data to the file dataset CSV for subsequent testing;

df.to_csv('dataset.csv',index=0,sep=',',encoding='utf-8')

#Logistic regression classifier construction, model saving and loading module

# Train a naive logistic regression model
clf = LogisticRegression(random_state=0)
clf.fit(X, y)



import pickle
# save the model we just trained
s=pickle.dumps(clf)
f=open("model_A.pkl",'wb')
f.write(s)
f.close()


f=open("model_A.pkl",'rb')
model=pickle.loads(f.read())
model.predict(X)

#Prediction program (preprocessing test data, feature engineering, supplement and other operations, loading model for reasoning), and then package the program

# prediction.py

#prediction

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
import pickle
import os

def inference_process(base_dir):
    """
    inference data processing and engineering
    
    """
    file_name = os.listdir(base_dir)[0]
    # encoding
    data = pd.read_csv(base_dir + "\\" + file_name)

    # Reasoning data preprocessing process;
    # Data cleaning and feature engineering;
    # Data supplement and data transformation;
    
    
    return data


if __name__ == "__main__":


    #  Raw data processing
    data_dir = r"dataset"  
    # Path to original table data
    data = inference_process(data_dir)  
    # Call handler
    
    # Model call
    f=open("model_A.pkl",'rb')
    model=pickle.loads(f.read())
    
    #The results are predicted and output to CSV
    y_pred = model.predict(data.iloc[:,:-1])  
    print(y_pred)
    # Result prediction
    data['prediction'] = y_pred
    data.to_csv('with_prediction.csv', index=0,sep = ',')
    
    print("done!")
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1
 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]
done!

Packing and pit stepping records

#Open terminal in anaconda under windows

#Note: cd d: does not work. You need to use d:

#After entering, use the cd command;

#Run pyinstaller - f prediction py 

#In case of coding error, remove Chinese characters in prediction

#Don't worry here. You have to wait for a while. The time may vary according to the number and size of packages you download;

#So as long as you don't report an error, just wait a minute;

#The following error occurred

win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryEx', 'the system cannot find the specified file.')

#It is suspected that the temporary file in the original build interferes with the new build. Continue to build after deletion, and the same error still exists;

#Check on stackoverflow and csdn, and you can talk about everything. Finally, the method of reducing the version is adopted successfully

# pip install pyinstaller==3.5

# pyinstaller -F prediction.py

#The packaged file structure is as follows:

#Package generated prediction The EXE file is stored in the dist directory

#Double click prediction Exe file

#The following error occurred:

ModuleNotFoundError: No module named 'scipy.spatial.transform._rotation_groups'

#Add in the main function:

#import scipy.spatial.transform._rotation_groups

#Then the following problems arise:

# ModuleNotFoundError: No module named 'scipy.special.cython_special'

#It feels like a bottomless pit,,,,

# sklearn.utils._cython_blas

# ModuleNotFoundError: No module named 'sklearn.utils._cython_blas'

#Then came sklearn utils._ weight_ Vector error

# ModuleNotFoundError: No module named 'sklearn.utils._weight_vector'

#The final packaging code is as follows:

#The input function is added to prevent the pop-up window from exiting quickly;

input("input:")

#prediction

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
import pickle
import os

def inference_process(base_dir):

    """
    inference data processing and engineering
    
    """
    
    file_name = os.listdir(base_dir)[0]
    #encoding
    data = pd.read_csv(base_dir + "\\" + file_name)
    
    return data


if __name__ == "__main__":

    import scipy.spatial.transform._rotation_groups
    import scipy.special.cython_special
    import sklearn.utils._cython_blas
    import sklearn.utils._weight_vector
    data_dir = r"dataset"
    
    data = inference_process(data_dir)
    
    f=open("model_A.pkl",'rb')
    model=pickle.loads(f.read())
    
    y_pred = model.predict(data.iloc[:,:-1])
    print(y_pred)
    
    data['prediction'] = y_pred
    data.to_csv('with_prediction.csv', index=0,sep = ',')
    
    print("done!")
    #to keep the process
    input("input:")

Then try to package and build in the following ways:

pyinstaller --hidden-import scipy.spatial.transform._rotation_groups --hidden-import scipy.special.cython_special --hidden-import sklearn.utils._cython_blas --hidden-import sklearn.utils._weight_vector --onefile prediction.py

OK,

BINGO

reference resources: http://www.pyinstaller.org/

Reference: use pyinstaller to package py files into exe files

Reference: how to switch drive letters on the windows command line

reference resources: Switch drive letter under Windows

Reference: package Python 3 project into exe executable program

Reference: Python installer

Reference: failed to package with pyinstaller and reported error win32ctypes pywin32. pywintypes. Error: (1920, 'LoadLibraryExW', 'the system cannot access this file.')

Reference: pit encountered by python pyinstaller packaging tool

Reference: pyinstaller win32ctypes pywin32. pywintypes. error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

reference resources: Pyinstaller win32ctypes.pywin32.pywintypes.error: (1920, 'LoadLibraryExW', 'System cannot access the file')

reference resources: No module named 'scipy.spatial.transform._rotation_groups after compile python script with pyinstaller

reference resources: No module named 'scipy.spatial.transform._rotation_groups after compile python script with pyinstaller

Topics: Python Linux Machine Learning Deep Learning Data Mining