The slow start of powershell caused by conda init (Loading personal and system profiles took xxxx ms.)

1. Problem description

powershellSlow startup:

insert image description here

A configuration file that will be loaded for a long time every time it starts: Loading personal and system profiles took xxxxms

What I use is powershell 7.xxthat the 5.x that comes with the system may also have such a problem, for a reason.

The name of the new version is pwsh.exe, that is, enter win r, then enter pwsh, and press Enter to open it quickly.

This is very useful, it is recommended to install (go to Microsoft official website to download and install).

2. Problem traceability

Since it took a long time to load the configuration file, let's see what the configuration file is.

enter:

$PROFILE
  • $PROFILE.AllUsersAllHosts: Profiles valid for all users and all hosts.
  • $PROFILE.AllUsersCurrentHost: Profile valid for all users and current host.
  • $PROFILE.CurrentUserAllHosts: Profiles valid for the current user and all hosts.
  • $PROFILE.CurrentUserCurrentHost: Profile valid for current user and current host. When you type $PROFILE directly, PowerShell actually returns the value of $PROFILE.CurrentUserCurrentHost, which is the profile in effect for the current user and current host.

will show the location of your configuration file, for example:

C:\Users\xxx\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

Enter the relevant directory, you may find:

  • no Microsoft.PowerShell_profile.ps1;
  • Only: profile.ps1;

After testing, the name does not affect.

Open the configuration file to see:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "E:\anaconda3\Scripts\conda.exe") {
    
    
    (& "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{
    
    $_} | Invoke-Expression
}
#endregion

conda init powershellThis is about the content of the conda environment, which you generated after a certain execution:

Configuration file interpretation:

The purpose of this script is to check conda.exewhether exists in the specified path. If present, it executes conda.exeto generate an init script for PowerShell and executes the script to set up Anaconda's environment. This ensures that when you use the command in PowerShell conda, all necessary environment variables and settings are properly configured.

  1. If (Test-Path "E:\anaconda3\Scripts\conda.exe") {

    This is a conditional statement which checks E:\anaconda3\Scripts\conda.exeif the specified path (ie) exists. Test-Pathis a PowerShell command to determine if a file or directory exists. If conda.exeexists in the specified path, then {}the code inside the braces will be executed.

  2. (& "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression

    This is a complex command, I'll explain it in parts:

    • & "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook": This part &is executed using the operator conda.exe. It passes two parameters to conda.exe: shell.powershelland hook. This basically tells condaPowerShell to generate init scripts.

    • | Out-String: This part condaconverts the output of the command to string format. |is a pipe symbol that passes the output of the previous command to the next command.

    • ?{$_}: This is a filter which ensures that the string passed to the next command is not empty.

    • Invoke-Expression: This command executes the string passed to it as a PowerShell command. So, condathe generated init script will be executed to set up Anaconda's environment.

  3. }

    This is Ifthe closing brace of the statement.

3. Solutions

3.1 Test

Comment out the configuration file (# sign) and start powershell again.

Now it is opened in seconds, but it will not enter the conda environment (base).
insert image description here

3.2 Solution 1: Do not use conda in powershell

Using conda in powershell is nothing more than configuring a virtual environment. Can I use powershell?

Anaconda typically provides users with two PowerShell variants:

  1. Anaconda PowerShell Prompt : This is a special PowerShell prompt that is pre-configured with all the necessary environment variables and settings so that users can use condaand other Anaconda tools directly. When you start from this prompt, you don't need to perform any additional initialization steps to use all of Anaconda's features.

  2. Anaconda Prompt (cmd) : This is a Windows command prompt (cmd.exe) based prompt that is also pre-configured with the Anaconda environment. While it's not PowerShell, it functions similarly to Anaconda PowerShell Prompt.
    insert image description here

The purpose of both prompts is to simplify the user's workflow, allowing them to easily use Anaconda and its related tools without manually configuring the environment or performing additional initialization steps.

If you have Anaconda installed, you should be able to find these two prompts in the Windows Start menu. If you mainly use PowerShell, I recommend using " Anaconda PowerShell Prompt", because it provides all the functions of PowerShell, and also integrates Anaconda's tools and settings (of course, the opening speed is relatively slow).
insert image description here

Therefore, in Solution 1, you can comment out the powershell configuration file and use Anaconda PowerShell Prompt.

3.2 Solution 2: Use conda in powershell when needed (recommended)

Still comment out the part about conda in the powershell configuration file.

Save the conda configuration as a separate file. When you need to use conda, just run this script in powershell.

specific methods:

  1. Run powershell to view the directory where the default command is executed, generally:C:\Users\username
  2. For convenience, report the configuration file about conda to the above location, for example, name it: conda .ps1;
  3. When you need to use conda in powershell, run this script: ./conda.ps1( ./indicates this directory, same as Linux);
    insert image description here

Four, powershell 7 features

PowerShell 7 introduces many new features and improvements. Here are some key features:

  1. Cross-platform support : PowerShell 7 is cross-platform, supporting Windows, macOS, and Linux.

  2. Compatible with Windows PowerShell : PowerShell 7 is designed to be compatible with Windows PowerShell 5.1, making migration even easier.

  3. New Parallel feature : Pass ForEach-Object -Parallel, users can run script blocks in parallel.

  4. New Operators?? : The and ??=operators are introduced for working with $nullvalues.

  5. Chaining operators&& : The and ||operators are introduced , allowing users to chain commands.

  6. Ternary Operator : PowerShell 7 added the ternary operator a ? b : c.

  7. New Get-Errorcommand : This command provides a detailed, structured view of errors.

  8. Other Improvements : Includes performance improvements to JSON cmdlets, new Get-SubStringcommands, and many other enhancements and fixes.

  9. Command history: PowerShell 7 (and previous versions) has a command history feature. This feature allows the user to view and recall previously executed commands. (Different from cmd, even if it is closed and reopened, historical commands can also be queried) The following are some basic operations related to the historical command function:

    1. Get-History : This command lists the history of all previously executed commands.

    2. Invoke-History : This command allows the user to re-execute previous commands. For example, Invoke-History -Id 5the command with ID 5 will be re-executed.

    3. Add-History : This command allows the user to add commands to the history.

    4. Clear-History : This command will clear all command history.

    5. Up/Down Arrow Keys : In PowerShell, users can use the up/down arrow keys to recall and execute previous commands.

    6. Ctrl + R : This is an interactive search feature that allows the user to search for previously executed commands.

  10. Smart command completion: What I often use is: after entering a character, the history command will be displayed in light color, and it can be completed by right-clicking the keyboardinsert image description here



Write love you forever into the end of the poem ~

Guess you like

Origin blog.csdn.net/weixin_43764974/article/details/132241233