[Fixed] Why when I create an enviroment with anaconda in Visual Studio Code and I try to activate it, shows me an error – Anaconda

by
Ali Hasan
anaconda backend django llama-cpp-python

Quick Fix: In Visual Studio Code:

  1. Open the User Settings JSON file (Ctrl+Shift+P, then select ‘Preferences: Open User Settings (JSON)’).
  2. Add or update the ‘python.condaPath’ setting to point to the correct location of your Conda installation.

The Problem:

I’m facing an error while activating a newly created environment named ‘whynotdata’ using Anaconda in Visual Studio Code. The command ‘conda activate whynotdata’ results in an error message: ‘usage: conda-script.py [-h] [–no-plugins] [-V] COMMAND …’, with the following options listed: clean, compare, config, create, info, init, install, list, notices, package, remove, uninstall, rename, run, search, update, upgrade, build, convert, debug, develop, doctor, index, inspect, metapackage, render, skeleton, verify, token, pack, server, env, content-trust, and repo. Surprisingly, creating the environment works flawlessly via the Anaconda prompt. My goal is to set up the environment within a specific folder in Visual Studio Code and install Django. As a beginner in back-end development with limited formal training, I’d appreciate assistance in resolving this issue.

The Solutions:

Solution 1: Set the “python.condaPath” Variable

To fix the error when activating the environment in Visual Studio Code, you need to set the “python.condaPath” variable in the user settings. Here’s how you can do it:

  1. Open Visual Studio Code.

  2. Press Ctrl+Shift+P to open the Command Palette.

  3. Search for "Preferences: Open User Settings (JSON)" and select it.

  4. In the user settings JSON file, find the "python.condaPath" variable and set it to the path of the conda executable on your system. The default path is usually:

    "python.condaPath": "C:\\Users\\your-user-name\\Anaconda3\\Scripts\\conda.exe"
    

    Make sure to replace "your-user-name" with your actual username.

  5. Save the user settings file.

Once you have set the "python.condaPath" variable, you should be able to activate the environment in Visual Studio Code by running the following command in the terminal:

conda activate whynotdata

where "whynotdata" is the name of your environment.

Remember that you may need to restart Visual Studio Code for the changes to take effect.

Solution 2: {title}

To resolve the error encountered when activating a previously created environment in Visual Studio Code, follow these steps:

  1. Set the Python: Conda Path Variable:
    • Open Visual Studio Code.
    • Navigate to the Settings (gear icon) in the lower-left corner.
    • In the Settings search bar, type “Python: Conda Path”.
    • Set the value of “Python: Conda Path” to the path where Anaconda is installed, followed by \Anaconda3\conda.exe. For example: “C:\ProgramData\Anaconda3\conda.exe”.
  2. Set the Integrated Default Profile: Windows to Command Prompt:
    • In the Visual Studio Code Settings, search for “Integrated Default Profile: Windows”.
    • Select “Command Prompt” from the dropdown options.

Once you have completed these steps, try activating your environment again using the command “conda activate whynotdata”. It should now activate successfully.

Q&A

When I try to activate the env that I already created, it shows me an error, what should I do?

You may need to make changes in conda path to make VS code recognize conda commands.

How to set the path properly?

Set the “python.condaPath” variable to “python.condaPath”: “C:\Users\your-user-name\Anaconda3\Scripts\conda.exe”

Video Explanation:

The following video, titled "Using Python with Anaconda and Visual Studio Code (Windows ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Setup Visual Studio Code to use Anaconda Python environments and interpreters. Step-by-step instructions: ...