Information Technology & Programming

Resolve errors when deploying Visual Studio Code and Anaconda!

記事内に商品プロモーションを含む場合があります

This is a note to record an error I encountered while setting up the Visual Studio Code environment to write a Python program and how I dealt with it (information as of 8/20/20/2022). I was able to solve the problem in the end.

Problem: I can’t use the conda command in Visual Studio Code…

I installed Visual Studio Code and Python environment with Anaconda. But then I got an error… I was stuck for a while. Here is the solution.

Figure 1: Error displayed on the VS Code terminal screen. In reality, the error text is much longer. Language setting is Japanese.

I was trying to install the OpenCV library for Python in a virtual environment on Anaconda. But an error occurred. It seemed to be caused by the problem that the “conda” command was unavailable.

Execution environment and status

The execution environment was as follows.

  • Windows 11
  • Anaconda Python environment already installed
  • Visual Studio Code (ver. 1.67.2)

In addition, I have introduced a Python extension to Visual Studio Code.

Figure 2: The Python extension in VS Code

In this state, I tried to install OpenCV using conda and typed from the terminal as follows.

conda install opencv

Then, I got an error as shown in Figure 1.

Solution: Execute an initialize command for PowerShell

In Windows, I would like to run the conda command on PowerShell, but the problem was that it was not working. So I needed to run the following command in Anaconda Prompt.

conda init powershell

Then it would look like Figure 3.

Figure 3: Screen after executing a command in Anaconda Prompt

This “conda init” command is described on this page.

conda init

Initialize conda for shell interaction.

https://docs.conda.io/projects/conda/en/latest/commands/init.html

The PowerShell part of the argument depends on your environment, such as bash, zsh, etc. In a Windows environment, it is most likely PowerShell.

After this, you can use conda on Visual Studio Code without any problem. Of course, the “conda install opencv” mentioned above was also completed successfully.

When using a virtual environment

If you are not using a virtual environment, there is no problem, but if you are using a virtual environment, there is one more setting you may need to make.

That is the setting of the execution policy for commands on PowerShell. It can be checked by the following command.

Get-ExecutionPolicy

In Figure 4, it is set to “Restricted“. Therefore, it is necessary to set the execution policy to “RemoteSigned” by continuing to execute the following command. Note, however, that the PowerShell terminal must be running as an administrator.

Figure 4: Review and modify execution policies on PowerShell (Japanese ver.)

This will allow you to switch to a virtual environment.

By the way, to switch the Python interpreter on VS Code, open the command palette by pressing “Ctrl + shift + p” and search for “Interpreter.” You will see “Python: Select interpreter,” select it and choose the environment you want.

Figure 5: Command palette display and interpreter switching in VSCode. In fact, it will appear if you type as far as “Interpr.”

For more information on the Execution Policy, please see the following.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

Creating a virtual environment with conda

For more information on the Execution Policy, please see the following. Finally, I note the commands and the standard usage for creating virtual environments in conda. (since I always forget them)

conda create -n [Environment name] python=[Python version]

Reference: https://docs.conda.io/projects/conda/en/latest/commands/create.html