Import error while the application is running in another Pc - python

I created an executable on Windows platform by cx_freeze, the exe file run successfully in my PC, it works also in another PC but when I try to use some features in my application, I find this error in my log file:
File "C:\Users\xxxx\venv\mpetools\lib\site-packages\pandas\io\pytables.py", line 448, in init
This is the block of the error in pytables.py:
try:
import tables # noqa
except:
#ImportError as ex: # pragma: no cover
raise ImportError('HDFStore requires PyTables, "{ex}" problem '
'importing'.format(ex=str(ex)))
PS: The pytables.pyc and the folder tables exist in my build folder.
My PC:
OS:Windows 10 64bit
Python:3.6.8
Miniconda3: 4.7.12
cx_freeze: 6.0
The other PC:
OS:Windows 10 64bit
How can I resolve this problem please?

Related

Pyinstaller to package app hidden-import warning/exceptions

So, I've had some trouble setting up my application through Inno setup Compiler, at first I assumed it was a problem within Inno itself but on further inspection I think it is my actual exe. I am able to create a working exe file that runs my program properly but only on my own pc. I am able to create the setup file that also works through Inno setup but it only works on my own pc. I have sent both the actual exe file and the Inno setup file to another computer and downloaded it there and ran it, both meet the same "Fatal Error: failed to run script tk_app.py". Therefore, the problem must be that I have not been able to pavkage the app properly with pyinstaller.
My prgoram has 5 files in total (all in the same folder): main.py, file1.py, file2.py file3.py, tk_app.py
All of them importing each other and using python libraries. I know that pyinstaller supports librarires such as pandas, tkinter and many more without needing the --hidden-impoort= command and that it will pick up all files within the program if there are files that are importing each other.
So I use the file tk_app.py (which contains my tkinter UI and imports main.py which then goes onto import file1.py which import another file so on)
The pyinstaller command line I use to make the exe is as follows:
PS C:\Users\ripta\Desktop\CODING\CSV_Upload> pyinstaller -w --hidden-import=bs4 --hidden-import=fake_useragent --hidden-import=urllib.prase --hidden-import=urllib.request --hidden-import=os --hidden-import=pandas.io.parsers --icon=trademark_icon.ico --onefile tk_app.py
My question is, will pyinstaller tell me when it needs a given --hidden-import='name' when running becuase it doesn not throw up any errors and does produce a spec file, a build folder and a dist folder containing the exe file.
I have noticed that it throws up WARNINGs or Exceptions (also not sure why it mentions django as I do not import or use it at all inthe application) :
59182 INFO: Loading module hook 'hook-django.db.backends.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... 61711 WARNING: Hidden import "django.db.backends.__pycache__.base" not found! 61712 INFO: Loading module hook 'hook-django.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... Traceback (most recent call last): File "<string>", line 21, in walk_packages File "c:\users\ripta\appdata\local\programs\python\python36-32\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
Or show Hidden-import not found (Of imports I have no idea about):
149329 WARNING: Hidden import "pkg_resources.py2_warn" not found! 149330 WARNING: Hidden import "pkg_resources.markers" not found!
The fact that the script fails to run on any other computer besided my own leads me to think that it must be lacking a dependency that is only found on my computer, therefore I am not using pyinstaller correctly but am not too sure where exactly I am making the mistake.
First I've uninstalled Setuptools and reinstalled it with specific version. Then, I've imported pkg_resources.py2_warn as hidden import :
pip uninstalled setuptools
pip install --upgrade 'setuptools<45.0.0'
pyinstaller --hidden-import='pkg_resources.py2_warn' tk_app.py
It worked for me.

Problem Creating One File exe with pyinstaller and pysqlcipher

