import sys, os

if not os.path.exists('venv'):
  np = 'numpy-1.13.0+mkl-cp35-cp35m-win_amd64.whl'
  sp = 'scipy-0.19.1-cp35-cp35m-win_amd64.whl'

  if not (os.path.exists(np) and os.path.exists(sp)):
    print('''
Download following 2 files:
  * %s
  * %s
from:
  http://www.lfd.uci.edu/~gohlke/pythonlibs/
and place these files into:
  %s
'''.strip() % (np, sp, os.path.dirname(os.path.realpath(os.curdir))))
    sys.exit(0)


  import pip
  pip.main(['install', 'virtualenv'])

  import virtualenv
  sys.argv = ['virtualenv', 'venv']
  virtualenv.main()

  import subprocess
  subprocess.run(['venv/Scripts/pip.exe', 'install', np], check=True)
  subprocess.run(['venv/Scripts/pip.exe', 'install', sp], check=True)
  subprocess.run([
    'venv/Scripts/pip.exe', 'install',
    'pyqt5', 'pyqtgraph',
    'pyexcel', 'pyexcel-io', 'pyexcel-xls', 'pyexcel-odsr',
    'cx_Freeze'
  ], check=True)


print('Go into virtualenv')
__file__ = 'venv/Scripts/activate_this.py'
exec(open('venv/Scripts/activate_this.py').read())

import glob
from cx_Freeze import setup, Executable

# https://stackoverflow.com/questions/32432887/cx-freeze-importerror-no-module-named-scipy
# https://bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with
from cx_Freeze.finder import ModuleFinder
modname_map = {
  'scipy.lib': 'scipy._lib',
}
IncludePackage = ModuleFinder.IncludePackage
ModuleFinder.IncludePackage = lambda s, n: IncludePackage(s, modname_map.get(n, n))

build_exe_options = {
  'include_files': glob.glob('../../src/*.py'),
  'packages': [
    'numpy',
    'numpy.core._methods',
    'numpy.lib.format',
    'scipy.sparse.csgraph._validation',
    'scipy.spatial.ckdtree',
    'pyqtgraph',
    'pyexcel.plugins',
    'pyexcel.plugins.parsers',
    'pyexcel.plugins.parsers.excel',
    'pyexcel.plugins.renderers',
    'pyexcel.plugins.sources',
    'pyexcel.plugins.sources.file_input',
    'pyexcel_io',
    'pyexcel_io.database',
    'pyexcel_io.readers.csvr',
    'pyexcel_io.readers.csvz',
    'pyexcel_io.readers.tsv',
    'pyexcel_io.readers.tsvz',
    'pyexcel_io.writers.csvw',
    'pyexcel_io.readers.csvz',
    'pyexcel_io.readers.tsv',
    'pyexcel_io.readers.tsvz',
    'pyexcel_xls',
    'pyexcel_xls.xlsr',
    'pyexcel_odsr',
    'pyexcel_odsr.odsr',
    'lxml._elementpath' # required by pyexcel_odsr
  ]
}

base = None
if sys.platform == 'win32':
  base = 'Win32GUI'

sys.argv = ['build.py', 'build_exe']
setup(
  name = 'Tuna',
  options = {'build_exe': build_exe_options},
  executables = [Executable('../../src/tuna.py', base='Win32GUI')]
)

for fn in glob.glob('build/*/scipy/spatial/cKDTree.*'):
  dirname = os.path.dirname(fn)
  newname = os.path.basename(fn).lower()
  print('Rename file: %s => %s' % (fn, newname))
  os.rename(fn, os.path.join(dirname, newname))

import zipfile
with zipfile.ZipFile('tuna.zip', 'w', zipfile.ZIP_DEFLATED) as f:
  for dirpath, dirnames, filenames in os.walk(glob.glob('build/*/')[0]):
    for fn in filenames:
      path = os.path.join(dirpath, fn)
      print()

