python 在windowns下以管理员运行代码(pip安装权限不够)


 

 

win10下,pip安装的时候权限不够,无法安装;

1.以管理员权限权限运行cmd;

2.使用 --user 参数;不过只能当前安装的人使用;安装路径也在%APPDATA%;

 --user                      Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows. (See the Python documentation for site.USER_BASE for full details.)

3.用python以管理员权限运行代码;

import os import subprocess import time import ctypes, sys # 'wcwidth', 'zipp'] pip_list = [ 'requests'] def send_cmd(cmd, encoding='utf-8'): ''' cmd发送命令 :param cmd:命令 :param encoding: 编码方式,默认utf-8,出现乱码用gbk :return: ''' print("==" * 40) print("【CMD】 ----> {}".format(cmd)) print("installing...") res = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("【RES】 ----> {}".format(res.stdout.decode("utf-8").strip())) return res.stdout def pip_install(): print("PIP package install begin...") python = "python" cmd = "where python" res = send_cmd(cmd=cmd).decode("utf-8").strip() res_list = [i.strip() for i in res.split("\r\n")] print("==" * 40) print(str(res_list)) flag = 0 for index, value in enumerate(res_list): if "Python36" in value: python = value print("==" * 40) print("python36 路径:" + value) os.chdir(os.path.dirname(os.path.abspath(python))) print("当前路径为:%s" % str(os.getcwd())) flag = 1 break if flag == 0: os.chdir(r"C:\Program Files\Python36") print("当前路径为:%s" % str(os.getcwd())) # cmd = "python.exe -m pip install --user --upgrade pip" cmd = "python.exe -m pip install --upgrade pip" res = send_cmd(cmd=cmd) for i in pip_list: cmd = "python.exe -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host https://pypi.tuna.tsinghua.edu.cn " + str( i) res = send_cmd(cmd=cmd) time.sleep(0.5) print("==" * 40) print("pip install success!") def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False if __name__ == "__main__": if is_admin(): print("==" * 40) pip_install() print("==" * 40) else: if sys.version_info[0] == 3: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM