PyAutoGui module not working in PyCharm IDE - python

I am trying to use PyAutoGui's locateCenterOnScreen() function in PyCharm. However, it always throws an error even though it works in VS Code. I am running on an M1 Macbook Air w/ macOS Monterey. I have PyAutoGui version 0.9.53 installed. Does anyone know why it does this?
Traceback (most recent call last):
File "/Users/username/Desktop/Files/Programming/Projects/Auto Mining Tool/main.py", line 3, in <module>
x, y = pyautogui.locateCenterOnScreen('image.png')
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyautogui/__init__.py", line 175, in wrapper
return wrappedFunction(*args, **kwargs)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyautogui/__init__.py", line 207, in locateCenterOnScreen
return pyscreeze.locateCenterOnScreen(*args, **kwargs)
File "/Users/andrewwalker/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 413, in locateCenterOnScreen
coords = locateOnScreen(image, **kwargs)
File "/Users/andrewwalker/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 372, in locateOnScreen
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
File "/Users/andrewwalker/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 477, in _screenshot_osx
im = Image.open(tmpFilename)
NameError: name 'Image' is not defined

I can tell you why, but I'm not smart enough to know how to fix it.
The reason seems to stem from updates. I just ran into this problem today myself, I typically build projects and then never update modules etc.
Did you update modules prior to this happening?
It seems they've changed function names, and other modules that were built by other people either were not updated, or have import errors, resulting in pyscreeze and pyautogui not playing nice together.

Related

PyAutoGUI Script no longer working - "windll.gdi32.getpixel failed"

My script has been working fine for the past month or so (up til yesterday), but as of today when I run it, it returns the following:
Traceback (most recent call last):
File "C:\...\main.py", line 39, in <module>
pxFish = pyautogui.pixelMatchesColor(int(fishLocX[i]), int(fishLocY[i]), (76, 76, 76), tolerance=50)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pyscreeze\__init__.py", line 589, in pixelMatchesColor
pix = pixel(x, y)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pyscreeze\__init__.py", line 610, in pixel
raise WindowsError("windll.gdi32.GetPixel failed : return {}".format(color))
OSError: windll.gdi32.GetPixel failed : return -1
I haven't made any changes to the code or to my system, so I have no idea why it'd just suddenly stop working. Help would be appreciated.
Also, somewhat bizarrely, my other functionally identical script still works (i.e. uses pixelMatchesColor, pyautogui click etc.).
Cheers.
Edit: It started working again out of nowhere. Didn't do anything at all. Epic. Very cool.

Python stopped working after the first instance of "Segmentation fault (core dumped)" error message upon executing some previously valid code

