日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Windows 10 使用 PowerShell 7 + Windows Terminal 及乱码、不显示 python环境问题

發布時間:2023/12/29 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows 10 使用 PowerShell 7 + Windows Terminal 及乱码、不显示 python环境问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 1. Windows Terminal
    • 1.1 安裝 Windows Terminal
    • 1.2 安裝字體
    • 1.3 配置 Windows Terminal
  • 2. Power Shell 7
    • 2.1 安裝 Power Shell
    • 2.2 配置 Power Shell
  • 3. 疑難雜癥
    • 3.1 安裝字體后 Poweline 仍然亂碼
    • 3.2 Powerline 不顯示 Python 虛擬環境
    • 3.3 去掉主機名
    • 3.4 遺留問題
  • 參考文獻

寫這篇文章純屬偶然,習慣 Mac上使用 iTerm2 + oh-my-zsh,也希望 Windows上有好用的 Terminal,雖然知道有 Windows Terminal,但懶的折騰也就沒整。不過最近電腦頻繁死機,不得以重裝后,順便把 Windows Terminal搞了,不過遇到各種亂七八糟的問題,在這里跟大分享一下。

  • 注意:本文各部分不完全是安裝過程,建議大家把所有的軟件安裝完后,再對各部進行配置

1. Windows Terminal

1.1 安裝 Windows Terminal

從Microsoft App Store 安裝Windows Terminal。

1.2 安裝字體

NERD FONTS 中選擇任意你喜歡的字體并進行安裝,注意: 需要與后面相關的字體設置相匹配