@@ -859,25 +859,27 @@
 ',%0A    '
-cx_Freeze
+pyinstaller
 '%0A  %5D, c
@@ -895,2158 +895,270 @@
 )%0A%0A%0A
-print('Go into virtualenv')%0A__file__ = 'venv/Scripts/activate_this.py'%0Aexec(open('venv/Scripts/activate_this.py').read())%0A%0Aimport glob%0Afrom cx_Freeze import setup, Executable%0A%0A# https://stackoverflow.com/questions/32432887/cx-freeze-importerror-no-module-named-scipy%0A# https://bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with%0Afrom cx_Freeze.finder import ModuleFinder%0Amodname_map = %7B%0A  'scipy.lib': 'scipy._lib',%0A%7D%0AIncludePackage = ModuleFinder.IncludePackage%0AModuleFinder.IncludePackage = lambda s, n: IncludePackage(s, modname_map.get(n, n))%0A%0Abuild_exe_options = %7B%0A  'include_files': glob.glob('../../src/*.py'),%0A  'packages': %5B%0A    'numpy',%0A    'numpy.core._methods',%0A    'numpy.lib.format',%0A    'scipy.sparse.csgraph._validation',%0A    'scipy.spa
+import shutil%0Afor name in 'build', 'dist':%0A  if os.path.exists(name):%0A    shu
 ti
-a
 l.
-ckd
+rm
 tree
-',%0A    'pyqtgraph',%0A    'pyexcel.plugins',%0A    'pyexcel.plugins.parsers',%0A    'pyexcel.plugins.parsers.excel',%0A    'pyexcel.plugins.renderers',%0A    'pyexcel.plugins.sources',%0A    'pyexcel.plugins.sources.file_input',%0A    'pyexcel_io',%0A    'pyexcel_io.database',%0A    'pyexcel_io.readers.csvr',%0A    'pyexcel_io.readers.csvz',%0A    'pyexcel_io.readers.tsv',%0A    'pyexcel_io.readers.tsvz',%0A    'pyexcel_io.writers.csvw',%0A    'pyexcel_io.readers.csvz',%0A    'pyexcel_io.readers.tsv',%0A    'pyexcel_io.readers.tsvz',%0A    'pyexcel_xls',%0A    'pyexcel_xls.xlsr',%0A    'pyexcel_odsr',%0A    'pyexcel_odsr.odsr',%0A    'lxml._elementpath' # required by pyexcel_odsr%0A  %5D%0A%7D%0A%0Abase = None%0Aif sys.platform == 'win32':%0A  base = 'Win32GUI'%0A%0Asys.argv = %5B'build.py', 'build_exe'%5D%0Asetup(%0A  name = 'Tuna',%0A  options = %7B'build_exe': build_exe_options%7D,%0A  executables = %5BExecutable('../../src/tuna.py', base='Win32GUI')%5D%0A)%0A%0Afor fn in glob.glob('build/*/scipy/spatial/cKDTree.*'):%0A  dirname = os.path.dirname(fn)%0A  newname = os.path.basename(fn).lower()%0A  print('Rename file: %25s =%3E %25s' %25 (fn, newname))%0A  os.rename(fn, os.path.join(dirname, newname))%0A%0Aimport zipfile%0Awith zipfile.ZipFile('tuna.zip', 'w', zipfile.ZIP_DEFLATED) as f:%0A  for dirpath, dirnames, filenames in os.walk(glob.glob('build/*/')%5B0%5D):%0A    for fn in filenames:%0A      path = os.path.join(dirpath, fn)%0A      print(
+(name)%0A%0Aimport subprocess%0Asubprocess.run(%5B'venv/Scripts/pyinstaller.exe', '../tuna.spec'%5D, check=True)%0Aos.makedirs('../../dist')%0Aos.rename('dist/Tuna.exe', '../../dist/Tuna.exe'
 )%0A

Update build script for native windows to use pyinstaller