pyautogui moveto not working(has correct coords) - python

import pyautogui
while True:
test = pyautogui.locateCenterOnScreen('test.png',region=[600,570,680,570],grayscale=False,confidence=.6)
if test:
pyautogui.moveTo(1000,1000)
print(str(test),'found',test.x,test.y)
break
The print statement is getting run(so its not that the if statement is not getting accessed)
Also the move to does work, if run outside of the while statement

With further inspection/test, visual studio code has to be run as admin in order for (moveto) to work while it's not a focused application. This said, if you are using (moveto) and visual studio code has focus, (moveto) will work.
In conclusion, run VS CODE as admin for (moveto) to work correctly.

Related

Function recall working in other IDEs but not VS Code

I wrote some very simple code:
def yo():
text = "hi there"
print(text)
print(text)
yo()
I ran this in Spyder and online compilers without error. Obviously it spits out:
hi there
hi there
But when I run this in VS Code terminal using the "Run Python file in terminal" play button I get
"SyntaxError: invalid syntax"
for line 1 (the def line).
When I type yo() into the terminal itself, I get the expected output of:
hi there
hi there
Why is do I get a different result from these? I executed other simple bits of Python in VS Code using the "play" button without issue. It goes without saying that I have the python extension and interpreter installed.
UPDATE: I restarted VS Code and now the file runs without issue. I guess "did you restart the computer" really does solve the issue sometimes...
Your function - yo(), is being defined, however Visual Studio Code does not know how to run it. To fix this, try adding the if __name__ == '__main__': clause. Here is your full code:
def yo():
text = "hi there"
print(text)
print(text)
if __name__ == '__main__':
yo()
Here is some more information about if __name__ == '__main__':
If that doesn't fix it, you must have some formatting issues or some different settings of Visual Studio Code. You could do the following things.
Make sure you're running the right file
Delete all of the code and paste it in again
Reset your Visual Studio Code settings
Make sure your settings for Tab are 4 spaces.
Disable terminal.integrated.inheritEnv in Settings
If all else fails, try these:
You should use the exit() command in the terminal to end python session. Then re-run and see if anything works.
Run your code using 'Start without debugging'.

Restarted Visual Studio Code and now my code doesn't work