Today, I noticed that my python code while executing, produces the following upon execution without actually execute the code:
Segmentation fault (core dumped)
After checking out this page (Error: Segmentation fault (core dumped)), I noticed that my python has actually crashed rather than something else.
Here is the error message first I received before my later attempts resulting in the above error message (This is not reproducible and I was lucky to save it somewhere):
Traceback (most recent call last):
File "my_code.py", line 984, in <module>
cf = ax12.tricontourf(x, y, z, levels, cmap=cm.get_cmap(cmap, len(levels)-1), norm=norm)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 8025, in tricontourf
return mtri.tricontourf(self, *args, **kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/tri/tricontour.py", line 282, in tricontourf
return TriContourSet(ax, *args, **kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/tri/tricontour.py", line 40, in __init__
ContourSet.__init__(self, ax, *args, **kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/contour.py", line 846, in __init__
kwargs = self._process_args(*args, **kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/tri/tricontour.py", line 51, in _process_args
tri, z = self._contour_args(args, kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/tri/tricontour.py", line 85, in _contour_args
**kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/tri/triangulation.py", line 164, in get_from_args_and_kwargs
triangulation = Triangulation(x, y, triangles, mask)
File "/home/username/anaconda3/lib/python3.6/site-packages/matplotlib/tri/triangulation.py", line 55, in __init__
self.triangles, self._neighbors = _qhull.delaunay(x, y)
RuntimeError: Error in qhull Delaunay triangulation calculation: precision error (exitcode=3); use python verbose option (-v) to see original qhull error.
By the way, I am using python3 from anaconda on an Ubuntu linux. Is there any way to narrow down the issue by downgrading to some previous versions of python? However, it doesn't seem that python itself has been upgraded recently after checking it through:
conda info python
Thanks,
Are you using virtualenv? You can sometimes get weird errors running Python in virtualenv after updating System Python, usually a segfault or dynamic linking errors. From what I understand this happens because the python executable inside the virtualenv is of different version than the libpython.so that it's trying to load from the system. To work around this issue, you will need to recreate the virtualenv.

Tweepy issues with twitter bot and python

I have a few twitterbots that I run on my raspberryPi. I have most functions wrapped in a try / except to ensure that if something errors it doesn't break the program and continues to execute.
I'm also using Python's Streaming library as my source of monitoring for the tags that I want the bot to retweet.
Here is an issue that happens that kills the program although I have the main function wrapped in a try/except:
Unhandled exception in thread started by <function startBot5 at 0x762fbed0>
Traceback (most recent call last):
File "TwitButter.py", line 151, in startBot5
'<botnamehere>'
File "/home/pi/twitter/bots/TwitBot.py", line 49, in __init__
self.startFiltering(trackList)
File "/home/pi/twitter/bots/TwitBot.py", line 54, in startFiltering
self.myStream.filter(track=tList)
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 445, in filter
self._start(async)
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 361, in _start
self._run()
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 294, in _run
raise exception
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 263, in _run
self._read_loop(resp)
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 313, in _read_loop
line = buf.read_line().strip()
AttributeError: 'NoneType' object has no attribute 'strip'
My setup:
I have a parent class TwitButter.py, that creates an object from the TwitBot.py. These objects are the bots, and they are started on their own thread so they can run independently.
I have a function in the TwitBot that runs the startFiltering() function. It is wrapped in a try/except, but my except code is never triggered.
My guess is that the error is occurring within the Streaming library. Maybe that library is poorly coded and breaks on the line that is specified at the bottom of the traceback.
Any help would be awesome, and I wonder if others have experienced this issue?
I can provide extra details if needed.
Thanks!!!
This actually is problem in tweepy that was fixed by github #870 in 2017-04. So, should be resolved by updating your local copy to latest master.
What I did to discover that:
Did a web search to find the tweepy source repo.
Looked at streaming.py for context on the last traceback lines.
Noticed the most recent change to the file was the same problem.
I'll also note that most of the time you get a traceback from deep inside a Python library, the problem comes from the code calling it incorrectly, rather than a bug in the library. But not always. :)

Python - Getting error whenever I try to run program "Module cannot be found"

I'm trying to do this little tutorial http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod,_part_1
A little ways down the page right before it says moving around it says to test what you have so far. I'm using Pycharm and this is my first time using an outside library or whatever you call it.
This is what I have so far and it is exactly what is in their example:
import libtcodpy as libtcod
#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20 #20 frames-per-second maximum
libtcod.console_set_custom_font('terminal.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
libtcod.sys_set_fps(LIMIT_FPS)
while not libtcod.console_is_window_closed():
libtcod.console_set_default_foreground(0, libtcod.white)
libtcod.console_put_char(0, 1, 1, '#', libtcod.BKGND_NONE)
libtcod.console_flush()
Whenever I run it I get this error.
Traceback (most recent call last):
File "D:\Programming\Project 1\Rogue Like\libtcodpy.py", line 57, in <module>
_lib = ctypes.cdll['./libtcod-mingw.dll']
File "C:\Python34\lib\ctypes\__init__.py", line 426, in __getitem__
return getattr(self, name)
File "C:\Python34\lib\ctypes\__init__.py", line 421, in __getattr__
dll = self._dlltype(name)
File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Programming/Project 1/Rogue Like/firstrl.py", line 1, in <module>
import libtcodpy as libtcod
File "D:\Programming\Project 1\Rogue Like\libtcodpy.py", line 60, in <module>
_lib = ctypes.cdll['./libtcod-VS.dll']
File "C:\Python34\lib\ctypes\__init__.py", line 426, in __getitem__
return getattr(self, name)
File "C:\Python34\lib\ctypes\__init__.py", line 421, in __getattr__
dll = self._dlltype(name)
File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
Thanks
I'm assuming you also copied libtcod-VS.dll or libtcod-mingw.dll to the project directory, not just libtcodpy.py. And also SDL.dll and a arial10x10.png. If not, go back and look at the Setting it up instructions again.
But if you have, this isn't really your fault, it's theirs.
libtcodpy.py tries to import the libtcod-VS.dll or libtcod-mingw.dll DLL from the current working directory. You can see that from this line:
_lib = ctypes.cdll['./libtcod-mingw.dll']
So, if the current working directory happens to be anything other than the directory that libtcodpy.py is in, it won't find them there.
This is a silly thing to do. If you do what the Choice of code editor section suggests and always run the script from a console (a "DOS prompt"), it will work (as long as you're always running it without an explicit path), but they really shouldn't be relying on that.
Still, that's obviously the simplest fix: Run the program from the console, the way they're expecting you to, instead of from PyCharm.
Alternatively, you can configure PyCharm to run your project with the project directory as your working directory.
There are a few ways to set this, but the one you probably want is the Run/Debug Configurations dialog (which you can find under Edit Configurations… on the Run menu). Open that dialog, open the disclosure triangle to Defaults, click Python, then look for "Working directory:" on the right. Click the … button and pick your project directory (or wherever you put libtcod-VS.dll or libtcod-mingw.dll).
Or you can edit libtcodpy.py to make it look for the DLL alongside of itself, rather than in the current working directory. There are only 4 small changes you should need.
First, in the middle of the import statements near the top, if there's no import os, add it.
Next, right after the import statements, add this:
modpath = os.path.dirname(os.path.abspath(__FILE__))
Now search for the two lines that start with _lib = ctypes.dll (or just look at the line numbers from the tracebacks) and change them as follows:
_lib = ctyles.cdll(os.path.join(modpath, 'libtcod-mingw.dll'))
_lib = ctyles.cdll(os.path.join(modpath, 'libtcod-VS.dll'))
I've just been struggling with the same problem myself, though I'm using Emacs and Python 2.7.
What solved the problem for me was installing a 32-bit python instead of a 64-bit python. The .dlls in libtcod are 32-bit, and 64-bit python on Windows isn't compatible with 32-bit .dlls.
Also, you might want to check if libtcod is compatible with python 3. I've found two places where the subject is discussed, but I can't tell if libtcod-1.5.1 is compatible with the later 3.xs.
I'd also suggest trying to run the samples_py.py in the libtcod folder to test these two problems, as if that runs it's your folder setup or path, rather then your version of python.

Fixing PyMel import error "pop from empty list"?

Im using PyMel to write up some tools, but as of yesterday my PyMel modules won't source, due to an error that I get during import:
import pymel.core as pm
# pymel.core : Updating pymel with pre-loaded plugins: OpenEXRLoader, DirectConnect, mayaHIK, ikSpringSolver, Mayatomr, decomposeMatrix, tiffFloatReader, VectorRender, studioImport, mayaCharacterization, rotateHelper, MayaCryExport22012-x64, Substance, MayaMuscle, fbxmaya, ik2Bsolver #
# pop from empty list
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel-1.0.0-py2.6.egg\pymel\core\__init__.py", line 250, in <module>
# _installCallbacks()
# File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel-1.0.0-py2.6.egg\pymel\core\__init__.py", line 248, in _installCallbacks
# _pluginLoaded( plugin )
# File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel-1.0.0-py2.6.egg\pymel\core\__init__.py", line 79, in _pluginLoaded
# _factories.cmdlist[funcName] = _factories.cmdcache.getCmdInfoBasic( funcName )
# File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel-1.0.0-py2.6.egg\pymel\internal\cmdcache.py", line 212, in getCmdInfoBasic
# synopsis = lines.pop(0)
# IndexError: pop from empty list #
any ideas on how to fix this? I downloaded the newest version of PyMel, checked the install guide if I left anything out (i used method 2), but i still have no idea what the problem is.
Thx,
Nils
Alright, so after trying to re-install most of my programs I simply fixed the cdmcashe.py from where the error comes from - I ddnt originally want to do this because I wasnt sure what I might break, but after over 2 weeks of trying things out I just went for broke:
If anyone else has this problem, all u need to do is open your cmdcache.py and add an if test to line 212
if lines:
That'll just fix it.
yeah. simple as that. Imagine the head->desking session I had after doing that.

Categories

Resources