How to create a standalone python executable with pymc3 package?

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?

I am not at all experienced with creating executables - that is an interesting use case! What sort of error message are you seeing? I worry that you might find better help from the pyinstaller or auto-py-to-exe communities…

Here is the error message I get when I execute the converted .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 (sample.py)

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()

It looks like the environment is missing scipy. Can you make sure it works without pymc3/theano?

Thank you.

Without pymc3 module, I could able to convert the script to standalone executable. It runs fine without any issues.

Problem comes only while importing pymc3.

Now, I could get rid of the import issues,

pkg_resources.DistributionNotFound: The 'scipy' distribution was not found and is required by the application
[13796] Failed to execute script sample

But now there is some issue with compilation. Could you please help me in this regard?

Thank you.

Here is the error message,

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__)

You can find the C code in this temporary file: C:\Users\Vimal\AppData\Local\Temp\theano_compilation_error_tyojr99y
Traceback (most recent call last):
  File "sample.py", line 66, in <module>
  File "sample.py", line 15, in coeffEstimation
  File "site-packages\pymc3\distributions\distribution.py", line 46, in __new__
  File "site-packages\pymc3\model.py", line 833, in Var
  File "site-packages\pymc3\model.py", line 1557, in __init__
  File "site-packages\pymc3\distributions\transforms.py", line 95, in apply
  File "site-packages\pymc3\distributions\distribution.py", line 56, in dist
  File "site-packages\pymc3\distributions\transforms.py", line 125, in __init__
  File "site-packages\pymc3\model.py", line 1274, in __init__
  File "site-packages\pymc3\distributions\continuous.py", line 224, in logp
  File "site-packages\theano\tensor\var.py", line 69, in __ge__
  File "site-packages\theano\gof\op.py", line 615, in __call__
  File "site-packages\theano\tensor\elemwise.py", line 482, in make_node
  File "site-packages\theano\tensor\elemwise.py", line 438, in get_output_info
  File "site-packages\theano\gof\op.py", line 670, in __call__
  File "site-packages\theano\gof\op.py", line 955, in make_thunk
  File "site-packages\theano\gof\op.py", line 858, in make_c_thunk
  File "site-packages\theano\gof\cc.py", line 1217, in make_thunk
  File "site-packages\theano\gof\cc.py", line 1157, in __compile__
  File "site-packages\theano\gof\cc.py", line 1624, in cthunk_factory
  File "site-packages\theano\gof\cmodule.py", line 1189, in module_from_key
  File "site-packages\theano\gof\cc.py", line 1527, in compile_cmodule
  File "site-packages\theano\gof\cmodule.py", line 2396, in compile_str
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 <Python.h>.           ^~~~~~~~~~. compilation terminated.\r. ', '[InplaceDimShuffle{x}(TensorConstant{0.0})]')
[1820] Failed to execute script sample