I'm trying to create a one file .exe to run on any windows machine but I've hit a problem with pysqlcipher. I've gone back to some basic code that just creates a simple database with a key, on my dev machine all works fine whether I use the python file or the compiled exe. I seem to be missing a library, path or both? I've tried adding vaious items using --add-data but have spent hours and made no progress. Here is the basic bit of python:-
from pysqlcipher3 import dbapi2 as sqlite
import os
import sys
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
conn = sqlite.connect('test.db')
c = conn.cursor()
c.execute("PRAGMA key='password'")
c.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()
When I run the exe on a different windows 10 PC I get this error
Traceback (most recent call last):
File "testdb.py", line 1, in
File "c:\users\xxx\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
File "site-packages\pysqlcipher3-1.0.3-py3.8-win-amd64.egg\pysqlcipher3\dbapi2.py", line 33, in
ModuleNotFoundError: No module named 'pysqlcipher3._sqlite3'
[9248] Failed to execute script testdb
The error references the path on my dev PC and also refers to line 33 in dbapi2.py which is:-
from pysqlcipher3._sqlite3 import *
I have tried adding various files when running pyinstaller but I am making no progress, I'm sure its nothing simple but need help please.
I encountered a similar problem a while back and one thing that worked for me is adding the sqlite3 dll in the spec file of pyinstaller. It is also possible to add it from command line. I had tried many other things before such as re-creating a conda environment and rebuilding my python package but that did not work. In your spec file where you add the pyinstaller binaries please try something like.
binaries=[('C:\\Users\\myname\\newfolder\\sqlite3.dll','.')]
You can download the DLL from https://www.sqlite.org/download.html depending on windows or linux platform . Once you add the dll and recompile it should be fine. I think this error is primarily happening as dbapi2 requests the sqlite3 dll which in my case atlease was missing from the DLL folder. I just downloaded and added it in my spec file, but you can also try to add it to the hookspath or ENV folder.

Installing OCRmyPDF on Windows Server 2016 - Can't find liblept.dll. Is editing Path safe?

I'm trying to import ocrmypdf on my company's client's Windows Server 2016 Build 14393 computer using Python 37-32. When I import the library, in a Jupyter Notebook, it is unable to locate leptonica by using ctypes.utility.find_library().
Ocrmypdf is a Linux-developed Python 3 package. Per the documentation (https://ocrmypdf.readthedocs.io/en/latest/introduction.html) it does not support Windows. The suggested workarounds are a docker container and Windows Subsystem for Linux.
I would rather not use a docker container as neither I nor my coworkers are very experienced with it. I am unable to use wsl as it is not available for my build of Windows Server 2016 (see the troubleshoot subsection: https://learn.microsoft.com/en-us/windows/wsl/install-on-server)
This discussion (find_library() in ctypes) states that you can point ctypes.utility.find_library to the needed library file by editing the environment Path variable to be a folder which includes it. Conveniently, Tesseract OCR's windows download includes liblept. Would editing the Path variable to point toward that folder be a dangerous thing to do?
Edit: I tried adding the path to Tesseract-OCR's folder on my laptop's environment Path and restarted Anaconda, etc. ocrmypdf still gave the same error.
A closer read of that discussion brought up the point that find_library operates differently on Windows. A read of the documentation (https://docs.python.org/2.5/lib/ctypes-finding-shared-libraries.html) states that "On Windows, find_library searches along the system search path, and returns the full pathname, but since there is no predefined naming scheme a call like find_library("c") will fail and return None." Does this mean I have to hardcode in a name to use in order to find the library?
This issue has been replicated, albeit on a different machine, here: https://github.com/jbarlow83/OCRmyPDF/issues/341. You can reproduce the issue by running the below code on a Windows machine.
!pip install ocrmypdf
import ocrmypdf
The expected result of the above code is that ocrmypdf is successfully imported in a usable form.
The result of the above code is:
OSError Traceback (most recent call last)
<ipython-input-2-a81f3474d7ad> in <module>
----> 1 import ocrmypdf
~\AppData\Local\Continuum\anaconda3\lib\site-packages\ocrmypdf\__init__.py in <module>
16 # along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
17
---> 18 from . import helpers, hocrtransform, leptonica, pdfa, pdfinfo
19 from ._version import PROGRAM_NAME, __version__
20 from .api import Verbosity, configure_logging, ocr
~\AppData\Local\Continuum\anaconda3\lib\site-packages\ocrmypdf\leptonica.py in <module>
40 logger = logging.getLogger(__name__)
41
---> 42 lept = ffi.dlopen(find_library('lept'))
43 lept.setMsgSeverity(lept.L_SEVERITY_WARNING)
44
OSError: cannot load library '<None>': error 0x57
I have been able to get this working Windows 10 by updating the path and it works fine. I used msys2 to install it, hence, the path name. Update to point where your liblept-5.dll is located.
if os.name == 'nt':
os.environ['PATH'] = os.environ.get("PATH", "") + ';C:\\msys64\\mingw64\\bin'

ver.2 PyGreSQL ERROR: from _pg import * ImportError: DLL load failed: The specified module could not be found

I have the same problem that was discussed here, but I haven't credit to comment an answer so I start new question.
I have in PATH way to libpq.dll (C:\PostgreSql\lib) but it doesn't solve this problem.
Using Python 2.7.9 32-bit, PostgreSQL 8.4, Win 8
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pg
File "C:\Python27\lib\site-packages\pg.py", line 21, in <module>
from _pg import *
ImportError: DLL load failed: The specified module could not be found.
I was also facing the same issue on Win 8. First time I had installed PostgreSQL in "C:\Program Files" and also set environment PATH to point to PostgreSQL folder. I was suspecting permission issue for "C:\Program Files" folder.
I was able to fix this issue by following the steps as mentioned below.
Uninstalled PostgreSQL
Re-installed PostgreSQL in "C:\PostgreSQL"
Note that I have not installed PostgreSQL into "C:\Program Files" folder this time
Set the environment PATH C:\PostgreSQL\9.4;C:\PostgreSQL\9.4\bin
Also ensure that _pg.pyd exist in C:\Python27\Lib\site-packages
I got the same error on Win10 with Python 3.5 and Python 3.8, both are 64bit, and Postresql 12, also 64bit. DLL locations "c:\Program Files\PostgreSQL\12\bin" and "c:\Program Files\PostgreSQL\psqlODBC\bin" were added to the PATH but it caused another error:
"from _pg import * ImportError: DLL load failed: The operating system cannot run %1."
Then I've checked what is going on with Process Monitor from Sysinternals and found that libpg.dll was looking for other DLLs. Finally, the following files:
libpq.dll
libssl-1_1-x64.dll
libcrypto-1_1-x64.dll
were copied from "c:\Program Files\PostgreSQL\12\bin" folder to:
"c:\Users...\AppData\Local\Programs\Python\Python35"
"c:\Users...\AppData\Local\Programs\Python\Python38"
folders and now "import pg" works fine in both versions of Python.
I faced just the same issue; just to try my chance before doing what you say, I changed the PATH environment variable: I directly specified the whole directory path of libpq.dll OF THE ODBC DRIVER OF PostgreSQL (there are other "libpqdll"s as well in other relevant directories) which takes place in my Windows10 here: C:\Program Files\PostgreSQL\psqlODBC\bin .. and the problem is solved.. before, my PATH specification was C:\Program Files\PostgreSQL and I think the driver looked for the dll files directly here and either 1) when couldnt find directly in this specific diretory (by disregarding the subdirectories), 2) or came cross other "libpq.dll"s which didnt work for the driver, it gave the error message.. so here I come to the conclusion that it looks for the dll file OF THE ODBC DRIVER in PATH env.varible, so I needed to specify in the PATH environment variable directly the directory of the PostgreSQL's ODBC driver I used, psqlODBC .. for future needs I wanted to write those details:)

Importing PyVISA via Py2exe

My script file test.py is:
try:
from visa import *
except Exception as ex:
print ex
raw_input()
My setup.py file is:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
I then run:
python setup.py py2exe
If I then launch test.exe on my pc I see no issues but if I copy it to a new pc with no prior python installs I get:
[Error 126] The specified module could not be found
I don't know how to get the full traceback to print. Has anyone else seen this issue?
Running Python 2.7, PyVisa 1.4. Both machines are Win7.
Turns out the problem was that the visa libraries that pyvisa relies on weren't installed on the test pc. These libraries don't come included with pyvisa.

Categories