print('==', end='', flush=True)
the above code give the error "SyntaxError: invalid syntax" even though it was working fine? All the variables seem to have turned white rather than blue (which is what it used to be) in Visual Studio Code
Help please?!
Here is the whole source code (it's for a hangman game): https://www.codepile.net/pile/QmJE5BYO
I have tried to run your code, except for needing to add alreadyGuessedLetters = [], it works well.
It has not given me the error message of SyntaxError: invalid syntax. So could you provide the traceback of it? Such as a screenshot. And have you tried to restart the VSCode again?
I can't see the color of your code, Have you tried to switch the color theme?
You can take the command of Developer:Inspect Editor Tokens and Scopes to check the colorization ruler of the semantic token like this:

Prevent "runfile(...)" expression in Spyder console

I'm just getting started with Python and trying to get some easy code-examples to compile. I am using the 'Spyder' Editor and everytime I run code it shows 'runfile(...)' before the actual compiled code in the console.
Is there a way to prevent this behaviour?
Try including this instead immediately prior to your code. The terminal will now return a clean code only response:
cls = lambda: print("\033[2J\033[;H", end='')
cls()
you are trying to run the code, instead go to settings, keyboard shortcuts, and look for "run selection" it will have a shortcut assigned to it
Now select all the code and use the shortcut
it will only give you in and out

Is there an interpreter for Python similar to Pry for Ruby? [duplicate]

Is there a way to programmatically force a Python script to drop into a REPL at an arbitrary point in its execution, even if the script was launched from the command line?
I'm writing a quick and dirty plotting program, which I want to read data from stdin or a file, plot it, and then drop into the REPL to allow for the plot to be customized.
I frequently use this:
def interact():
import code
code.InteractiveConsole(locals=globals()).interact()
You could try using the interactive option for python:
python -i program.py
This will execute the code in program.py, then go to the REPL. Anything you define or import in the top level of program.py will be available.
Here's how you should do it (IPython > v0.11):
import IPython
IPython.embed()
For IPython <= v0.11:
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell() # this call anywhere in your program will start IPython
You should use IPython, the Cadillac of Python REPLs. See http://ipython.org/ipython-doc/stable/interactive/reference.html#embedding-ipython
From the documentation:
It can also be useful in scientific
computing situations where it is
common to need to do some automatic,
computationally intensive part and
then stop to look at data, plots, etc.
Opening an IPython instance will give
you full access to your data and
functions, and you can resume program
execution once you are done with the
interactive part (perhaps to stop
again later, as many times as needed).
You can launch the debugger:
import pdb;pdb.set_trace()
Not sure what you want the REPL for, but the debugger is very similar.
To get use of iPython and functionality of debugger you should use ipdb,
You can use it in the same way as pdb, with the addition of :
import ipdb
ipdb.set_trace()
I just did this in one of my own scripts (it runs inside an automation framework that is a huge PITA to instrument):
x = 0 # exit loop counter
while x == 0:
user_input = raw_input("Please enter a command, or press q to quit: ")
if user_input[0] == "q":
x = 1
else:
try:
print eval(user_input)
except:
print "I can't do that, Dave."
continue
Just place this wherever you want a breakpoint, and you can check the state using the same syntax as the python interpreter (although it doesn't seem to let you do module imports).
It's not very elegant, but it doesn't require any other setup.
Great answers above, but if you would like this functionality in your IDE. Using Visual Studio Code (v1.5.*) with Python Setup:
Highlight the lines you would like to run and
right click and select Run Selection/Line in Interactive Window from the drop down.
Press shift + enter on your keyboard.
Right click on the Python file you want to execute in the file explorer and select Run Current File in Interactive Window
This will launch an interactive session, with linting, code completion and syntax highlighting:
Enter the code you would like to evaluate, and hit shift + enter on your keyboard to execute.
Enjoy Python!

How to drop into REPL (Read, Eval, Print, Loop) from Python code

Is there a way to programmatically force a Python script to drop into a REPL at an arbitrary point in its execution, even if the script was launched from the command line?
I'm writing a quick and dirty plotting program, which I want to read data from stdin or a file, plot it, and then drop into the REPL to allow for the plot to be customized.
I frequently use this:
def interact():
import code
code.InteractiveConsole(locals=globals()).interact()
You could try using the interactive option for python:
python -i program.py
This will execute the code in program.py, then go to the REPL. Anything you define or import in the top level of program.py will be available.
Here's how you should do it (IPython > v0.11):
import IPython
IPython.embed()
For IPython <= v0.11:
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell() # this call anywhere in your program will start IPython
You should use IPython, the Cadillac of Python REPLs. See http://ipython.org/ipython-doc/stable/interactive/reference.html#embedding-ipython
From the documentation:
It can also be useful in scientific
computing situations where it is
common to need to do some automatic,
computationally intensive part and
then stop to look at data, plots, etc.
Opening an IPython instance will give
you full access to your data and
functions, and you can resume program
execution once you are done with the
interactive part (perhaps to stop
again later, as many times as needed).
You can launch the debugger:
import pdb;pdb.set_trace()
Not sure what you want the REPL for, but the debugger is very similar.
To get use of iPython and functionality of debugger you should use ipdb,
You can use it in the same way as pdb, with the addition of :
import ipdb
ipdb.set_trace()
I just did this in one of my own scripts (it runs inside an automation framework that is a huge PITA to instrument):
x = 0 # exit loop counter
while x == 0:
user_input = raw_input("Please enter a command, or press q to quit: ")
if user_input[0] == "q":
x = 1
else:
try:
print eval(user_input)
except:
print "I can't do that, Dave."
continue
Just place this wherever you want a breakpoint, and you can check the state using the same syntax as the python interpreter (although it doesn't seem to let you do module imports).
It's not very elegant, but it doesn't require any other setup.
Great answers above, but if you would like this functionality in your IDE. Using Visual Studio Code (v1.5.*) with Python Setup:
Highlight the lines you would like to run and
right click and select Run Selection/Line in Interactive Window from the drop down.
Press shift + enter on your keyboard.
Right click on the Python file you want to execute in the file explorer and select Run Current File in Interactive Window
This will launch an interactive session, with linting, code completion and syntax highlighting:
Enter the code you would like to evaluate, and hit shift + enter on your keyboard to execute.
Enjoy Python!

Categories

Resources