how use pyinstaller with opencv in raspbian? - python

i wrote this python script "mycv.py" in raspbian:
import cv2 as cv
and it debug and run currectly. then i use pyinstaller to make executable file.
but when run file in terminal. it has error:
Traceback (most recent call last):
File "mycv.py", line 1, in <module>
import cv2 as cv
File "/usr/local/lib/python3.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "cv2/__init__.py", line 89, in <module>
File "cv2/__init__.py", line 58, in bootstrap
File "cv2/__init__.py", line 56, in load_first_config
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
[28400] Failed to execute script mycv
Any help is appreciated

After lots of debugging, I found the following solution:
Get the path of OpenCV
import cv2
print(cv2.file) # /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.so
Add this path while compiling through pyinstaller
pyinstaller main.py -n myApp --paths="/usr/local/lib/python3.6/dist-packages/cv2/python-3.6"
I hope this helps others also

Related

Pyinstaller having issues with import of librairies

I was working with python (which I rarely do) for a simple program that macros some functionalities with certain keyboard inputs. I decided to export the script to a .exe using pyinstaller so I go ahead and run command: pyinstaller --onefile code.py and it generates the code.exe as expected. Only for my .exe to crash instantly. I got a view of the error by running it with cmd and got this
Traceback (most recent call last):
File "code.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[1284] Failed to execute script 'code' due to unhandled exception!
I'm certain there is an explanation to this so here are my imports:
from pynput import keyboard
from pynput.keyboard import Key
from pywinauto import application
from pywinauto.findwindows
import WindowAmbiguousError, WindowNotFoundError
import os
I couldn't seem to find any similar problems on here. Thank you in advance for the help.
In your .spec file, you should add all of your modules as "hidden imports", this usually fixes all module not found errors.
hiddenimport=["pynput","pywinauto"]
Then run pyinstaller with the spec file
pyinstaller my.spec
I'd also ensure you're using a virtual environment during this entire process.

"Failed to execute script " pyinstaller

I have a python code that I wanted to turn into an exe.
I used pyinstaller with : pyinstaller --onefile -w script.py
After it finished making the exe file I double clicked the file but I got "Failed to execute script".
I also tried running it from the cmd but it gives the same fatal error.
Stuff to add:
The code imports a couple of files packages including a python code I made, as well as it makes files referenced to it's location.
Is there something I'm doing wrong?
the script has those imports:
import socket
import os
from PIL import ImageGrab
import cv2
import time
import json
import myFile
I ran the code under cmd and it gives this error:
File "script.py", line 3, in <module>
from PIL import ImageGrab
ModuleNotFoundError: No module named 'PIL'
Might be unrelated but now that I tried to do pyinstaller --onefile -w client.py.
After I ran it windows defender found this inside :
after running it in the terminal in pycharm with :
pyinstaller --onefile --hidden-import=PIL --hidden-import=Pillow --hidden-import=pynput client.py
I get this error (note that i moved it from the dist directory to the main one):
Traceback (most recent call last):
File "client.py", line 7, in <module>
import myFile
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "myFile.py", line 1, in <module>
from pynput import mouse, keyboard
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 82, in backend
ImportError
[13364] Failed to execute script client
This may be due to pyinstaller not being able to properly find your dependencies and skipping some packages.
To fix any error like ModuleNotFoundError: No module named 'PIL' just add it as a hidden import:
pyinstaller --onefile --hidden-import=PIL -w script.py
For the second error this is a known issue with pyinstaller and pynput.
Find here some explanation.
The TLDR of it seems to be that you need to add --hidden-import=pynput.mouse._win32 --hidden-import=pynput.keyboard._win32 --hidden-import=pynput._util._win32 and any other sub-packages that give you errors.

I can't install any python package

