Pynput only works in Pycharm - python

I'm making a Tetris clone that runs in terminal and I'm using the Pynput Keyboard Listener to listen for user input. Everything works fine when I run it in Pycharm (be it by pressing the "run" button or when I launch it with $ python3 main.py from Pycharm's built-in terminal. However, when I open a "standalone" terminal and launch the program inside it, the Listener doesn't do anything, even though it seems to be running when I run for thread in threading.enumerate(): print(thread.name) in the script (same output in both terminals).
Pycharm is using the default Python interpreter (/usr/bin/python3.8). Running pip list returns the same information when run in both terminals. Running the program as root doesn't change the behaviour in either terminal. Neither does running it with the absolute path of the interpreter, $ /usr/bin/python3.8 main.py.
I have tried $ pip install --force-reinstall pynput along with updating pynput, but nothing has worked.
I am running KDE Plasma (Ubuntu LTS) and using Python 3.8.10.
EDIT: I have tried running the program on a Windows machine and it worked fine both in Pycharm and in PowerShell.

Related

Pipenv run does not execute py file. Error reading broker pipe. VScode

I am trying to run the following simple python script called 'main.py', through a pipenv virtualenv:
print("started.")
Executing pipenv run ./main.py --verbose (in pipenv venv) does not print anything to the console.
Through VS code, running pipenv run ./main.py --verbose outputs the following:
That is to say, the python file seemingly does not run, expected 'started.' to be printed to the console, recieved nothing printed and program terminating.
This behaviour is also present when I attempt to run it through cmd, note the error produced with the --verbose flag:
I can run it sucessfully through just using python, however I really want to use pipenv to manage packages ect. I guess I'm mostly just curious at to:
Why does this error occur?
What does the following error, caused by running it with --verbose flag, mean?
[5772:0107/202720.999:ERROR:broker_win.cc(56)] Error reading broker pipe: The pipe has been ended. (0x6D)
How do I fix the issue?
I have reinstalled Pyhton, pipenv, and vscode - but have recieved the same results. Running the 'pipenv run' command both within the shell and outside the shell have the same results.
EDIT:
A Visual Studio Code issue
In the post above, after runing pipenv run main.py, vscode would always open. Whats stranger is, once vscode is uninstalled, the command works as expected! So an additional question of mine is,
Why does vscode make this issue happen/what's the fix for being able to code using vscode and running pipenv run?
see bellow for what occurs when running pipenv run main.py with vscode initally closed:
This screenshot is after ~5mins of no action in iether the vscode console or the cmd console. The program never exited, and closeing the cmd also closed vscode.
pipenv run main.py means open the file in the ide you chose in the pipenv environment.
In this way, it just opened the file instead of run python file.
You need to use command python main.py to run it.

Why is pyautogui not working when running from terminal but fine when running from VSC’s terminal?

I’m trying to figure out why this very simple script only works when I run the command in VS Code’s terminal and not work when I open a normal terminal window (on my Mac), CD to the project and run the script from there.
The script:
import pyautogui
def switch_to_vsc():
print("switching")
pyautogui.keyDown("command")
pyautogui.press("space")
pyautogui.keyUp("command")
pyautogui.typewrite("visual studio code")
pyautogui.press("enter")
#
# pyautogui.typewrite("open -a Visual\ Studio\ Code")
# pyautogui.press("enter")
print("start")
switch_to_vsc()
print("end")
When running python3 filename.py from VSC’s terminal window, everything works correctly (pressing the keys, writing ‘visual studio code’, printing ‘start’, ‘end’, etc), but when running it from the terminal, pyautogui commands seem to not run, and it just prints ‘start’, ‘switching’ and ‘end’.
Why is this happening? I tried running the script with sudo, etc too, and I don’t know what’s going on. I ran python3 -m pip install pyautogui on the non-VSC terminal before running python3 filename.py too.
If I run pip show pyautogui on either the VSC terminal or separate terminal window, it shows the following:
Name: PyAutoGUI
Version: 0.9.53
Summary: PyAutoGUI lets Python control the mouse and keyboard, and other GUI automation tasks. For Windows, macOS, and Linux, on Python 3 and 2.
Home-page: https://github.com/asweigart/pyautogui
Author: Al Sweigart
Author-email: al#inventwithpython.com
License: BSD
Location: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
Requires: mouseinfo, pygetwindow, pymsgbox, pyobjc, pyobjc-core, pyscreeze, PyTweening
And pip --version on both:
pip 21.3.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
And that is after following the answer from here: Import "flask" could not be resolved from source Pylance (reportMissingModuleSource)
Judging by the CMD Space shortcut, I assume you're on Mac.
On Mac, you need to allow your terminal to control you computer:
Preferences > Privacy > Accessibility
then add Terminal in your list like so:
You should also have AEServer there, since it allows Apple Events to be performed programatically, which pyautogui does in some cases.
Another note, instead of writing all those lines for CMD+space, you can do it in one line as follows:
pyautogui.hotkey("command", "space", interval=0.2)
Hope this helps.

Every time I open or switch to IDLE it spams Q

I'm on Windows, using WLS Ubuntu on terminal. Got python3 installed. Installed idle through
sudo apt-get install idle3
Every time I open IDLE through typing 'idle' in the terminal, or alt tab to IDLE, or run a .py file through IDLE, it starts spamming the letter Q. I wasn't able to reproduce this issue in any other program.
I tried reinstalling IDLE, to no avail.
Is there a fix for this? I want to keep using IDLE but this is driving me mad!

VSCode suddenly started executing Python scripts as a subprocess

Normally when I would click on "Run" button in VSCode for a Python script it would activate the currently selected virtual environment and simply call python <script_name.py> in the terminal and it all worked fine.
Now all of a sudden every time I try to run a script what is does is instead call a subprocess via conda like so:
conda run -n <environment_name> --no-capture-output --live-stream python <script_name.py>
And this new version is causing some issue because for whatever reason conda refuses to recognise some of the packages as having been installed. The code still works fine when I manually type the run command in the terminal but I want the old behaviour to be back.
Anyone knows how to fix this?
Input: Ctrl + Shift + P .
Enter: Terminal: select default profile .
Change the default to CMD .
Maybe this can help you.

how do you run parallel code on Win10 terminal

In lessons we've been creating and running parallel programs on a virtual machine running Linux and we've been running python programs directly though the terminal with the command line
mpi4exec -n N python my_file.py
when we moved to a mac suit we had to run the command line
export PATH=/Users/admin/anaconda3/bin:$PATH
first before mpi4exec was a recognised commmand. After this the file would run fine.
My question is firstly, what does the mac line actually mean/do and secondly, is there a Win10 equivalent that allows me to run python code directly though my PC terminal?
yes, you can run python code same as that of linux in temrinal/command prompt.
for this you have to set the python path to the window environmental variable path by yourself or you can install the anaconda which do everything, install python, python-packages, spyder ide .

Categories