• 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("
    ")]
        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 FilesPython36")
            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)


  • 相关阅读:
    Linux基础命令-cp
    Linux基础命令-mkdir
    Linux基础命令-touch
    Linux基础命令-diff
    Linux基础命令-cut
    Linux基础命令-stat
    System.Web.HttpException: 请求在此上下文中不可用
    数据库日志删除、压缩操作
    如何收缩和删除SQL日志文件
    Excel 常用宏代码大全
  • 原文地址:https://www.cnblogs.com/breakcircle/p/13157716.html
Copyright © 2020-2023  润新知