When I'm trying to install any package using PyCharm terminal, I always get this error.
(venv) C:\Users\Mi\Desktop\MAIN\Coding\python>pip install PyMySQL
Traceback (most recent call last):
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\Scripts\pip-script.py",
line 11, in <module>
load_entry_point('pip==19.0.3', 'console_scripts', 'pip')()
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 489, in load_entry_point
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 2793, in load_entry_point
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 2411, in load
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 2417, in resolve
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\__init__.py", line 40, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\cli\autocompletion.py", line 8, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\cli\main_parser.py", line 12, in <module>
from pip._internal.commands import (
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\commands\__init__.py", line 6, in <module>
from pip._internal.commands.completion import CompletionCommand
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\commands\completion.py", line 6, in <module>
from pip._internal.cli.base_command import Command
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\cli\base_command.py", line 20, in <module>
from pip._internal.download import PipSession
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\download.py", line 6, in <module>
from json import json
ImportError: cannot import name 'json' from 'json' (C:\Users\Mi\Anaconda3\lib\json\__
init__.py)
But when I use pip install the library is installed, but it still doesn't work in PyCharm.
Thanks for help! And sorry for my english.
you can try to download corresponding pacages in '.tar.gz' format on this link
manually unzip it
enter the unzipped folder
install the package through the following cmd command:
python setup.py install
here I suppose you are using windows and your python is above 3
If you are continuously encontering this problem,I suggest you alter the original download channel to a mirror.
If you installed the library through terminal and Pycharm still doesn't recognize the library, I think may be you should select the correct interpreter for the project by clicking the project interpreter at the bottom right in a Pycharm project. Look at the image in this
link. Click on the Interpreter which is named just "Python 3.x", it may work then.
["3.x" as in which version of python you are using]

Struggling with imports in Python

I'm just sruggling with importing modules from nested packages in Python.
After execute command in project root directory:
$ nosetests
Unfortunatelly, i'm still getting logs like this:
======================================================================
ERROR: Failure: ImportError (No module named io_file)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/user/dev/ease-ci/easeci-core/tests/lib/io/test_io_facade.py", line 4, in <module>
from lib.io.io_file import file_load, File, file_exist, file_save, file_delete, file_change
ImportError: No module named io_file
And more and more errors like that.
Can someone tell me something help me to resolve my issue? Thanks.
Pycharm put me in error, because if I run test by green arrow, everything is ok.
I'm guessing that you are trying to install a library without pip installed because you used pycharm in a weird way.
Try adding a path to the library which you have to download and then run the import within the code. Cheers!
import tensorflow as tf
https://www.jetbrains.com/help/pycharm/absolute-path-variables.html

py2exe: run the compiled .exe, got error: type object 'pandas._libs.tslib._TSObject' has no attribute...__'

I use py2exe to compile python script. The python script runs without any error before compiling.
below is the setup.py
from distutils.core import setup
import py2exe, sys, os
import matplotlib
import numpy
from glob import glob
sys.argv.append('py2exe')
datafiles = [("Microsoft.VC90.CRT", glob(r'C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_99b61f5e8371c1d4\*.*'))]
datafiles.extend(matplotlib.get_py2exe_datafiles())
setup(windows=['Run.py'], data_files= datafiles, options={"py2exe": {"includes": ["matplotlib"]}})
In the run.py, I imported pandas. Pandas is used in a function as below
import pandas
def PossDist(Iterations, AssumptionFolder, ResultFolder, SampledResult):
SampledLoss = pandas.read_csv(SampledResult)
d= pandas.pivot_table(...)
The compile finished successfully without error.
When I run the compiled run.exe, I got error as below.
Traceback (most recent call last):
File "Run.py", line 6, in
File "xlwings__init__.pyc", line 34, in
File "xlwings\main.pyc", line 1727, in
File "xlwings\conversion__init__.pyc", line 3, in
File "pandas__init__.pyc", line 26, in
Main Features
File "pandas_libs__init__.pyc", line 4, in
File "pandas_libs\tslib.pyc", line 12, in
File "pandas_libs\tslib.pyc", line 10, in __load
File "pandas_libs\tslib.pyx", line 1514, in init pandas._libs.tslib
AttributeError: type object 'pandas._libs.tslib._TSObject' has no attribute 'reduce_cython'
Anyone have any clue?
Is there other way to let others run my python script on pc without python installed?
Thanks,

Categories

Resources