Py2Exe - Addressing NumPy and SciPy dependencies - python

Objective (End-Goal):
I want to create a stand-alone executable Python script (only one file) that includes NumPy and SciPy dependencies for my application.
Background:
From my understanding, to create an executable script in Python - there are three options that are available:
PyInstaller
Py2exe
CxFreeze
I went ahead and tried Py2exe for my development. It appears that CxFreeze does not support the single-file option (from the documentation here). I also considered the option of using PyInstaller, but ran into issues regarding missing DLLs (similar to what is found here). The issue continued to persist even after installing the Microsoft Visual C++ 2010 Redistributable Package in my laptop.
I followed the tutorial to use Py2exe here and was able to get a dummy script executable (Hello World!) working. However, I tried to re-modify the setup.py script specific to my application to include numpy and scipy dependencies (see below):
from distutils.core import setup
import py2exe,sys,numpy,scipy
sys.argv.append('py2exe')
setup(
console=['Application.py'],
options={
'py2exe': {
'includes':['numpy','scipy','scipy.integrate','scipy.special.*','scipy.linalg.*'],
'bundle_files':1,
'compressed':True
}
},
zipfile=None)
This is the resulting error I received when I tried running the script:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\pydev_run_in_console.py", line 52, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/prest/PycharmProjects/Application/setup.py", line 15, in <module>
zipfile=None
File "C:\Python34\lib\distutils\core.py", line 149, in setup
dist.run_commands()
File "C:\Python34\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
self._run()
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
builder.analyze()
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\runtime.py", line 164, in analyze
mf.import_hook(modname)
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
module = self._gcd_import(name)
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
return self._find_and_load(name)
File "C:\Users\prest\PycharmProjects\Application\venv\lib\site-packages\py2exe\mf3.py", line 337, in _find_and_load
raise ImportError(name)
ImportError: scipy.linalg.*
These are the versions that I am using relevant for my application:
Python 3.4
NumPy 1.14.5
SciPy 1.1.0
Question:
Can anyone provide any insight as to why I am receiving this error and any next steps to address this? I appreciate any input!
Thanks,
Preston

Closing - went ahead and use PyInstaller for the single-file executable. I re-modified my script to address specific dependencies (only used NumPy).

Related

Python py2exe executable attributeerror

I am trying to create an executable for a script I have already written. It will be used by a coworker who doesn't have python on their machine, so I want to create an executable to make their life easier. I'm currently just trying to get py2exe to work and create an executable for a simple script that simply prints hello world and the constant e from the math module (just to get an idea for how py2exe works).
My setup script is as follows:
from distutils.core import setup
import py2exe
import math
setup(
console=[{'script':'hello.py'}],
options={
'py2exe':{
'packages' : ['math'],
'bundle_files':1
},
},
version='1.0.0'
)
where hello.py is simply:
import math
print('hello world!')
print(f'Here is e: {math.exp(1)}')
The error comes from this stack after I call "python setup.py py2exe" in cmd
Traceback (most recent call last):
File ".\setup.py", line 5, in <module>
setup(
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\core.py", line 148, in setup
return run_commands(dist)
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\core.py", line 163, in run_commands
dist.run_commands()
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\dist.py", line 967, in run_commands
self.run_command(cmd)
File ".\Anaconda3\lib\site-packages\setuptools\dist.py", line 1214, in run_command
super().run_command(command)
File ".\Anaconda3\lib\site-packages\setuptools\_distutils\dist.py", line 986, in run_command
cmd_obj.run()
File ".\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 192, in run
self._run()
File ".\Anaconda3\lib\site-packages\py2exe\distutils_buildexe.py", line 273, in _run
builder.build()
File ".\Anaconda3\lib\site-packages\py2exe\runtime.py", line 250, in build
self.build_archive(exe_path)
File ".\Anaconda3\lib\site-packages\py2exe\runtime.py", line 490, in build_archive
base = dist_path.rsplit('\\', 1)[0]
AttributeError: 'NoneType' object has no attribute 'rsplit'
To covert a .py file to .exe you need to install auto-py-to-exe
Windows
pip install auto-py-to-exe
After it is installed, open cmd then type
auto-py-to-exe
Then specify the path of the script and the location where you want to save the .exe file.

Getting an odd error trying to compile a Python Executable

I'm attempting to use Py2exe to create a Python executable out of my program. The program definitely works and I've made sure all the libraries it uses are up to date, but when I attempt to run the command "python setup.py py2exe" to finish it up, I get an error I'm unfamiliar with.
The full output is below:
running py2exe
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
warnings.warn(
Traceback (most recent call last):
File "C:\Users\user\OneDrive\Desktop\intern\pdf\setup.py", line 11, in
setup(console=[{ "script": "tca_script.py"}],
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\distutils_buildexe.py", line 192, in run
self._run()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\distutils_buildexe.py", line 272, in _run
builder.analyze()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\runtime.py", line 172, in analyze
mf.import_package(modname)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\mf34.py", line 84, in import_package
self.import_package("%s.%s" % (name, modname))
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\mf34.py", line 84, in import_package
self.import_package("%s.%s" % (name, modname))
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\mf34.py", line 84, in import_package
self.import_package("%s.%s" % (name, modname))
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\py2exe\mf34.py", line 71, in import_package
package = self.modules[name]
KeyError: 'pandas.tests.io.excel'
Any help would be greatly appreciated.

Cx_freeze: How do I resolve conflicts caused by multiple .dylib files having the same name

I have created a python application that I now want to make into a standalone application. I am using cx_freeze from a virtualenv, which has worked before on my machine with scripts that have less dependencies.
I am currently running into an issue where cx_freeze errors when it tries to copy dylib files that are named the same to the build folder.
I am running the command:
python3 setup.py build
My setup.py file looks like:
from cx_Freeze import setup, Executable
build_options = {'packages': ["PySimpleGUI", "matplotlib", "pandas", "descartes", "geopandas", "cities_coordinates"],
#'excludes': ["PIL", "pyproj", "setuptools"],
"zip_exclude_packages": [],}
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('main.py', base=base, targetName = 'data_visualizer')
]
setup(name='data_visualizer',
version = '0.1',
description = 'Data visualization tool',
options = {'build_exe': build_options},
executables = executables)
This is the error I am getting:
...
copying /Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libproj.12.dylib -> build/exe.macosx-10.9-x86_64-3.7/libproj.12.dylib
copying /Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libjson-c.2.dylib -> build/exe.macosx-10.9-x86_64-3.7/libjson-c.2.dylib
Traceback (most recent call last):
File "setup.py", line 18, in <module>
executables = executables)
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/dist.py", line 392, in setup
distutils.core.setup(**attrs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/dist.py", line 260, in run
freezer.Freeze()
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 788, in Freeze
self._WriteModules(fileName, self.finder)
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 712, in _WriteModules
relativeSource=True,
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 231, in _CopyFile
resolvedPath=dependent_file
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 231, in _CopyFile
resolvedPath=dependent_file
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/freezer.py", line 173, in _CopyFile
sourcePath=normalizedSource, targetPath=normalizedTarget
File "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/cx_Freeze/darwintools.py", line 446, in getDarwinFile
raise DarwinException(exceptionString)
cx_Freeze.darwintools.DarwinException: Attempting to copy two files to "build/exe.macosx-10.9-x86_64-3.7/libwebp.7.dylib"
source 1: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/PIL/.dylibs/libwebp.7.dylib" (real: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/PIL/.dylibs/libwebp.7.dylib")
source 2: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libwebp.7.dylib" (real: "/Users/AidenTheJaunty/projects/test/lib/python3.7/site-packages/fiona/.dylibs/libwebp.7.dylib")
(This may be caused by including modules in the zip file that rely on binary libraries with the same name.)
If I exclude libraries that introduce a dylib that conflicts with another (see commented out excludes build option in the setup.py) then the command will run successfully, but my built application will fail because of missing dependencies.
Does anyone know of a way to get around this issue?
Some other info if it matters ->
Operating System: Mac OS Big Sur
Python Version: 3.7.8
cx_freeze version: 6.2
This same name error appears to have been reported in this cx_Freeze issue, introduced by this pull request that raises an error when copying a second file onto the path of another.
A workaround was merged to print a warning and simply use the first copy of the file rather than raising an error. As it doesn't appear to have been released yet (as of 2021-03-09, v6.5.3), I installed cx_Freeze from Git (after cloning and entering the repo, I installed it by pip install -e .), ran cxfreeze, and saw warnings such as this:
*** WARNING ***
Attempting to copy two files to '../distcx/libopenblas.0.dylib'
source 1: '/venv/lib/python3.6/site-packages/numpy/.dylibs/libopenblas.0.dylib' (real: '/venv/lib/python3.6/site-packages/numpy/.dylibs/libopenblas.0.dylib')
source 2: '/venv/lib/python3.6/site-packages/scipy/.dylibs/libopenblas.0.dylib' (real: '/venv/lib/python3.6/site-packages/scipy/.dylibs/libopenblas.0.dylib')
(This may be caused by including modules in the zip file that rely on binary libraries with the same name.)
Using only source 1.
where the conflicting file was simply skipped, allowing the build to proceed.

py2app failing with Anaconda Python 3

I am trying to build my wxPython application with Python 3 using py2app. Before I updated my code to Python 3, I was able to do this successfully in Python 2.
After fixing a few initial problems (had to sys.setrecursionlimit(2000) and downgrade to Python 3.5), I got stuck here:
Traceback (most recent call last):
File "setup_pmag_gui.py", line 23, in <module>
setup_requires=['py2app'],
File "/Users/****/anaconda/lib/python3.5/distutils/core.py", line 148, in setup
dist.run_commands()
File "/Users/****/anaconda/lib/python3.5/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/Users/****/anaconda/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 757, in run
self._run()
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 967, in _run
self.run_normal()
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 1075, in run_normal
self.create_binaries(py_files, pkgdirs, extensions, loader_files)
File "/Users/****/my_project/.eggs/py2app-0.13-py3.5.egg/py2app/build_app.py", line 1385, in create_binaries
mm.mm.run_file(runtime)
File "/Users/****/my_project/.eggs/macholib-1.8-py3.5.egg/macholib/MachOGraph.py", line 83, in run_file
ValueError: '/Users/****/anaconda/lib/libpython3.5.dylib' does not exist
/Users/****/anaconda/lib/libpython3.5m.dylib does exist, though. Is there a way to tell MachOGraph how to find this?
I am on OS X 10.12.4, using Anaconda Python 3.5. My version of py2app is 0.13.
This is what my setup.py file looks like:
from setuptools import setup
import sys
import os
directory = os.getcwd()
sys.setrecursionlimit(3000)
APP = ['programs/pmag_gui.py']
DATA_FILES = [('', ['dialogs/help_files', 'pmagpy/data_model', 'programs/images'])]
OPTIONS = {'argv_emulation': False,
'iconfile': os.path.join(directory, 'programs', 'images', 'text_x_xslfo.icns')}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
After reading the Tweaking your Info.plist py2app documentation, and this question, I tried adding to OPTIONS:
'plist': dict(PyRuntimeLocations=['/Users/****/anaconda/lib/libpython3.5m.dylib'])
But I get the same exact error message as before.
Any suggestions on how to solve this, or what documentation to look at?
I had same problem with miniconda and py2app. I could fix it by creating a symlink!
ln -s /path/to/lib/libpython3.5m.dylib /path/to/lib/libpython3.5.dylib

How to install pybrain

I am attempting to install pybrains but I am getting this error:
C:\Python34\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.4\helpers\pycharm\pycharm_setup_runner.py" C:\Users\PycharmProjects\Youtube\pybrain-pybrain-87c7ac3\setup.py
Testing started at 14:08 ...
running pycharm_test
running egg_info
writing PyBrain.egg-info\PKG-INFO
writing dependency_links to PyBrain.egg-info\dependency_links.txt
writing top-level names to PyBrain.egg-info\top_level.txt
reading manifest file 'PyBrain.egg-info\SOURCES.txt'
writing manifest file 'PyBrain.egg-info\SOURCES.txt'
running build_ext
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.4\helpers\pycharm\pycharm_setup_runner.py", line 26, in <module>
exec (fh.read(), globals(), locals())
File "<string>", line 21, in <module>
File "C:\PYTHON34\LIB\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\PYTHON34\LIB\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\PYTHON34\LIB\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python34\lib\site-packages\setuptools\command\test.py", line 138, in run
self.with_project_on_sys_path(self.run_tests)
File "C:\Python34\lib\site-packages\setuptools\command\test.py", line 118, in with_project_on_sys_path
func()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.4\helpers\pycharm\pycharm_commands\pycharm_test.py", line 18, in run_tests
testLoader=loader_class()
File "C:\PYTHON34\LIB\unittest\main.py", line 92, in __init__
self.parseArgs(argv)
File "C:\PYTHON34\LIB\unittest\main.py", line 139, in parseArgs
self.createTests()
File "C:\PYTHON34\LIB\unittest\main.py", line 146, in createTests
self.module)
File "C:\PYTHON34\LIB\unittest\loader.py", line 146, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\PYTHON34\LIB\unittest\loader.py", line 146, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\PYTHON34\LIB\unittest\loader.py", line 105, in loadTestsFromName
module = __import__('.'.join(parts_copy))
File "c:\users\dhowarth\pycharmprojects\youtube\pybrain-pybrain-87c7ac3\pybrain\__init__.py", line 1, in <module>
from structure.__init__ import *
ImportError: No module named 'structure'
Process finished with exit code 1
Where do I find the 'Structure' module?
Anyone having the same issue?
I have followed the instructions on: https://github.com/pybrain/pybrain/wiki/installation.
what am I missing?
The Pybrain can be setup by pip install pybrain directly.
The FNN in pybrain have a bit problem of convergence and performance. As substitution, there are lots of advices on Python for AI, such as FANN (An example) and scikit-learn (for lots of ML method but not contains FNN directly).
Try not to use pip. I always prefer getting the entire folder from PyPI or github (if available) and then doing the simple python setup.py install.
Also, whenever you do some work with the packages which are under active development (or not very commonly use), please read their documentation.
If you see the documentation : Here. You will see that they use Python 2.x.
And you are clearly using Python 3.x.
Get Python 2.x and download the folder from github. Unzip it and install.
Hope it helps.
Cheers.
Try following queries:
pip install numpy
pip install scipy
pip install pybrain
OR:
download ez_setup.py and run it:
python ez_setup.py
OR:
use easy_install:
easy_install pybrain

Categories

Resources