为什么我会收到警告“QStandardPaths: XDG_RUNTIME_DIR not set"?每次用于 PyQt5 项目 [英] Why do I get warning "QStandardPaths: XDG_RUNTIME_DIR not set" every time for a PyQt5 project

查看:1903
本文介绍了为什么我会收到警告“QStandardPaths: XDG_RUNTIME_DIR not set"?每次用于 PyQt5 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 3.6.2 和 Emacs 25 在 Ubuntu 中开发 PyQt5 项目,并且它以 root 权限运行.这工作正常,但我得到了

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

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'

从每次运行的命令行.

如果您让我了解这是什么以及避免相同情况的可能解决方案,那就太好了.

It would great, if you let me understand what this is and the possible solution for to avoid the same.

代码

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.

推荐答案

不确定是 pyqt 或 python 相关问题.可能然后以 root 权限运行,您会丢失一些环境变量和 XDG_RUNTIME_DIR 就是其中之一.

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.

这没什么大不了的,因为 Qt 足够聪明,可以回退到合理的默认值,但是您可以使用 sudo -E <you_app> 保留当前使用的环境变量:

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>:

-E' -E(保留环境)选项向安全策略表明用户希望保留其现有环境变量.如果 -E 选项是安全策略可能会返回错误指定且用户无权保留环境.

-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.

<小时>

UPD: 您可以通过 /etc/sudoers 明确指定要保留的一组变量,而不是将所有变量复制到提升的(root)环境中(这可能会引起安全问题) 文件.使用 sudo visudo 命令编辑此文件并添加一行:


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:

Defaults        env_keep += "XDG_RUNTIME_DIR"

<小时>

UPD2:如果您想没有超级用户权限访问串行设备,请将您的用户添加到设备组中(通常称为dialout):


UPD2: if you want to access serial device without superuser privileges add an your user into device's group (usually it's called 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

这篇关于为什么我会收到警告“QStandardPaths: XDG_RUNTIME_DIR not set"?每次用于 PyQt5 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