0

I am using keyboard library that`s need acces to sudo but when I try to run the program with sudo python dont find the others libraries

import pyautogui
import keyboard

if keyboard.is_preseed('w'):
....

So when I use without sudo keyboard dont work and when I use with sudo python dont find the others libraries

deadshot
  • 8,881
  • 4
  • 20
  • 39
  • this will help https://stackoverflow.com/questions/25346171/cant-import-module-when-using-root-user – deadshot Jul 05 '20 at 17:47

2 Answers2

1

'sudo' sets up a new environment because it aims to be safe and because the new user won't have the same view of the world as you have.

You can use the keep-environment parameter to 'sudo' to preserve some of them, but that won't guarantee that your view is the same. It could be a permissions problem, or a relative path problem or a difference in homes, or a user shell-initialization setting in the other user. You can likely fabricate a good environment on the other side of sudo with something like "sudo env PYTHONSOMETHING=$PYTHONSOMETHING programname" .

So, it's complicated. I'd first use "sudo -i" to get an interactive shell and test what it looks like, and find what to change.

Chad Miller
  • 1,435
  • 8
  • 11
0

Python uses a different package insallation directory for each user by default. For example, you can find the location of the keyboard package like so:

>>> import keyboard
>>> keyboard.__file__
'/home/user/.local/lib/python3.8/site-packages/keyboard/__init__.py'

As you can see, it is located in /home/user, which means only user is supposed to use it. In order to install the package for the root user, just run pip with sudo:

sudo pip3 install keyboard
Ofir
  • 118
  • 1
  • 6