安裝方法:

  • 下載相應的字體并解壓縮
  • 選中所有的字體
  • 右擊選擇 “安裝”
  • 1.3 配置 Windows Terminal

    • 打開Windows Terminal的配置文件

    • 配置默認設置部分

    “defaultProfile”: 將 Power Shell 7 設置為默認終端
    “profiles” :設置外觀、字體、主題等
    “useAcrylic”:用于設置毛玻璃特效
    “colorScheme”:設置主題
    “face”:設置字體

    "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}","profiles": {"defaults": {"acrylicOpacity": 0.5,"closeOnExit": "graceful","colorScheme": "Homebrew","cursorColor": "#FFFFFF","cursorShape": "bar","font": {"face": "JetBrainsMono NF","size": 11},"hidden": false,"historySize": 9001,"padding": "5, 5, 20, 25","snapOnInput": true,"startingDirectory": ".","useAcrylic": false},
    • 設置 Windows Terminal 主題

    本文主題為經他人修改的 Homebrew 主題
    將該配置文件放置于 "schemes": 項目下

    {"background": "#283033","black": "#000000","blue": "#6666E9","brightBlack": "#666666","brightBlue": "#0000FF","brightCyan": "#00E5E5","brightGreen": "#00D900","brightPurple": "#E500E5","brightRed": "#E50000","brightWhite": "#E5E5E5","brightYellow": "#E5E500","cursorColor": "#FFFFFF","cyan": "#00A6B2","foreground": "#00FF00","green": "#00A600","name": "Homebrew","purple": "#B200B2","red": "#FC5275","selectionBackground": "#FFFFFF","white": "#BFBFBF","yellow": "#999900"},

    2. Power Shell 7

    2.1 安裝 Power Shell

    在 Power Shell 的 GitHub 項目中選擇適合自己的 Power Shell 進行安裝。

    2.2 配置 Power Shell

    • 安裝 Powe Shell 插件

    打開 Windows Terminal ,進入 Power Shell

    # 1. 安裝 PSReadline 包,該插件可以讓命令行很好用,類似 zsh Install-Module -Name PSReadLine -Scope CurrentUser# 2. 安裝 posh-git 包,讓你的 git 更好用 Install-Module posh-git -Scope CurrentUser# 3. 安裝 oh-my-posh 包,讓你的命令行更酷炫、優雅 Install-Module oh-my-posh -Scope CurrentUser
    • 修改 Power Shell 的配置文件

    Set-PoshPrompt :用于設置 oh-my-posh 主題

    code $Profile <#* FileName: Microsoft.PowerShell_profile.ps1* Author: 劉 鵬* Email: littleNewton6@outlook.com* Date: 2021, Aug. 21* Copyright: No copyright. You can use this code for anything with no warranty. #>#------------------------------- Import Modules BEGIN ------------------------------- # 引入 posh-git Import-Module posh-git# 引入 oh-my-posh Import-Module oh-my-posh# 引入 ps-read-line Import-Module PSReadLine# 設置 PowerShell 主題 # Set-PoshPrompt ys Set-PoshPrompt paradox #------------------------------- Import Modules END -------------------------------#------------------------------- Set Hot-keys BEGIN ------------------------------- # 設置預測文本來源為歷史記錄 Set-PSReadLineOption -PredictionSource History# 每次回溯輸入歷史,光標定位于輸入內容末尾 Set-PSReadLineOption -HistorySearchCursorMovesToEnd# 設置 Tab 為菜單補全和 Intellisense Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete# 設置 Ctrl+d 為退出 PowerShell Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit# 設置 Ctrl+z 為撤銷 Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo# 設置向上鍵為后向搜索歷史記錄 Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward# 設置向下鍵為前向搜索歷史紀錄 Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward #------------------------------- Set Hot-keys END -------------------------------#------------------------------- Functions BEGIN ------------------------------- # Python 直接執行 $env:PATHEXT += ";.py"# 更新系統組件 function Update-Packages {# update pipWrite-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan$a = pip list --outdated$num_package = $a.Length - 2for ($i = 0; $i -lt $num_package; $i++) {$tmp = ($a[2 + $i].Split(" "))[0]pip install -U $tmp}# update TeX Live$CurrentYear = Get-Date -Format yyyyWrite-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyantlmgr update --selftlmgr update --all# update ChocoloteyWrite-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyanchoco outdated } #------------------------------- Functions END -------------------------------#------------------------------- Set Alias BEGIN ------------------------------- # 1. 編譯函數 make function MakeThings {nmake.exe $args -nologo } Set-Alias -Name make -Value MakeThings# 2. 更新系統 os-update Set-Alias -Name os-update -Value Update-Packages# 3. 查看目錄 ls & ll function ListDirectory {(Get-ChildItem).NameWrite-Host("") } Set-Alias -Name ls -Value ListDirectory Set-Alias -Name ll -Value Get-ChildItem# 4. 打開當前工作目錄 function OpenCurrentFolder {param(# 輸入要打開的路徑# 用法示例:open C:\# 默認路徑:當前工作文件夾$Path = '.')Invoke-Item $Path } Set-Alias -Name open -Value OpenCurrentFolder #------------------------------- Set Alias END -------------------------------#------------------------------- Set Network BEGIN ------------------------------- # 1. 獲取所有 Network Interface function Get-AllNic {Get-NetAdapter | Sort-Object -Property MacAddress } Set-Alias -Name getnic -Value Get-AllNic# 2. 獲取 IPv4 關鍵路由 function Get-IPv4Routes {Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } Set-Alias -Name getip -Value Get-IPv4Routes# 3. 獲取 IPv6 關鍵路由 function Get-IPv6Routes {Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } Set-Alias -Name getip6 -Value Get-IPv6Routes #------------------------------- Set Network END -------------------------------
    • 查看 oh-my-posh 主題

    選1個自己喜歡的主題,并對 Set-PoshPrompt 進行修改

    Get-PoshThemes

    3. 疑難雜癥

    3.1 安裝字體后 Poweline 仍然亂碼

    • 這主要與字體選擇有關,安裝自己喜歡的字體后,在選擇字體時請使以 NF 結尾的字體,例如:JetBrainsMono NF。

    3.2 Powerline 不顯示 Python 虛擬環境

    • 個人感覺這大概是 oh-my-posh 的 bug,因為不啟用 oh-my-posh 時,會默認顯示 python 的虛擬環境。
    • 開啟 oh-my-posh 主題后,python 虛擬環境不再顯示。
    • 解決這個問題需要修改 oh-my-posh 相應主題的配置文件
    • 解決的方案有兩個,一是修改 oh-my-posh 對應主題的 python 模塊,二是在對應主題添加 Environment Variable 模塊
    • 主路徑應該在 xxxxxxx\Documents\PowerShell\Modules\oh-my-posh\5.7.0\themes
    • 方法一
    {"type": "python","style": "powerline","powerline_symbol": "\uE0B0","foreground": "#100e23","background": "#906cff","properties": {"prefix": " \uE235 ","display_virtual_env": true,"display_default": true,"display_mode": "context"}},

    • 方法二

    找到 anaconda 的環境變量

    dir env:

    在 properties 配置相應的環境變量

    {"type": "envvar","style": "powerline","powerline_symbol": "\uE0B0","foreground": "#193549","background": "#FF6E6E","properties": {"prefix": "\uE235","var_name": "CONDA_DEFAULT_ENV"}},

    3.3 去掉主機名

    • 去掉主機名需要修改對應主題的 "session"模塊

    禁用 "display_host" 即可

    {"type": "session","style": "powerline","powerline_symbol": "\uE0B0","foreground": "#100e23","background": "#ffffff","properties": {"display_user": true,"display_host": false}},

    3.4 遺留問題

    • 仔細觀察會發現 oh-my-posh 左側會有一段空白,以前在 Vim 中是通過換適版本的主題解決的,但是現在不知道入何解決了

    參考文獻

    [1] Oh my Posh 3——易于自定義主題的Powershell美化工具
    [2] Windows Terminal 完美配置 PowerShell 7.1
    [3] PowerShell 界面美化
    [4] Windows Terminal + oh-my-posh模塊美化官方教程集錦以及常見問題(問題收集中)
    [5] 簡單配置與美化Powershell和Terminal
    [6] Powershell 主題美化學習過程

    總結

    以上是生活随笔為你收集整理的Windows 10 使用 PowerShell 7 + Windows Terminal 及乱码、不显示 python环境问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。