Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get warning "QStandardPaths: XDG_RUNTIME_DIR not set" every time for a PyQt5 project

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'

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.

like image 846
Nithin Varghese Avatar asked Aug 31 '17 12:08

Nithin Varghese


1 Answers

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.

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

like image 194
9dogs Avatar answered Oct 21 '22 12:10

9dogs