简体   繁体   English

有没有办法使用以 root 身份安装的 python 库以及未以 root 身份安装的库?

[英]Is there a way to use python libraries installed as root alongside libraries not installed as root?

I'm writing a python application for Raspberry Pi that should be able to respond to keyboard shortcuts.我正在为 Raspberry Pi 编写一个 python 应用程序,它应该能够响应键盘快捷键。 I've made a few attempts at using alternative solutions, but in the end the only thing that appears to have worked well in context is keyboard ( https://pypi.org/project/keyboard/ ).我在使用替代解决方案方面做了一些尝试,但最后似乎唯一在上下文中运行良好的是键盘( https://pypi.org/project/keyboard/ )。

The keyboard library requires root on Linux-based systems.键盘库在基于 Linux 的系统上需要 root。 Installing keyboard as root (eg. 'sudo pip3 install keyboard') is easy enough, as is running the python program making use of keyboard as root.以 root 身份安装键盘(例如“sudo pip3 install keyboard”)非常简单,就像以 root 身份运行 python 程序一样。 However, keyboard isn't the only third-party library I'll be using, and those weren't installed as root in the first place.但是,键盘不是我将使用的唯一第三方库,而且这些库一开始也没有以 root 身份安装。 So when I run my application as root, it can only import keyboard, whilst if I don't run it as root, it can import everything except keyboard.所以当我以root身份运行我的应用程序时,它只能导入键盘,而如果我不以root身份运行它,它可以导入除键盘之外的所有内容。

Do I need to reinstall all the other libraries as root to get them working alongside keyboard, or is there anything else I can do to make the non-root installed libraries accessible when running my application as root in order to use keyboard?我是否需要以 root 身份重新安装所有其他库以使它们与键盘一起工作,或者在以 root 身份运行我的应用程序以使用键盘时,我还能做些什么来使非 root 安装的库可访问?

Whenever possible, it's preferable to install libraries as a non-root user.只要有可能,最好以非 root 用户身份安装库。 I'm not familiar with the keyboard package, but perhaps it only requires the user to be in a specific group.我不熟悉键盘 package,但也许它只要求用户在特定组中。 I would guess that you only need the user to be in the input group (or whichever group owns the devices in /dev/input.) If possible, I would try adding the user to the input group:我猜你只需要用户在input组中(或者任何组拥有 /dev/input 中的设备。)如果可能的话,我会尝试将用户添加到输入组:

sudo usermod -aG input USER

Be sure to log out and log back in as that user, because that user won't be added to the group until a new session is started.请务必注销并以该用户身份重新登录,因为在启动新的 session 之前,不会将该用户添加到组中。

If you actually need to run as root, you have a couple of options...如果你真的需要以 root 身份运行,你有几个选择......

You can either install all of the libraries as root, or you can add to the python path when you run the executable.您可以以 root 身份安装所有库,也可以在运行可执行文件时添加到 python 路径。 This can be done using the PYTHONPATH environment variable.这可以使用PYTHONPATH环境变量来完成。

PYTHONPATH蟒蛇路径

Augment the default search path for module files.增加模块文件的默认搜索路径。 The format is the same as the shell's PATH: one or more directory pathnames separated by os.pathsep (eg colons on Unix or semicolons on Windows).格式与 shell 的 PATH 相同:一个或多个由 os.pathsep 分隔的目录路径名(例如 Unix 上的冒号或 Windows 上的分号)。 Non-existent directories are silently ignored.不存在的目录会被静默忽略。

In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form).除了普通目录之外,单独的 PYTHONPATH 条目可以引用包含纯 Python 模块(源代码或编译形式)的 zip 文件。 Extension modules cannot be imported from zipfiles.扩展模块不能从 zip 文件中导入。

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above).默认搜索路径取决于安装,但通常以 prefix/lib/pythonversion 开头(参见上面的 PYTHONHOME)。 It is always appended to PYTHONPATH.它始终附加到 PYTHONPATH。

An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options.如上文接口选项中所述,将在 PYTHONPATH 前面的搜索路径中插入一个附加目录。 The search path can be manipulated from within a Python program as the variable sys.path.搜索路径可以在 Python 程序中作为变量 sys.path 进行操作。

You can simply set PYTHONPATH when you run the executable.您可以在运行可执行文件时简单地设置 PYTHONPATH。

PYTHONPATH=/PATH/TO/USER/LIBRARIES /path/to/application

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM