ImportError when using cx_Freeze with scipy - python-3.x

I'm trying to use cx_Freeze to generate a .app from a python project. Generally I have it working, but some of my modules which depend on scipy have an import error when executed:
No module named '_csr'
under the build folder I see a file:
scipy.sparse.sparsetools._csr.so
and watching the output of the build command seems to suggest that it's copying csr:
$ python3 setup.py bdist_mac | grep csr
m scipy.sparse.csr /usr/local/lib/python3.3/site-packages/scipy/sparse/csr.py
m scipy.sparse.sparsetools._csr /usr/local/lib/python3.3/site-packages/scipy/sparse/sparsetools/_csr.so
m scipy.sparse.sparsetools.csr /usr/local/lib/python3.3/site-packages/scipy/sparse/sparsetools/csr.py
? _csr imported from scipy.sparse.sparsetools.csr
? os.path imported from NIF_WRF.util.StopPow, distutils.file_util, matplotlib.backends.backend_tkagg, matplotlib.cbook, numpy.core.memmap, numpy.distutils.command.scons, os, pkg_resources, pkgutil, scipy.lib.blas.scons_support, scipy.lib.blas.setup, scipy.lib.lapack.scons_support, scipy.linalg.setup, scipy.sparse.csgraph.setup, scipy.sparse.linalg.dsolve.setup, scipy.sparse.linalg.eigen.arpack.setup, scipy.sparse.linalg.isolve.setup, scipy.sparse.sparsetools.bsr, scipy.sparse.sparsetools.coo, scipy.sparse.sparsetools.csc, scipy.sparse.sparsetools.csgraph, scipy.sparse.sparsetools.csr, scipy.sparse.sparsetools.dia, scipy.special.setup, shutil, sysconfig
? scipy.lib.six.moves imported from scipy.integrate.quadrature, scipy.interpolate.interpolate, scipy.interpolate.polyint, scipy.linalg.special_matrices, scipy.misc.common, scipy.optimize.anneal, scipy.optimize.linesearch, scipy.optimize.nonlin, scipy.sparse.base, scipy.sparse.compressed, scipy.sparse.coo, scipy.sparse.csc, scipy.sparse.csr, scipy.sparse.dok, scipy.sparse.lil, scipy.sparse.linalg.eigen.lobpcg.lobpcg, scipy.sparse.linalg.isolve.lgmres, scipy.spatial.distance, scipy.special.basic, scipy.stats.stats
copying /usr/local/lib/python3.3/site-packages/scipy/sparse/sparsetools/_csr.so -> build/exe.macosx-10.8-x86_64-3.3/scipy.sparse.sparsetools._csr.so
The problem seems to be related to this other question but that user seemed to solve it by building again, which hasn't helped here. Any ideas?
UPDATE
I mucked around in the .app package contents and found that renaming scipy.sparse.sparsetools._csr.so to _csr.so solves that error (though generates another similar one for another scipy component). It seems like the cx_Freeze script is not properly naming scipy inputs.
Also, here are the versions I'm using:
cx_Freeze: 4.3.2
scipy: 0.13.0
python: 3.3.2

Related

XGBoost module not found in site-packages. Imports but doesn't work. (mac OS)

I was able to install xgboost on my mac and have it so that I can import the module without any errors. However I cannot use it. When I try to import XGBRegressor. I get the following error :
cannot import name 'XGBRegressor' from 'xgboost' (unknown location).
Some of the support for this type of problem says that there may be more than one xgboost location, and that python is selecting the incorrect xgboost, however I have not been able to find how to fix this problem.
When I print the xgboost.__file__ location, it returns 'None'. However I know its in the site-packages folder in my python path.
I have python 3.9
Maybe this.
conda install -c conda-forge xgboost=0.6a2
Then, restart Spyder and re-run!

Pyinstaller does not properly include numpy in Mac bundle

If I include numpy in my script the bundle application does not even open. However, if I run the application from the console everything is fine. So:
pyinstaller -w myScript.spec
with import numpy as np in one of the modules does not create a proper executable. However:
python3.7 myScript.py
runs without problems. Even more, if I comment the import numpy as np line the executable is created without problem. I have also used numpy in another console-only script without problems.
So, how can I make PyInstaller include numpy in the bundle app?
I checked the warn-myScript.txt file from PyInstaller and there are lots of modules from numpy.core that are not found, for example: numpy.core.sqrt.
But I have no idea where to find these modules.
I tried doing what j4n7 suggested here, but it did not work.
I am using Python3.7, numpy 1.15.4 and PyInstaller 3.4
I installed Python from the Python web page and numpy and Pyinstaller using pip.
In a different computer I installed Python3.7 from homebrew and I have the same problem
I installed miniconda and then created an environment with numpy 1.15.4, Pyinstaller 3.4 and python3.7.1. Within the environment, I can create the bundle app with no problem.
However, the bundle app goes to 600MB. I will start a new question regarding how to reduce the size of the bundle app.

