Issues with pyinstaller and pyproj - python

I'm trying to do an standalone application with pyinstaller. The executable has just build fine, but when I´m trying to do some operations with functions integrated on library pyproj, the executable crashes.
The script runs fine on Pycharm, so I think that the problem is that pyinstaller is not linking with some kind of library of pyproj.
May I have to do something special with spec file or another thing to specify pyproj on the standalone application built with pyinstaller?
This is the error that Ihave obtained:
Traceback (most recent call last): File "<string>", line 6, in
<module> File "C:\pyproj\build\main\out00-PYZ.pyz\pyproj", line 343,
in __new__ File "_proj.pyx", line 85, in _proj.Proj.__cinit__
(_proj.c:1190)
RuntimeError: no system list, errno: 2
This is my "main.py"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyproj
print pyproj.__version__
p=pyproj.Proj(init='EPSG:4326')
Thanks in advance

The problem is that when using pyproj with PyInstaller, pyproj can not find the data files that are in the library folder.
The solution is to create a hook file, which will specify where the data files, so you can link them with our executable.
hook-pyproj.py
from PyInstaller.hooks.hookutils import collect_data_files
datas = collect_data_files('pyproj')
The hook file can be located on "hooks" folder on Pyinstaller installation or using the order --additional-hooks-dir, specifying a folder in which will be located "hook-pyproj.py"

Just threading on the previous answer, since 2014 there has been some refactoring on PyInstaller and here is the correct import line for the hook file above :
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('pyproj')

from PyInstaller.hooks.hookutils import collect_data_files
datas = collect_data_files('pyproj')
This didn't worked for me. There were some errors in the executable again.
But I found in another thread that the problem can be solved with this:
from mpl_toolkits.basemap import pyproj as pyproj
pyinstaller seem to have problems to integrate the pyproj module itself, but basemap includes pyproj and is not ignored by pyinstaller.
Just for update

Related

DLL load failed, module not found?

I have a folder with a file "main.py" and a file "_test.pyd" (note .pyd). The file "main.py" looks like this:
import _test
I get the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import _test
ImportError: DLL load failed while importing _test: The specified module could not be found.
Why is this error coming up? Cheers.
Note: I was given this code by others, and it works for the original authors, so I'm not sure what's wrong with me/my machine.
Update: Running os.path.isfile('_test.pyd') returns True, so I don't think it's a problem with the path
You should append the path of the folder which contains the imported module before import.
Code:
import os
import sys
sys.path.append(os.path.realpath(os.path.dirname(__file__)))
import _test # noqa: E402
EDIT:
Other ideas:
Adding __init__.py file to the related director.
Checking the PyInit_foo() function in .pyd file.
If the Python finds the .pyd file, it will attempt to call PyInit_foo() to initialize it
Update Following posts from people experiencing similar issues, I tried downgrading my Python version (from 3.8.4rc1 to 3.5.4) and the import now works correctly. No clue why. I guess the .pyd file was written in that version of Python (I'm not the author of the file), but still I'm clueless as to what the exact origin of the problem is.
I've been through this error and what I found after a lot of investigation:-
issue was in Opencv==4.5.1 build from source with cuda and flag cuda_with_fast_math=on
I just rebuild OpenCV and disable
cuda_with_fast_math
and it works for me.

How to import my python file in another file in visual studio code

I am using python/selenium in visual studio code. I am trying to import my another python class driverScript which resides in executionEngine module and in the file DriverScript. I have imported as below:
import driverScript from executionEngine.DriverScript
It is producing error:
Traceback (most recent call last):
File "c:/Selenium/Selenium-Python Framework/RK_Practice/Tests/mainTest.py", line 5, in <module>
from executionEngine.DriverScript import driverScript
ModuleNotFoundError: No module named 'executionEngine'
How can I import correctly? Your help is very much appreciated.
If the script you are wishing to import is not in the current directory, you may want to take this approach:
import sys
sys.path.insert(1, '/path/to/script/folder')
import driverScript from executionEngine.DriverScript
If your python file is on the same level in dir, then you can import just by calling:
import filename
If your python file is inside another folder, then you need to create a blank __init__.py file that will help python to understand it's a package. Then you can import that as follows:
from folderName import filename
Depends on where executionEngine is supposed to come from. If it's from a package installable via a package manager such as pip or Anaconda, looks like it's not properly installed. If you installed it yourself, you probably need to add the directory containing executionEngine to your PYTHONPATH, so the Python interpreter can find it. This can be done in the VSCode environment files. See the PYTHONPATH section in https://code.visualstudio.com/docs/python/environments

ImportError: cannot import name RAND_egd

I've tried to create an exe file using py2exe. I've recently updated Python from 2.7.7 to 2.7.10 to be able to work with requests - proxies.
Before the update everything worked fine but now, the exe file recently created, raising this error:
Traceback (most recent call last):
File "puoka_2.py", line 1, in <module>
import mLib
File "mLib.pyc", line 4, in <module>
File "urllib2.pyc", line 94, in <module
File "httplib.pyc", line 71, in <module
File "socket.pyc", line 68, in <module>
ImportError: cannot import name RAND_egd
It could be probably repaired by changing options in setup.py file but I can't figure out what I have to write there. I've tried options = {'py2exe': {'packages': ['requests','urllib2']}}) but with no success.
It works as a Python script but not as an exe.
Do anybody knows what to do?
EDIT:
I've tried to put into setup.py file this import: from _ssl import RAND_egd
and it says that it can't be imported.
EDIT2: Setup.py:
from distutils.core import setup
import py2exe
# from _ssl import RAND_egd
setup(
console=['puoka_2.py'],
options = {'py2exe': {'packages': ['requests']}})
According to Google, it seems to be a very rare Error. I don't know exactly what is wrong but I found a workaround for that so if somebody experiences this problem, maybe this answer helps.
Go to socket.py file and search for RAND_egd. There is a block of code (67th line in my case):
from _ssl import SSLError as sslerror
from _ssl import \
RAND_add, \
RAND_status, \
SSL_ERROR_ZERO_RETURN, \
SSL_ERROR_WANT_READ, \
SSL_ERROR_WANT_WRITE, \
SSL_ERROR_WANT_X509_LOOKUP, \
SSL_ERROR_SYSCALL, \
SSL_ERROR_SSL, \
SSL_ERROR_WANT_CONNECT, \
SSL_ERROR_EOF, \
SSL_ERROR_INVALID_ERROR_CODE
try:
from _ssl import RAND_egd
except ImportError:
# LibreSSL does not provide RAND_egd
pass
Everything what you have to do is to comment the 5 lines:
#try:
#from _ssl import RAND_egd
#except ImportError:
## LibreSSL does not provide RAND_egd
#pass
I don't know why it raises the ImportError because there is a try - except block with pass so the error should not being raised but it helped me to successfully run the exe file.
EDIT: WARNING: I don't know whether it could cause some problems. I experienced no problems yet.
Experienced the same problem.
Solved the problem by removing directories 'dist' and 'build' created by py2exe when it was run on previous version of Python.
Seems like py2exe doesn't rebuild all the files every time. And obviously doesn't catch the fact of Python version changing.
Finally you have a mix of files generated with different versions of Python in your 'dist' directory.
My setup.py is pretty simple:
from distutils.core import setup import py2exe
setup(console=['xxxxxx.py'])
In my case problem was in two installations of Python27: x86 and x64. Only x86 version was in %PATH%, but pip installation script was using files from x64 installation for some reason. Solution was: remove x64, cause I don't really need it.
I found a way to solve it. This might be caused by old version of socket.pyc.
My solutions is edit socket.py, add a space to anywhere and delete then. And then run your setup.py again which will generate new socket.pyc.
Now the problem is solved.
I just remove socket.pyc under c:\Python27\lib, and run py2exe again. The error gone.
I have changed the python version from 2.7.12 to 2.7.9 and problem gone.
It will replace the python files but leave you packages as it is.
Good Luck.

