操作環境
Python3.6 + Winodws7
問題現象
新安裝python3.6版本后使用pip安裝第三方模塊失敗,報錯信息如下:
C:\Users\linyfeng>pip install lxml Collecting lxml Downloading http://pypi.doubanio.com/packages/fb/41/b8d5c869d01fcb77c72d7d226a847a3946034ef19c244ac12920b71cd036/lxml-3.8.0-cp36-cp36m-win32.whl (2.9MB) 100% |████████████████████████████████| 2.9MB 4.6MB/s Installing collected packages: lxml Exception: Traceback (most recent call last): File "c:\program files (x86)\python36-32\lib\site-packages\pip\basecommand.py", line 215, in main status = self.run(options, args) File "c:\program files (x86)\python36-32\lib\site-packages\pip\commands\install.py", line 342, in run prefix=options.prefix_path, File "c:\program files (x86)\python36-32\lib\site-packages\pip\req\req_set.py", line 784, in install **kwargs File "c:\program files (x86)\python36-32\lib\site-packages\pip\req\req_install.py", line 851, in install self.move_wheel_files(self.source_dir, root=root, prefix=prefix) File "c:\program files (x86)\python36-32\lib\site-packages\pip\req\req_install.py", line 1064, in move_wheel_files isolated=self.isolated, File "c:\program files (x86)\python36-32\lib\site-packages\pip\wheel.py", line 345, in move_wheel_files clobber(source, lib_dir, True) File "c:\program files (x86)\python36-32\lib\site-packages\pip\wheel.py", line 316, in clobber ensure_dir(destdir) File "c:\program files (x86)\python36-32\lib\site-packages\pip\utils\__init__.py", line 83, in ensure_dir os.makedirs(path) File "c:\program files (x86)\python36-32\lib\os.py", line 220, in makedirs mkdir(name, mode) PermissionError: [WinError 5] 拒絕訪問。: 'c:\\program files (x86)\\python36-32\\Lib\\site-packages\\lxml'
問題分析
提示PermissionError權限不足。查看當前cmd命令執行的用戶以及該用戶是否具有administrator權限。
(1)查看windows當前登錄用戶,使用命令whoami或者echo %username%
C:\Users\Test>whoami China\test 或者 C:\Users\Test>echo %username% test
(2)查看test用戶及所屬管理組
C:\Users\Administrator>net user Test 用戶名 Test 全名 注釋 管理計算機(域)的內置帳戶 用戶的注釋 國家/地區代碼 000 (系統默認值) 帳戶啟用 Yes 帳戶到期 從不 上次設置密碼 2010/11/21 5:56:34 密碼到期 從不 密碼可更改 2010/11/21 5:56:34 需要密碼 Yes 用戶可以更改密碼 Yes 允許的工作站 All 登錄腳本 用戶配置文件 主目錄 上次登錄 2018/1/10 21:17:20 可允許的登錄小時數 All 本地組成員 *Users 全局組成員 *None 命令成功完成。
(3)查看c:\\program files (x86)\\python36-32\\Lib\\site-packages路徑的site-packages文件夾目錄權限
Users用戶組的用戶不具備寫入權限,可以使用administrator權限用戶執行或者將Test用戶加入administrator用戶組。
問題解決
使用管理員權限運行cmd命令,具體操作過程如下步驟所示:
(1) 利用where查找cmd運行絕對路徑
C:\Users\test>where cmd C:\Windows\System32\cmd.exe
(2) 到C:\Windows\System32目錄下,右鍵單擊cmd.exe文件以管理員身份運行。如下
運行結果如下:
可以發現路徑名變成了C:\Windows\system32>
(3) 重新執行pip install lxml,結果如下
C:\Windows\system32>pip install lxml Collecting lxml Downloading http://pypi.doubanio.com/packages/fb/41/b8d5c869d01fcb77c72d7d226a847a3946034ef19c244ac12920b71cd036/lxml-3.8.0-cp36-cp36m-win32.whl (2.9MB) 100% |████████████████████████████████| 2.9MB 6.8MB/s Installing collected packages: lxml Successfully installed lxml-3.8.0
(4) 安裝成功。
知識引申
DOS其他相關操作命令
query user:顯示有關終端服務器上的用戶會話的信息 net localgroup administrator ${普通用戶名} /add:將指定用戶給予管理員權限 netplwiz:打開用戶賬戶管理界面快捷鍵