cant find moduals even though sys.path searches in right place

I am working on widows10 with python3.7
I have downloaded opencv and numpy and sklearn(scikit-learn) and they are in C:\Python37\Lib\site-packages (where pip put them) and when I run
import sys
print(sys.path)
I get the output:
['', 'C:\\Python35\\Lib', 'C:\\Python35\\DLLs', 'C:\\Python35\\Lib\\site-packages', 'C:\\Users\\E6440', 'C:\\Python37\\python37.zip', 'C:\\Python37\\DLLs', 'C:\\Python37\\lib', 'C:\\Python37', 'C:\\Python37\\lib\\site-packages']
on cmd it says C:\Python37\Lib\site-packages which is one of the paths it looks in for when importing but then as soon as I run some code it gives a no module named ___ error
this is similar to the question python cant find module in sys.path but the comments in that one did not help me
edit
solved by using PYTHONPATH that was accurate to the python version
The package you're trying to import the module from may be available in the Python 3.5 installation too, but the package for Python 3.5 may be missing the module you're trying to import. Try moving the Python 3.7 paths ahead of the Python 3.5 paths in your PYTHONPATH environment variable.

Python 3.6 cx_freeze ModuleNotFoundError: No module named Tkinter

I froze a python 3.6 program with cx_freeze, and it worked just fine. But as soon as I tried to run it, I got this error message.
Does anyone know what to do? Please help!
try using
import tkinter
(small 't' instead of capital)
Try checking the dir names in the 'lib' folder (in my case it was 'build\exe.win-amd64-3.6\lib'). I had a similar issue (without the 'ImportError: DLL load failed...') and found that the 'lib' directory contained a "Tkinter" folder. Changing its name to lowercase 'tkinter' did the trick and made the .exe run just fine.
If you read the error more clearly, it stated in a comment that if importing _tkinter failed, your computer isn't configured to use tkinter. You should download tk and ttk to make it work.
And According to http://wiki.python.org/moin/TkInter :
If it fails with "No module named _tkinter" or "Tkinter", your Python configuration
needs to be modified to include this module (which is an extension
module implemented in C). Do not edit Modules/Setup (it is out of
date). You may have to install Tcl and Tk (when using RPM, install the
-devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in
the default locations, simply rerunning "make" should build the
_tkinter extension.

cx_freeze and scipy: "ImportError: cannot import name vode"

i am trying to create an executable from my python script. My script runs fine, but after freezing it, starting the .exe gives me the following error:
http://www.bild.me/bild.php?file=4663406scipyerror.png
I am using Python 3.2.3, Scipy 0.12.0b1, Numpy 1.7.0 and Matplotlib 1.2.0 (all 32bit).
Any ideas/hints on how to solve this? My guess is i have to include something manually in my freezing script, but i am running out of guesses :-(
I got it finally to work, but I am very unsatisfied with my solution:
1) copy _odepack.pyd and odepack.py from the SciPy package to my program folder
2) in odepack.py change from . import _odepack to import _odepack (otherwise ValueError: Attempted relative import in non-package is raised)
3) in my main change from scipy.integrate import odeint to from odepack import odeint
Now it is working as expected and after using cx_freeze it is still working.
Still got no idea why it would not work before :-(
Thanks ThomasK for pushing me in the right direction though :-)
I finally got around this vode-problem by specifying "scipy.integrate.vode" as an include in the cx setup-file. This resulted in a file "scipy.integrate.vode.pyd" to end up in the build folder. I am using SciPy 0.11, Python 3.2.3 and the latest cx on Windows.
But adding such a "scipy.integrate.vode" file manually to the build folder would not fix the problem for me either, even though such manual-include-fixes were needed for many other .pyd files cx could not find either (and whereby the above setup.py include-solution would not work instead)...
Thanks for sharing your distress and wisdom, would not have managed to freeze my program otherwize...

Resources