Mouse, and keyboard python modules conflicting? - python

I am writing a test program which contains the mouse, and keyboard modules together, but they conflict when I run the program.
If applicable, I run Linux Mint 20.04 (Uma).
Here is my code:
import keyboard
import mouse
import time
time.sleep(2)
print("Finished part 1!")
x, y = mouse.get_position()
time.sleep(2)
print("Finished part 2!")
mouse.move(x, y)
time.sleep(2)
print("Finished part 3!")
keyboard.add_hotkey('ctrl+alt+c', mouse.move(x, y))
If I run this program normally, such as /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py in my terminal, it outputs:
Finished part 1!
Finished part 2!
Finished part 3!
Traceback (most recent call last):
File "/home/bhrz/Scripts/Python/Mouse/main.py", line 18, in <module>
keyboard.add_hotkey('ctrl+alt+c', mouse.move(x, y))
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 639, in add_hotkey
_listener.start_if_necessary()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 196, in init
_os_keyboard.init()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 109, in build_device
ensure_root()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixcommon.py", line 174, in ensure_root
raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.
When I attempt to solve that error by entering this, sudo /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py, it outputs:
Traceback (most recent call last):
File "/home/bhrz/Scripts/Python/Mouse/main.py", line 2, in <module>
import mouse
ModuleNotFoundError: No module named 'mouse'
I have looked for answers, and some were to do the above, which as you can see, has resulted in failure, and another said to do sudo su then run the script--which also failed, with the same output as above.
Please help me figure out what the problem is.
I do have unintentionally 3 versions of python installed: 2.7, 3.8.10, and 3.9.5. I personally installed Python 3.9.5. I don't use 2.7, but it was installed with the python-dev
On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.

I have found the answer to my problem. The thread: Python can't find module when started with sudo 's second answer fixed my problem.
Instead of running sudo python3(/3.8) myScriptName.py, run sudo -E python3(/3.8) myScriptName.py.
Thank you, Nima for attempting to help me!

Use sudo /bin/python3.8 /home/bhrz/Scripts/Python/Mouse/main.py as you said:
On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.
python3 has been replaced when you installed python3.9.5. So it belongs to python 3.9.5. You need to use python3.8 in order to execute your script in python 3.8.10 environment.

Related

Pycharm error with easygui: No module named 'global_state'

I am working on a script which calls easygui. This has been added to the virtual environment.
The lines involving easygui are
#import module
from easygui import *
#set message and title
msg="Hello World!"
title="Sample Program"
# a simple window showing a message, a title and a ‘Ok’ button
msgbox(msg,title)
The script throws the error below. However, it runs perfectly when I call it from the command line. Why is pycharm throwing an error, but not the command line? Thanks.
Traceback (most recent call last):
File "/Users/nickriches/PycharmProjects/pythonProject3/main.py", line 12, in <module>
from easygui import *
File "/Users/nickriches/PycharmProjects/pythonProject3/venv/lib/python3.7/site-packages/easygui/__init__.py", line 34, in <module>
from .boxes.button_box import buttonbox
File "/Users/nickriches/PycharmProjects/pythonProject3/venv/lib/python3.7/site-packages/easygui/boxes/button_box.py", line 18, in <module>
import global_state
ModuleNotFoundError: No module named 'global_state'
I had this error this afternoon (MacOS M1, running python 3.9), and this is how I fixed it.
Basically the error is caused by the absence of a properly functioning tkinter installation, even though the error is complaining about "global_state". You can work this out by tracing through the code, which I will not do here.
You can prove to yourself that there is a defect in your tkinter installation by opening python in REPL mode and typing:
"import tkinter".
If you get an error, then you are on the right track to continue below.
The solution therefore is to properly get tkinter working.
I installed tkinter on my Mac by going to https://www.activestate.com/products/tcl/downloads/ and installing it.
Then I installed the "python-tk" module by running:
"brew install python-tk".
After that, when I opened python in REPL and ran "import tkinter", things worked.

Arbitrary strings in bash throw python errors?

Something in my setup of my shell causes arbitrary strings like "krmpfl" or "u45g5svtJ7" to create a Python error:
$> krmpfl
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I would expect bash (and not python!) to throw an error of the kind "Unknown command krmpfl. Did you mean...", but any non-recognized command is for some reason passed to python. I am confused.
Does anyone have an idea on how to debug this or how to move forward? I've tried type krmpfl but this (correctly) echoes bash: type: krmpfl: not found
My setup:
Win10 using Ubuntu 18.04 within WSL
ConEmu as a console
Bash-it
Python 3.8
Click (python package) installed to simplify creating commands
If your current shell function defines a function named command_not_found_handle, bash runs that for a non-existent command rather than immediately failing with a "command not found" error. In your case, that function exists and calls /usr/lib/command-not-found, which appears to be a Python script that tries to download (or at least suggest you download) a package with apt_pkg, but you don't have that Python module installed, which leads to the Python exception.

