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)