GDAL Import Error after freezing Python 3 script - python

I am trying to freeze a Python script that contains an import from osgeo but the executable fails with an ImportError: No module named '_gdal'.
I've stripped down my script to just one line:
import osgeo
When running in Python (version 3.3.3) all is well. I've tried freezing with cx_Freeze and py2exe. Both report that there is a missing module: ? _gdal imported from osgeo (among others) but successfully freeze the script. Then the exe fails with the above ImportError.
I've tried importing _gdal in python and it works. I tried manually including the osgeo module in the freezing options but still get the same error.
There is a similar question here:
Importing GDAL with cx_Freeze, Python3.4
but maybe this isn't a cx_freeze issue because it also happens with py2exe (it has experimental support for python 3 now).
Does anybody know how to fix this?

I found a fix. I edited the osgeo\__init__.py file and modified line 13 like this: import osgeo._gdal. This works but only if the module osgeo._gdal is manually included when freezing. #Thomas K Your solution does the same thing I guess.
Note that the same modification must be applied both at osgeo.ogr (line 18) and osgeo.osr (line 18) modules if they are needed (import osgeo._ogr and import osgeo._osr respectively). This means that these must be manually included when freezing as well. My freezing command now looks like this: python cxfreeze.py module1.py --include-modules=osgeo._gdal,osgeo._ogr,osgeo._osr.
Thanks #Thomas K for your help.

Related

py2exe strange missing modules

I think this is one of the many questions about missing modules and py2exe... anyway...
PylibUty is a local package located in C:\Dati\workspaces\PythonEclipse\APyLibUty\PyLibUty and referred in every .py file with:
import sys, os
sys.path.append(os.path.join(os.path.dirname("file"), '..'))
sys.path.append("C:/Dati/workspaces/PythonEclipse/APyLibUty")
from PyLibUty.pystr import randomString, insertStr
uuid is a Python package so I normally import it
_posixshmem I am not able to find what package I should install using pip/pipenv
resource: I installed both resource and pyresource using pip/pipenv bat nothing changed
What should I install or import to solve these problems?
This is the output produced running py2exe:
I did not find a solution to understand what modules I should add or install to clear the problems of py2exe, so I searched the web to find a different tool able to make exe files from Python sources. I found cx_freeze, actually it does not produce the errors I reported so at the moment I switched to cx_freeze. I will test it better but at the moment my problem is solved.

How to manage pytests’ `tests` folder name collision with jupyter extensions