win32com DLL load failed

I'm trying to add win32com to Python 2.7. After looking at this, I added the directory with the _init file (Python27\Lib\site-packages\win32com) but I still get it. I went so far as to try to add a bunch of different folders to the path that seem to have to deal with win32com but I still get the error. If it knows where the file is and I added that folder to PYTHONPATH, why is this happening? I'm using PyDev with Eclipse Juno. My code:
import win32com.client
print("hello world")
when I try to run this, i get this error
Traceback (most recent call last):
File "C:\Users\Daniel\EclipseWorkspace\PhotoScript\src\scriptLaunch.py", line 1, in <module>
import win32com.client
File "C:\Python27\Lib\site-packages\win32com\__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found.
in my case typing in cmd:
python C:\Python27\Scripts\pywin32_postinstall.py -install
cmd Windows
I hope this helps
Try installing ActivePython, it includes win32com:
The Python for Windows Extensions (PyWin32 version 214).
The interface to the Win32 API (win32api).
The interfaces to Win32 COM (win32com and win32comext).
The Pythonwin Development Environment.
*ActivePython is fully binary compatible with python.org Python builds to ensure that 3rd-party binary extensions just work*
Try to install python for windows extensions:
https://sourceforge.net/projects/pywin32/files/pywin32/Build%20210/

Windows XP GTK App Glib Import Error

I am trying to run my GTK app on Windows XP and I am having troubles with an import. I have installed the following as needed and recommended:
python-2.6.msi
gtk2-runtime.2-16.6.exe
pycairo-1.8.6.exe
pygobject-2.20.0
pygtk-2.16.0.exe
pyserial-2.5.exe
However, I am getting the following error when running my script:
Traceback (most recent call last):
File "C:\python26\app\src\start.py", line 22, in <module>
import gtk
File "C:\Python26\Lib\site-packages\gtk-2.0\gtk\__init__.py", line 30, in <mod
ule>
import gobject as _gobject
File "C:\Python26\Lib\site-packages\gtk-2.0\gobject\__init__.py", line 26, in
<module>
from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
File "C:\Python26\Lib\site-packages\gtk-2.0\glib\__init__.py", line 22, in <mo
dule>
from glib._glib import *
ImportError: DLL load failed: The specified module could not be found.
GTK is installed into C:\gtk\bin and is setup in my System PATH.
Am I missing something obvious here?
Any help would be appreciated.
Thank-you kindly.
Andy
I was getting the same issue - I'm not sure why, but I suspect it has something to do with some of the install/lookup paths. I tried adding all sorts of stuff to my path, but nothing seemed to work.
What I did to fix it was to uninstall python et. al. and reinstall using the Python (x,y) package. It's a fairly large download, but it has tons of tools, including several (matplotlib, numpy, scipy, IPython, etc) that I needed/wanted.
You could install pygtk bundle found in this page:
http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.22/
where you should find the "all in one" installer suited to your version.
It solved the problem to me without having to install Py(x,y), which seems to be a very usefull scientific environment for windows (I use linux and install all these packages from script).

Categories

Resources