Python 为什么每次 PyQt5 项目都会收到警告“QStandardPaths:XDG_RUNTIME_DIR 未设置”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45981317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get warning "QStandardPaths: XDG_RUNTIME_DIR not set" every time for a PyQt5 project
提问by Nithin Varghese
I am using python 3.6.2 and using Emacs 25 for the development of a PyQt5 project in Ubuntu and it's running with root privileges. This works fine but I'm getting
我正在使用 python 3.6.2 并使用 Emacs 25 在 Ubuntu 中开发 PyQt5 项目,并且它以 root 权限运行。这工作正常,但我得到
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
from the command line for each run.
从每次运行的命令行。
It would great, if you let me understand what this is and the possible solution for to avoid the same.
如果您让我了解这是什么以及避免这种情况的可能解决方案,那就太好了。
The code
编码
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
# TODO: board connection
self.comPort.addItems([str(port) for port in display_SerialPorts()])
self.comPort.highlighted.connect(self.boardConnet)
def boardConnet(self):
baudrate = 9600
port = self.comPort.currentText()
ser = serial.Serial(
port, baudrate, timeout=1) # open first serial port
ser.close()
ser.open()
Thanks in advance for your time – if I've missed out anything, over- or under-emphasised a specific point let me know in the comments.
提前感谢您的时间——如果我遗漏了任何东西,过分强调或过分强调某个特定点,请在评论中告诉我。
回答by 9dogs
Not sure it is a pyqt or python related problem. Probably then running with root privileges you are loosing some environment variables and XDG_RUNTIME_DIR
is among them.
不确定这是 pyqt 或 python 相关问题。可能然后以 root 权限运行,您会丢失一些环境变量,并且XDG_RUNTIME_DIR
是其中之一。
It's not a big deal since Qt is smart enough to fall back to reasonable default but you can preserve current uses's environment vars with sudo -E <you_app>
:
这没什么大不了的,因为 Qt 足够聪明,可以回退到合理的默认值,但您可以通过以下方式保留当前使用的环境变量sudo -E <you_app>
:
-E' The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.
-E' -E(保留环境)选项向安全策略指示用户希望保留其现有环境变量。如果指定了 -E 选项并且用户没有保存环境的权限,则安全策略可能会返回错误。
UPD:instead of copying all your vars into elevated (root) environment (which may rise security concern), you can explicitly specify a set of variables to keep via /etc/sudoers
file. Edit this file with sudo visudo
command and add a line:
UPD:您可以明确指定一组变量以通过/etc/sudoers
文件保留,而不是将所有变量复制到提升的(根)环境中(这可能会引起安全问题)。使用sudo visudo
命令编辑此文件并添加一行:
Defaults env_keep += "XDG_RUNTIME_DIR"
UPD2:if you want to access serial device without superuserprivileges add an your user into device's group (usually it's called dialout
):
UPD2:如果您想在没有超级用户权限的情况下访问串行设备,请将您的用户添加到设备组中(通常称为dialout
):
# check group
>>> ls -l /dev/ttyUSB0
crw-rw---- 1 root dialout 4, 66 Aug 6 12:23 /dev/ttyUSB0
# add your user to a group
>>> sudo usermod -a -G dialout <your_username>
logout - login may be required after group change
注销 - 更改组后可能需要登录