I’m in the process of writing unit tests for my software.
I wrote some helpers for my tests. For convenience I would like to use them in a jupyter notebook.
When I try to import them inside of the notebook though, I get an error.
from tests import helpers
->
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-12-d8ba72c24738> in <module>
----> 1 from tests.helpers import some_helper
ModuleNotFoundError: No module named 'tests.helpers'
Digging a little, I found out that importing tests actually imports this folder as a module:
'/my_project_path/.venv/lib/python3.8/site-packages/IPython/extensions/tests'
The folder contains test_autoreload.py and test_storemagic.py, which are tests for extensions I use.
Here’s my question, how do I properly manage this conflict? I would like to keep those extension installed, and I would like to keep the name tests for my folder, as it is the convention when working with pytest.
I installed those extensions with pip. Did I miss an option to ignore the tests when installing or something?
Thanks! :)
Not really a solution but some hints here in case they help you:
I was having the same issue today and getting a bit crazy. Not in jupyter but int the ipyton shell. As jupyter uses ipython I guess it will be the same issue.
IPython has a module called tests
within the IPython/extensions folder, and this folder is added is added to the search path when using ipython instead of python.
This are my paths from a python shell (using sys.path):
['',
'/usr/local/lib/python37.zip',
'/usr/local/lib/python3.7',
'/usr/local/lib/python3.7/lib-dynload',
'/usr/local/lib/python3.7/site-packages']
And the ones from an ipython shell:
['/usr/local/bin',
'/usr/local/lib/python37.zip',
'/usr/local/lib/python3.7',
'/usr/local/lib/python3.7/lib-dynload',
'',
'/usr/local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/site-packages/IPython/extensions',
'/home/dmontaner/.ipython']
So, if your are in iptyhon or in a notebook and you import tests or from tests import something, the interpreter will search in:
'/usr/local/lib/python3.7/site-packages/IPython/extensions'
So far all is as expected. But I also had as you my own tests module in the same directory where my ipython session was running. What should happen then is that my own tests module should be imported first. But it was not and I had the same error as you.
If I would remove the 'IPython/extensions' from the path doing sys.path.remove(.../IPython/extensions') then I was able to import my own module.
I did uninstall ipyton and ipython_genutils and, using python -m pytest in the command line (on a .py file, not a notebook) I was still having a slightly similar issue this time about dash trying to load the IPython. The weird thing is that in my project I am not using dash but flask and dash is not a dependency of pytest anyway I think.
So, I uninstalled ipyton, ipython_genutils, dash and plotly and then I could run pytest importing from my own tests module.
I reinstalled the 4 libraries again and the problem was solved! And now I can import from my tests module even from within a jupyter notebook.
I guess the message is that there is some buggy setup or dependency among all those libraries (and may be some others). Try to reinstall latest versions or recreate your virtual environment in case it helps.
Sorry that I could not figure out what was the exact problem... but I hope this helps you.
The long-term fix for these kind of naming conflicts is to put all your code, including your test code, into a project package, such that you imports look like
import my_project.tests.helpers instead of import tests.helpers.
By choosing a unique name for your project, you define your own namespace and avoid naming conflicts.

Get Python stack trace while using cx_freeze

I have a data acquisition program written in Python that I distribute to my collaboration as an executable (using cx_freeze), as I don't want to bother them with installing Python and installing all the software dependencies. The program has been working well for a year now. Recently, the program started to crash (crash, not give a scripting error, i.e., the Python virtual machine itself is crashing). So I would like to know what library is causing this problem. This problem is happening randomly, so it's difficult to systematically pinpoint the cause.
I learned about faulthandler, and I would like to use it with my cx_freeze, because I can't be sure the problem is happening due to cx_freeze itself or due to some other library.
The question: How can I produce a cx_freeze executable that will use faulthandler?
What I tried:
My current cx_freeze setup script is the following:
import sys
from cx_Freeze import setup, Executable
from GUI.Meta import *
target = Executable("Main.py",
#base = "Win32GUI",
icon = "GUI\\icon.ico",
compress=True,
targetName="Prog.exe")
setup(
name = "My Software",
version = SOFTWARE_VERSION,
description = "",
executables = [target])
I tried replacing my Executable part Main.py by Main.py -q -X faulthandler, but that didn't work. Importing faulthandler in my cx_freeze setup file with import faulthandler or from faulthandler import * didn't help.
Please advise.
Additional info: Dependencies that I'm using (in case you may know a possible cause of the problem): PySide, Sympy, Numpy, H5py, PySerial, Matplotlib
I learned that I could use procdump. It can be downloaded from here. It's a very simple program that can log stack trace. You can use it with:
C:\>procdump -ma -i c:\Dumps
and this will dump the stack trace of any program that crashes to that folder.

Using Module for different python version

The default version of python (ie, one that opens on typing "python" in command line) is 2.6 on my server. I also have 2.7 installed. How do I import a module, in this case numpy, in python2.7? When I try to import right now, it gives me an error -
ImportError: No module named numpy
Is there any workaround, apart from downloading the package and doing a build install?
I agree with the comment above. You will have to compile it separately. I once tried a hack of importing and modifying the sys.path however I ran into issues with the .so files.

CefPython3, PySide, and cx_freeze: Can't get EXE working

I recently downloaded an installed the following:
Python 3.2.5
CefPython 3
PySide - for handling the GUI of my CEF-powered app
cx_freeze - to make my app distributable
Now, the first three work just fine. If I run pyside.py in CefPython's examples directory, the app starts as it should. However, if I try to freeze the app using the instruction below, the app compiles, but with errors.
Command line instruction
C:\Python32\Lib\site-packages\cefpython3\examples\PySide>cxfreeze pyside.py --target-dir bin --base-name Win32GUI --include-modules atexit,PySide.QtNetwork
Error
Missing modules:
? _gestalt imported from platform
? _posixsubprocess imported from subprocess
? cefpython_py27 imported from __main__
? cefpython_py32 imported from __main__
? win32api imported from platform
? win32con imported from platform
? win32pipe imported from platform
Then, when I try to run the app, I get the following:
ImportError: No module named cefpython_py32
Problem here is that I can't seem to include the module in the command line instruction. cx_freeze simply cannot find it.
What do I need to do to get cx_freeze to find CefPython?
Note: cx_freeze imports all the necessary DLLs, from the Qt framework, to CefPython itself. They are all there. Which begs the question as to why it knows where the DLLs are, but not the libraries. Also note that I'm a Python newbie. I'm sure there's a simple explanation as to what I need to do. I have a feeling I need to copy something into cx_freeze's dist directory - I just don't know what that file would be. I naturally assumed it would be a .pyc file, but there are none...
Update (whilst I was compiling this question):
I also tried the following instruction:
C:\Python32\Lib\site-packages\cefpython3\examples\PySide>cxfreeze pyside.py --target-dir bin --base-name Win32GUI --include-modules atexit,PySide.QtNetwork,cefpython3
Note the added cefpython3. Now, it compiles it, but the EXE throws a cx_freeze error: Cannot get zipimporter instance Also, the above errors persist, and so I think that using cefpython3 is wrong in this regard.

Categories

Resources