Failed to execute script pyi_rth_win32comgenpy after packing with pyinstaller - python

I'm trying to pack a script that contains these external imports:
import keyboards
import win32gui
from PIL import ImageGrab
import pytesseract
I have a virtual environment set up with PyCharm, so I make sure to add the site-packages directory to the -paths option when running pyinstaller.
I am at a loss for what to do now since this error has never been fully answered. Some other resources (here and here) say to simply uninstall and then reinstall all packages relating to win32. I did that, but it's still not successful. Any ideas?

After playing around with it for about an hour, I've come to a solution. I added --hidden-import "pywin32" to the pyinstaller command, and it worked! So if uninstalling and reinstalling doesn't work, try adding "pywin32" as a hidden import.

Related

unable to import module pyautogui

I am working on OS X
Ive looked at many different threads and haven't found a solution. Maybe I haven't been installing things properly from the start either. I want to import the module pyautogui into my project but it is saying it can't find it. I did a pip install in terminal and in the pycharmterminal as well. I have found the file directory and verified that it is available. But yet I am still not able to access it in my project. On some of the threads I read something about init.py, do I need to put that in my project. If so is there code in that py file or do I just create a python file named init.py
On Mac OS and some Linux distros, you may need to type pip3 install package-name.

PyCharm OpenCV- autocomplete with import cv2.cv2, no errors with import cv2

I'm just getting started with PyCharm, python, and OpenCV, and I'm trying to set up my environment. I've installed all the necessary packages and I import OpenCV like so:
import cv2
However, this does not autocomplete and shows warnings that the method may be missing when called, BUT if I import like so:
import cv2.cv2
autocomplete does work, but running produces the following error:
Traceback (most recent call last):
File "C:/Users/dunnj/PycharmProjects/TransformApps/transformapps/blackwhite.py", line 1, in <module>
import cv2.cv2 as cv2
AttributeError: 'module' object has no attribute 'cv2'
My Configuration:
PyCharm 2021.2.3 on macOS 11.6
Python 3.9.7 running in a Virtual Environment(VE)
opencv-python 4.5.4.58 installed into the VE via pip using the PyCharm Terminal window
Steps that worked for me to get autocompletion working:
tldr: Update python interpreter settings to point to <full path to venv>/lib/python3.9/site-packages/cv2
In preferences, Select Python Interpreter
Click the setting icon ( gear on right of box that display your Python Interpreter and select Show All
A list of all your configured Interpreters is show with your current interpreter already hi-lighted.
With your interpreter still highlighted, click the Icon that shows a folder and subfolder at the top. Tool tip should say "Show Paths for Selected Interpreter.
Click the + button and add the following path:
<full path to the venv>/lib/python3.9/site-packages/cv2
The .../python3.9... will be different if you are using a different Python Version.
Click Ok until you are back to the main IDE window.
This has worked in three different Virtual environments for me so far. For two of those, I had to restart the IDE for the completions to show up. The remaining one did not require a restart and worked immediately.
Credit to ingolemo from r/learnpython. I was stuck on this for ages and it drove me mad so I'm here sharing.
My OpenCV was installed by using the wrapper opencv-python package
The sys.modules hacking that that module is doing is the source of the
problem. Pycharm doesn't exactly import modules in order to know
what's inside of them, so messing with the imports dynamically like
that confuses pycharm greatly. It's not pycharm's fault, the
opencv-python maintainer should have used a star import rather than
that messy import hack. You should be able to work around the problem
using the technique you stumbled upon. All you have to do is catch and
ignore the error under normal operation:
import cv2
# this is just to unconfuse pycharm
try:
from cv2 import cv2
except ImportError:
pass
just execute the following commands in your project working environment.
pip uninstall opencv-python
pip install opencv-python==4.5.4.60
The proposed import solution did not work for me.
I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.
I solved this way:
Select project interpreter
Click on the settings button next to it and then clicking on the Show paths for selected interpreter
added the directory containing the cv2 library (in my case in the Conda Python library path - e.g. miniconda3/lib/python3.7/site-packages/cv2/python-3.7). In general check the site-packages/cv2/python-X.X directory)
Following Workaround 2 from the JetBrains issue tracker (https://youtrack.jetbrains.com/issue/PY-54649) helped me:
In PyCharm open from menue FILE - SETTINGS
Go to PROJECT:<your_project_name> and select PYTHON INTERPRETER
Click on the gear symbol next to the interpreter path and select SHOW ALL.
Make sure the correct interpreter is selected.
Click on that icon that looks like a folder tree (on the top)
Click on the "+" icon
Select the folder where the opencv package is located
normally (if you installed it via package manager) you will find it in:
<your_project_path>\venv\Lib\site-packages\cv2
Click OK (twice)
Wait for updating skeletons
Installing Jedi solved this problem for me.
You can use pip install jedi in terminal
You can find more info about jedi here: https://pypi.org/project/jedi/
i had the same problem.
i used
import cv2 as cv2
and after that both importing meth
try
try:
import cv2.__init__ as cv2
except ImportError:
pass
If you are using virtualenv, then mark the virtualenv directory as excluded in your project structure in Pycharm project settings.
Encountered this before.
find "cv2.cp38-win_amd64.pyd" in "Lib\site-packages\cv2" path.
Copy it to "DLLs" path.
Work for system python and anaconda environments(need to do this in conda envs path)
PS.
"site-packages" path can be found by "pip --version"
"DLLs" path is located at "Lib\site-packages....\DLLs"

'ImportError: No module named pillow' in PyCharm

I'm getting an error while using PyCharm which doesn't allow me to import the pillow module even though I have it installed as a package in the project interpreter. Any help is greatly appreciated!
http://imgur.com/a/DfjC3
While the name of the package is pillow, it is a replacement for PIL and uses the PIL for the name of the base module
the usual way to use pillow is
from PIL import Image
im = Image.open("filename")
See the tutorial, and the documentation
You try to run code with default Python interpreter (/Library/Frameworks/Python.framework/Versions/3.2/bin/python3). You need to configure PyCharm to run Your code with anaconda (~/anaconda/bin/python)
And now (Like #JamesK say) read Pillow tutorial and documentation:
import PIL not import Pillow
For anybody still having trouble with this, I did the following which solved my problem.
Open up your Project Interpreter (⌘ + , on Mac).
At the bottom of this page you'll see the + symbol to the left of the anaconda logo. This will create a pop-up that allows you to search for available packages.
In this new window, search for 'Pillow'.
Click and Install Package.
You should now be able to use "from PIL import Image" or "import Pillow as pil" etc.
After running this command on your terminal
pip install pillow
and you are sure it was installed, but still having same problem of PIL module not found.
Go to your IDE and make sure existing interpreter is set to python interpreter and not anaconda

After installing NumPy in Python - I still get error: "no module named numpy"

I need to install and use the Python NumPy module (and then later the Pandas module) in order to process heavy data in Python.
I downloaded and installed ENTHOUGHT, but it wasn't what I wanted all that extra clutter of extra modules (which defeats the purpose of importing Python modules only as needed), but the uninstall did not work properly (i.e. it left garbage folders and ENTHOUGHT remnants all over my computer).
I have tried installing NumPy via EASY_INSTALL and PIP (two package managers if I understand correctly) - but with no success. Every time I try to run my program, I get the error: "no module named numpy".
I have searched the questions here and have tried to alter my ENVIRONMENT VARIABLE as per the following video, but again, no success:
https://www.youtube.com/watch?v=ddpYVA-7wq4
C:\Python34
...still the same error!
I downloaded Anaconda (with all its extra clutter and installed, but I don't like the development environment - I want my Vanilla Python IDLE to run Vanilla NumPy with no extra clutter modules...) and when I tried to again install Numpy I received a message that it was already installed with a path to:
C:\users\yoni\anaconda3\lib\site-packages
....so I ALSO added this PYTHONPATH to the ENVIRONMENT VARIABLE in hopes that it would now recognize where the NumPy installation was (currently with Anaconda3 - but I hoped to be able to import NumPy to my vanilla Python IDLE):
C:\Python34;C:\users\yoni\anaconda3\lib\site-packages
I don't find a clear answer - I see others have the same problem, and nothing is working for me. How can I finish this installation of NumPy so that it works for me when I do a simple import of module?
This is a temporary solution until you can resolve your path issue.
It will be environment specific.
import sys
sys.path.append('C:\users\yoni\anaconda3\lib\site-packages[PackageName]')
import PakcageName

Can't load pywin32 library win32gui

I'm trying to use the win32gui module included with pywin32 but I can't get it working.
I have downloaded it, built it and everything seem to be located under site-packages, I've found win32gui.pyd at site-packages/win32/win32gui.pyd but when I try to import it I get:
import pyHook, win32gui
ImportError: DLL load failed: The specified module could not be found.
Do I need to move a dll somewhere? and if so, which one?
This works:
import pywintypes
#import pythoncom # Uncomment this if some other DLL load will fail
import win32gui
I had the same issue. I added the path where pywintypes34.dll to system path and it worked.
In my case it was C:\Python34\Lib\site-packages\pywin32_system32
My guess is that win32gui depends on some DLL that is not on your system. You can download depends and see what you're missing.
However my first attempt will be try installing pywin32 from the installer, not by building it.
First check that "pywin32" module is installed in your system or not. If not installed then install it first. http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32
If the issue still persists then now for the /Lib/site-packages/pywin32_system32 and add this path to system library or add pythoncom35.dll and pywintypes35.dll to the directory which is added to a system path.
I tried to only "copy" pywin32 package once instead of installing it and it works well.
what i've done is :
Copy over all the related packages to site-packages folder
Copy pythoncom25.dll & pywintypes25.dll to c:\windows\system32 folder (you might need to change to version&system path)
Hope it helps
Oddly, this is still an issue 12 years later. I suddenly had the same problem - got a DLL not found error importing win32gui with pywin32 v303.
The answer by DSblizzard from 2011 solved the issue for me, and everything worked once I imported pywintypes prior to win32gui:
import pywintypes # Not used, but need it for win32gui to import correctly
import win32gui
Same problem, I installed from sourceforge and then I run the .exe as administrator.
I wanted a win32gui module for which I installed the pywin32 module still error " DLL load failed while importing win32gui: The specified module could not be found", I went to folder pywin32_system32 folder in my virtual environment and copied DLLs and pasted inside win32 folder.
worked for me
This seemed to be the missing piece for me:
python.exe Scripts/pywin32_postinstall.py -install
That should be run after doing a:
pip install pywin32
After running those two things, import win32gui started working for me.

Categories