python调用shell时报错:没有终端存在,且未指定 askpass 程序

本文介绍了解决因权限不足无法修改/etc/sudoers文件的问题。通过使用root权限执行特定命令,可以有效地解决这一问题。具体操作包括设置sudo密码及执行所需的终端指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题在于权限不够,可以使用root权限修改etc/sudoers

可以使用以下方法解决:

sudoPassword = 'password'(你的密码)
command = 'cmd'(你要调用的终端语句)
p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

 

from sys import version from utils.ColorUtils import * if float(version[0:3]) < 3.6: print(color.FAIL+'Python 3.6 or above required!'+color.ENDC) exit() import platform from os import mkdir, makedirs, remove, path, system, environ, getcwd if 'Windows' in platform.system(): from os import startfile system('python3 -m pip install matplotlib scikit-learn') system('python3 -m pip install -r requirements.txt') system('python3 -m pip install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html') import wget from zipfile import ZipFile from shutil import copy import sys import os import subprocess from getpass import getpass # Install Ansible password = getpass(color.BOLD+'Please enter linux password:'+color.ENDC) def run_cmd_pwd(cmd, password): os.system("bash -c \"echo "+password+" | sudo -S "+cmd+" &> /dev/null\"") def run_cmd(cmd): system("bash -c \""+cmd+"\"") # Install WSL if Windows if 'Windows' in platform.system(): trial = subprocess.run("where bash.exe", shell=True, stderr=subprocess.PIPE) stdout = trial.stderr.decode() if 'Could not find' in stdout: system("powershell.exe -ExecutionPolicy Bypass 'framework/install_scripts/install_wsl.ps1'") # Installing InfluxDB if 'Windows' in platform.system(): install_dir = 'InfluxDB' influxdb_install_path = 'C:/Program Files/' + install_dir if not path.exists(influxdb_install_path+'/influxdb-1.8.3-1/influxd.exe'): print(color.HEADER+'Installing InfluxDB'+color.ENDC) url = 'https://dl.influxdata.com/influxdb/releases/influxdb-1.8.3_windows_amd64.zip' mkdir(influxdb_install_path) filename = wget.download(url) zf = ZipFile(filename, 'r') zf.extractall(influxdb_install_path) zf.close() for folder in ['meta', 'data', 'wal']: mkdir(influxdb_install_path+'/'+folder) copyfile('framework/install_scripts/influxdb.conf', influxdb_install_path+'/influxdb-1.8.3-1/influxdb.conf') remove(filename) print('InfluxDB service runs as a separate front-end window. Please minimize this window.') startfile(influxdb_install_path+'/influxdb-1.8.3-1/influxd.exe') elif 'Linux' in platform.system(): run_cmd_pwd('apt install influxdb', password)
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值