No Python IDE will start and it will not start in the Command Prompt

When I click on my Python IDE's (IDEL, PyScripter) they will not even open. I tried typing python in the command prompt and this is what happened:
C:\>python
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.5\lib\site.py", line 548, in <module>
main()
File "C:\Python27\ArcGIS10.5\lib\site.py", line 537, in main
aliasmbcs()
File "C:\Python27\ArcGIS10.5\lib\site.py", line 469, in aliasmbcs
codecs.lookup(enc)
File "C:\Python27\ArcGIS10.5\lib\encodings\__init__.py", line 85, in search_function
norm_encoding = normalize_encoding(encoding)
File "C:\Python27\ArcGIS10.5\lib\encodings\__init__.py", line 57, in normalize_encoding`enter code here`
encoding = str(encoding, "ascii")
TypeError: str() takes at most 1 argument (2 given)
Did you recently install ArcGIS? It looks to me like ArcGIS installed a few libraries, and overwrote your site.py, but it's using code that's meant for Python3 rather than Python2.7. The str function is capable of taking in 2 arguments in Python3 but not in Python2.
To get your Python to work again, you could try deleting the entire ArcGIS10.5 directory from your computer (or temporarily moving it to your desktop and seeing if that helps). You can also try running python -S in Command Prompt to run Python without importing site.py.
To try to get ArcGIS working, you might be able to install Python3, and reinstall ArcGIS using that.
Hopefully that helps!

Ipython installation on 3.3 x64 errors

I don't have experience in downloading libraries, so any help is appreciated. I've got a fresh install of Python3.3 and am trying to get IPython for 64bit Windows 7. The IPython .exe installer ran fine and completed normally, but I can't access the program. I looked through their documentation, and tried the commands there, but
$ python setup.py install
returns invalid syntax, highlighting "setup". I thought the fix might be in the distribute library mentioned on the page, so I went to go try and get that first. However, using the script from distribute's install page revealed it's own errors, namely
Traceback (most recent call last):
File "C:\Python33\distribute_setup.py", line 541, in <module>
sys.exit(main())
File "C:\Python33\distribute_setup.py", line 537, in main
tarball = download_setuptools(download_base=options.download_base)
File "C:\Python33\distribute_setup.py", line 200, in download_setuptools
log.warn("Downloading %s", url)
File "C:\Python33\lib\distutils\log.py", line 47, in warn
self._log(WARN, msg, args)
File "C:\Python33\lib\distutils\log.py", line 30, in _log
if stream.errors == 'strict':
AttributeError: errors
I've been at it for a couple hours now, and I'm fresh out of ideas. What next?
It looks like you ran into Python bug 12967. You need to run distribute_setup.py outside of IDLE (i.e. in a command prompt).

Esky freezes after escalating permissions on windows 7

We're about to start using Esky to deploy updates to our application. On Windows 7, Esky appears to be unable to get the escalated privileges it needs to install an update.
I've narrowed it down to this simple script. It asks for escalated permissions, but will either freeze, or crash with the stacktrace below.
Script
import esky
import esky.sudo
class MyProgram(object):
#esky.sudo.allow_from_sudo()
def do_stuff(self):
pass
app = MyProgram()
sapp = esky.sudo.SudoProxy(app)
sapp.start()
sapp.do_stuff()
sapp.drop_root()
Stack Trace
$ python test.py
Traceback (most recent call last):
File "test.py", line 16, in <module>
sapp.start()
File "c:\Python27\lib\site-packages\esky\sudo\__init__.py", line 125, in start
raise RuntimeError("sudo helper process terminated unexpectedly")
RuntimeError: sudo helper process terminated unexpectedly
$ python test.py
Traceback (most recent call last):
File "test.py", line 16, in <module>
sapp.start()
File "c:\Python27\lib\site-packages\esky\sudo\__init__.py", line 140, in start
self.close()
File "c:\Python27\lib\site-packages\esky\sudo\__init__.py", line 156, in close
self.pipe.read()
File "c:\Python27\lib\site-packages\esky\sudo\sudo_base.py", line 123, in read
raise EOFError
EOFError
Does anyone know of a solution, or have any suggestions?
Using:
python 2.7.3
esky 0.9.7
3 years and no answer that is very sad :(
This is a bug in esky.
Esky works fine for me besides the fact that escalating privileges fails.
I'm used to programming in python3... so once I'm done updating Esky to be python 2 and 3 compatible I'm going to tackle this issue.
If anyone wants to solve this problem let's rock and roll! To the github issue tracker!
This is super late, but if anyone in the future has this problem, try running sudo python your_program.py.

Categories