简体   繁体   中英

Python requires root to run, but Kivy crashes using sudo

I've searched online to find answers to this, but I've come up short. Other examples are different enough to not get me to a solution. This is on a Raspberry Pi 3b, Raspbian, Jessie.

I have a kivy app that uses a bluetooth (ble) peripheral device. My BLE class has to scan for BLE devices which requires root privileges. The BLE class works using sudo outside of kivy so I don't 'think' there is a fundamental problem with the BLE code. FYI, the BLE class uses bluepy (btle). In order to get the peripheral working correctly I have to run:

scanner = btle.Scanner()
dev = scanner.scan(3)

The scan requires running as root . If I didn't need it I would remove it, but then the behavior of the program changes.

My problem is that running my program (w/ BLE class AND kivy) from command prompt like this: python3 FS_run.py runs the application w/out connecting to the BLE peripheral. However, when I run it like this: sudo python3 FS_run.py , I get:

Traceback (most recent call last):
  File "FS_run.py", line 1, in <module>
    from kivy.app import App
ImportError: No module named 'kivy'

I've seen a lot of posts where successfully running kivy w/ sudo makes the buttons not work. I've changed the permissions (chmod) of the BLE class file and tried running again w/out sudo , but that didn't help. I'm open to other suggestions to circumvent the use of sudo . Perhaps running the BLE in a subprocess, but I wouldn't know how to make it join the rest of the program. Also, I'm fairly new to BLE, I just got the BLE class to work yesterday. Suffice it to say I'm a bit out of my league here.

I'm not a linux guy so I am not sure where to focus my energy to solve this. Not sure if this is helpful, but I saw this on another post so I'll just add it:
which python3 gives /usr/bin/python3
sudo which python3 gives /usr/bin/python3

EDIT:

python -c "import sys; print(sys.path)" prints different output than when run with sudo . '/home/pi/kivy' is missing when run with sudo . How do I go about fixing this?

EDIT 2:

Other posts said this fixed it:
sudo cp /home/pi/.kivy/config.ini /root/.kivy/config.ini

Didn't work for me. I got:

cp: cannot create regularfile '/root/.kivy/config.ini': No such file or directory

So then I manually created the .kivy directory in root and then did a sudo cp to copy the file over. Still does not work.

I got it to work by adding at the end of the file "/root/profile" the line :

export PYTHONPATH=/home/pi/Documents/kivy/kivy:$PYTHONPATH

Then before launching the app :

sudo su
source ~/.profile

I don't know if it helped but I have also :

sudo cp /home/pi/.kivy/config.ini /root/.kivy/config.ini

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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