在pycharm中使用powershell7解决conda,ohmyposh报错的问题

在pycharm中使用powershell7解决conda,ohmyposh报错的问题

pycharm会默认使用系统的powershell5,在terminal中就会报错,尤其是安装了ohmyposh, conda 后

验证powershell使用版本方法:

在 PyCharm 终端中运行:

shell 复制代码
powershell$PSVersionTable
shell 复制代码
你可能会看到类似:

PSVersion    5.1.xxxxx    # Windows PowerShell


而在你的系统 PowerShell 中运行同样命令,可能看到:

PSVersion    7.x.x        # PowerShell Core

pycharm中配置路径:

单独配置一个 pycharm-terminal.ps1 给pycharm用

settings-> terminal-> shell path中配置如下:

注意是 pwsh.exe 而不是 powershell.exe

shell 复制代码
pwsh.exe -NoExit -File "C:\Users\Administrator\Documents\pycharm-terminal.ps1"

pycharm-terminal.ps1内容:

shell 复制代码
# 禁用 Conda 插件
$env:CONDA_NO_PLUGINS = "true"

# 导入 PSReadLine
Import-Module PSReadLine -ErrorAction SilentlyContinue

# 根据 PowerShell 版本选择正确的 Oh My Posh 初始化方式
if ($PSVersionTable.PSVersion.Major -ge 7) {
    # PowerShell 7+
    oh-my-posh init pwsh --config "C:\Users\Administrator\AppData\Local\Packages\ohmyposh.cli_96v55e8n804z4\LocalCache\Local\oh-my-posh\themes\aliens.omp.json" | Invoke-Expression
} else {
    # Windows PowerShell 5.x
    oh-my-posh init powershell --config "C:\Users\Administrator\AppData\Local\Packages\ohmyposh.cli_96v55e8n804z4\LocalCache\Local\oh-my-posh\themes\aliens.omp.json" | Invoke-Expression
}

# Conda 初始化
If (Test-Path "D:\ProgramData\miniconda3\Scripts\conda.exe") {
    (& "D:\ProgramData\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}

可以通过

shell 复制代码
notepad $PROFILE

读取默认的powershell_profile配置:

shell 复制代码
# 确保 PSReadLine 模块被导入
Import-Module PSReadLine

# 使用明确的路径初始化 Oh My Posh,避免环境变量问题
oh-my-posh init pwsh --config "C:\Users\Administrator\AppData\Local\Packages\ohmyposh.cli_96v55e8n804z4\LocalCache\Local\oh-my-posh\themes\aliens.omp.json" | Invoke-Expression

# Conda 初始化
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "D:\ProgramData\miniconda3\Scripts\conda.exe") {
    (& "D:\ProgramData\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion
相关推荐
SoulRoar.9 小时前
WSL2 + Conda 环境下MinerU 4.x GPU 服务部署与 systemd 常驻指南
conda·wsl·文档解析·mineru
我要见SA姐114 小时前
Chrome 内存泄漏诊断指南:从现象定位到根因排查
ide·vscode·python·自动化·编辑器
00后程序员张15 小时前
Windows 开发 iOS App 入门,用 SwiftUI 写第一个 iOS App 装到 iPhone
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
瓶 盖15 小时前
你的 IDE 插件改了 authorized_keys:Claude Code 扩展投毒链路拆解
ide·ssh·vs code·安全分析·供应链安全·claude code
golang学习记2 天前
IDEA 断点调试技巧:拖拽
java·ide·intellij-idea
Halo10132 天前
VSCode设置护眼背景色
ide·vscode·编辑器
fanruitian2 天前
pycharm 使用anaconda
ide·python·pycharm
weixin_563371472 天前
Android Studio 2024 版 Device Explorer 打开方法
android·ide·android studio
咸咸咸虾2 天前
Microsoft Visual Studio 历史版本与官方安装包链接汇总
ide·microsoft·visual studio
芝士爱沙拉2 天前
原生内置agent智能开发能力的MCU IDE-MounRiver Studio
ide·单片机·嵌入式硬件