在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
相关推荐
余衫马15 小时前
2026最新版!Android Studio 极速安装与配置指南
android·ide·android studio
你的菊花 倾心恋仙16 小时前
最锋利的Visual Studio Web开发工具扩展:Web Essentials详解
前端·ide·visual studio
MoSTChillax17 小时前
数字人竞品分析(中):核心功能、产品使用与技术能力对比
ide·macos·xcode
林三的日常20 小时前
AI编程IDE卡成PPT?低配电脑的末日来了
ide·电脑·ai编程
2501_9151063220 小时前
SwiftUI项目创建详解:使用Xcode从零开始创建第一个App项目
ide·vscode·ios·swiftui·个人开发·xcode·敏捷流程
PC2005-cloud1 天前
FinalShell 自定义背景图片教程
ide·python·pycharm
2501_916007471 天前
IDE 是什么?集成开发环境详解与 iOS 开发选型指南
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
方与圆之帆1 天前
conda 管理不同版本的软件包
conda·gdal·osm
骇客野人2 天前
IntelliJ IDEA 菜单中英对照
java·ide·intellij-idea
不喝水的鱼儿3 天前
本地部署 260K 上下文 Qwen-3.6-35B-A3B 与 Ornith-1.0-35B 模型及 VSCode Copilot 参数调优记录
ide·vscode·语言模型·copilot·llama