简体   繁体   English

Raspbian - 使用 sudo 权限在 venv 中运行 Python 脚本

[英]Raspbian - Run Python script in venv with sudo rights

I am having trouble writing files to a usb drive in /media/usb via python file.write().我无法通过 python file.write() 将文件写入 /media/usb 中的 usb 驱动器。 How do I run the python script with sudo?如何使用 sudo 运行 python 脚本? And, since it should be automatic, how would i avoid to type in the password?而且,既然它应该是自动的,我将如何避免输入密码?

It's nothing about python script, but linux user permissions.这与 python 脚本无关,而是 linux 用户权限。

You can have the following options (choose one),您可以有以下选项(选择一个),

  • Give your current user the access right to the mounted folder of usb drive授予当前用户对 usb 驱动器已安装文件夹的访问权限
  • Run script with sudo to elevate user privilige使用sudo运行脚本以提升用户权限

To avoid password prompt, you can add a sudoers file under /etc/sudoers.d/ , eg为了避免密码提示,您可以在/etc/sudoers.d/下添加一个 sudoers 文件,例如

echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$USER

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

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