Try   HackMD

使用Window內建之Powershell

一般Windows使用者使用conda通常應該是在內建的Powershell進行操作,但是在部份的電腦中常常遇到無法有效開啟conda的環境問題。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

首先使用conda指令,發現powershell可以成功的呼叫出conda相關說明與指令,但是卻無法切換環境,powershell也無法在前面出現(base)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用者原則問題

重點在於powershell.ps1 檔案,這個是PowerShell寫的指令碼文字,你可以在記事本中寫一段PowerShell程式碼,然後將其儲存為“xxx.ps1”ps1檔案是PowerShell寫好的指令碼檔案。在Windows系統中,預設情況下是不允許執行.ps1檔案的。

這時候會發現執行了conda init powershell之後再打開powershell後會發現類似以下的訊息:

. : 因為這個系統上已停用指令碼執行,所以無法載入 C:UsersUSERNAMEDocumentsWindows
PowerShellprofile.ps1 檔案。如需詳細資訊,請參閱 about_Execution_Policies,網址為
https:/go.microsoft.com/fwlink/?LinkID=135170。
位於 線路:1 字元:3
+ . 'C:UsersUSERNAMEDocumentsWindowsPowerShellprofile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

解決方式

使用 Powershell ISE進行原則修改,利用系統管理員的身份打開。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

並在頁面中輸入以下指令:

Get-ExecutionPolicy

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
此時電腦預設應該是Restricted,這時候我們要輸入修改的指令

Set-ExecutionPolicy RemoteSigned

最後在將程式關掉,Powershell以系統管理員身份重新開啟,並重新輸入conda init powershell指令,使其寫入指令檔後再次關閉Powershell,重新開啟後,應該就能看到前方寫著(base)的環境狀態了,這時候也能透過conda activate [name]進行環境上的切換了。