0

I have created a mathematical model in python. I want to make a standalone python executable for my model. The python script uses several python libraries like numpy, pandas, pymc3. I tried to make a python executable using pyinstaller and also auto-py-to-exe. In both cases, it failed to create the executable. The error occurs while importing the pymc3 module (theano library). My primary model is dependent on pymc3 module. I cannot bypass this module. Could anyone please help me to sort out this issue?

Here is the error message I get when I execute the .exe file,

WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
c:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:621: MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
  exec(bytecode, module.__dict__)
Traceback (most recent call last):
  File "sample.py", line 46, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 621, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pymc3\__init__.py", line 11, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 621, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pymc3\stats.py", line 20, in <module>
  File "site-packages\pkg_resources\__init__.py", line 481, in get_distribution
  File "site-packages\pkg_resources\__init__.py", line 357, in get_provider
  File "site-packages\pkg_resources\__init__.py", line 900, in require
  File "site-packages\pkg_resources\__init__.py", line 786, in resolve
pkg_resources.DistributionNotFound: The 'scipy' distribution was not found and is required by the application
[13796] Failed to execute script sample

Here is the python script,

class modelFit:
    # Initialises the attributes
    def __init__(self, x, y):
        self.x = x
        self.y = y

    # Performs the Bayes estimation
    def coeffEstimation(self):
        X = np.array(self.x)
        Y = np.array(self.y)

        with pm.Model() as linearRegModel:
            # Define priors
            #regCoeff = pm.HalfNormal('regCoeff', sd = 20, shape=X.shape[1])
            regCoeff = pm.Uniform('regCoeff', lower = 0, upper = 100, shape=X.shape[1])

            # Standard deviation
            sigma = pm.HalfNormal('sigma', sd = 5)

            # Estimate of mean
            mean = pm.math.dot(X, regCoeff)

            # Likelihood estimate
            likelihood = pm.Normal('Y_estimated', mu = mean, sd = sigma, observed = Y)

            # Sampler
            step = pm.NUTS()

            # Posterior distribution
            linearTrace = pm.sample(draws = 500, chains = 1,
                                    tune = 500, nuts_kwargs=dict(target_accept=0.95))

        with open('Summary.txt', 'w') as f:
            print(pm.summary(linearTrace).round(3), file=f)
            diverging = linearTrace['diverging']
            print('Number of Divergent Chains: {}'.format(diverging.nonzero()[0].size), file=f)
            divergingPer = diverging.nonzero()[0].size / len(linearTrace) * 100
            print('Percentage of Divergent Chains: {:.1f}'.format(divergingPer), file=f)

        print('Bayesian Inference Done! Exporting the results, Please hold on!')
        return(linearTrace)


import theano.tensor.shared_randomstreams
import scipy
import pymc3 as pm
import numpy as np
import pandas as pd
dataLocation = './input/'
inputData = pd.read_excel(dataLocation + 'input.xlsx')
sizeInputData = inputData.shape
x = inputData.iloc[:,1:sizeInputData[1]]
y = inputData.iloc[:,0]


# Executes the Bayes method
linReg = modelFit(x, y)
regTrace = linReg.coeffEstimation()
  • Pyinstaller sometimes works better when you specifify dependencies. Excplicitly import theano and other dependencies and overall narrow down the imports to what you actually end up using. (e.g. import pandas as pd -> from pandas import read_excel) – mik Sep 20 '19 at 10:00
  • @mik Now I face some issues with compilation. Here is the error message. `Exception: ('Compilation failed (return status=1): C:\\Users\\Vimal\\AppData\\Local\\Theano\\compiledir_Windows-10-10.0.17763-SP0-Intel64_Family_6_Model_142_Stepping_10_GenuineIntel-3.7.4-64\\tmp_yb6bb2e\\mod.cpp:1:10: fatal error: Python.h: No such file or directory. #include . ^~~~~~~~~~. compilation terminated.\r. ', '[InplaceDimShuffle{x}(TensorConstant{0.0})]') [1820] Failed to execute script sample` – Vimalathithan D Sep 25 '19 at 08:37

0 Answers0