Visual Studio Code and Anaconda are powerful tools for Python developers. However, if you are trying to use Anaconda and Visual Studio Code together there is a good chance you have run into some problems. As common as these tools are for programming with Python, it can be difficult to figure out how to get them to work together.
In Visual Studio Code you can run Python code with Anaconda by using the Anaconda Prompt, updating the Visual Studio Code workspace settings to be aware of your Anaconda installation, or adding Anaconda to the Windows Path variable.
This article will describe how to implement all three of these methods with step-by-step instructions. I have a FREE full course on installing Python and Anaconda for VSC. You can also check out this linked article for more ways to run Python code in VSC.
1. Install Visual Studio Code and Anaconda
To start, download and install Visual Studio Code. When installing Visual Studio Code (VSC), you can keep all the default settings.
Once you have VSC installed, open VSC and install the Python extension and the Pylance extension (if they’re not already installed).
Before you continue, you might want to check out my FREE Python Quickstart course for the most up-to-date information.
Now install Anaconda (you can also install Miniconda, I actually recommend Miniconda). I like to use the default/recommended installation settings for Anaconda. I’ll describe these with images below.
To keep things simple, you can install Anaconda just for the current user.
Select your install location. I usually keep mine as the default. For Windows, this will probably be C:\Users\your-user-name\anaconda3. Remember the install location because you will need it later if you decide to add Anaconda to the path variable.
Now you will have the option to add Anaconda to the PATH environment variable. If you add Anaconda to the PATH variable it will most likely work with Visual Studio Code with no problems. However, I don’t like to do this because I frequently uninstall and reinstall Anaconda and it can cause problems if it is added to the PATH variable. I also use multiple python versions and distributions and adding them to the PATH variable can get complicated and confusing.
I do not recommend adding Anaconda to the PATH variable for now. Later in this article, I’ll show you how to do it manually in case you change your mind.
I do recommend registering Anaconda as your default Python distribution. Doing so will allow Visual Studio Code to locate the Anaconda Python distribution.
Click install and wait for the Anaconda installation to finish. This will take a little while.
2. Connect Anaconda With Visual Studio Code
We want to make sure VSC recognizes the Anaconda installation so it can access installed packages for autocomplete, syntax highlighting, and error checking. VSC should recognize Anaconda by default. If it does you will probably see something like “Python 3.9.7 (‘base’: conda)” in the lower-left corner of VSC (see image below).
If you don’t see this, or want to change to a different Anaconda environment (the example below is using the ‘base’ environment), use CTRL + SHIFT + P to open the settings palette (see image below).
Search for “Select Interpreter” and click on the “Python: Select Interpreter” option (see image below). If you have previously used the environment or interpreter it will probably appear in the palette drop-down. Otherwise you can add it as explained below.
Now click on “Enter Interpreter Path” then click on “Find”. This will open a File Explorer window where you can navigate to the location of the Python interpreter for another Anaconda environment. Navigate to the location of your Anaconda installation (C:\Users\your-user-name\anaconda3).
For the base Anaconda installation, select “python.exe”. To use a different Anaconda environment navigate to the “envs” folder, open the folder for an environment, and select “python.exe”. The lower-left corner of VSC should now update to show the name and Python version of the selected environment.
Once you have the proper Python interpreter and environment combination selected, code highlighting, autocomplete, and intellisense will be available in Visual Studio Code. If you’ve followed directions to this point you will have syntax highlighting and autocomplete working with VSC but you will not yet be able to run Python code from VSC. I’ll go through a few ways to do that next.
3. Use the Anaconda Prompt
My preferred way to use Anaconda with Visual Studio Code is to write the code in VSC and run the code from the Anaconda Prompt. To do this, simply open the Anaconda Prompt. You can find the Anaconda Prompt by searching in the Windows menu. This will open a command line terminal like the Windows command prompt, except this terminal is configured to use all the Anaconda tools.
Once the Anaconda Prompt is open, type “conda” and press enter. You should get a help message that explains usage for
conda. If you get a message like the following, you do not have the correct prompt open.
'conda' is not recognized as an internal or external command,
operable program or batch file.
Once you’ve confirmed that you have access to the Anaconda tools from the Anaconda Command Prompt, you can run the Python scripts you develop in VSC from the Anaconda Prompt. If you’re not sure how to do this, check out this tutorial for a step-by-step guide.
This is how I use Anaconda and Python with VSC almost all of the time. VSC gives me a great code editor and using the Anaconda Prompt to run the code helps me keep my environment settings clean.
4. Change the Workspace Settings
To use Anaconda with VSC you can also change the workspace settings so your project uses the Anaconda tools. To do this open the VSC settings and (CTRL + SHIFT + P) and type “settings”. Then select “Open Workspace Settings (JSON). This will create a new directory (.vscode) and an empty JSON file (settings.json) in your code directory. The JSON file will contain settings that pertain to your current project.
settings.json by adding these lines of code. You may need to change the file paths slightly to reflect your system.
This code will run the Windows Command Prompt with the Anaconda tools available so that you can run the Python code directly from VSC.
Right-click on the Python file you want to run in the VSC file explorer panel and select “Run Python File in Terminal”. This will open a new terminal window that is aware of your Anaconda environment and run the Python code.
5. Add Anaconda Tools to the Windows PATH Variable
In VSC you’ll notice that if you type
conda in the command prompt you will get the error described above. And if you type
python in the command prompt it may take you to the Windows store to install Python. This is because the Windows command prompt does not know that you have Anaconda and its Python distribution installed.
To fix this add the path to the Anaconda python.exe and Scripts folder to the Windows PATH variable. As I mentioned above, you have the option to do this when Anaconda is installed. I do not recommend this method, and it’s not recommended by Anaconda, but it may be what some users want to do.
To add to the Windows PATH variable search for ‘Environment Variables’ from the Windows Start Menu and select the option to “Edit the system environment variables”.
In the window that opens select “Environment Variables”
Now you have the option to change environment variables for the current user or the system. You can add the appropriate file paths to either the Path (or PATH) variable for either the User or the System.
Select the “Path” variable and click “Edit”. This will open a new window with a list of file paths. Select “New” to add a new path. Add the following two paths to the “Path” variable.
Now click OK to close all the open Environment Variable windows.
Now when you type
conda into the command prompt a usage information message should display.
Visual Studio Code is my go-to IDE/editor for Python development and Anaconda is my preferred Python distribution and environment management system. Over the years I’ve found it easiest to use VSC to write and edit code and the Anaconda Prompt to run Python code. If you’re looking for a more integrated approach, updating the settings JSON for each project is flexible and lets you work completely in VSC. For those that know what they’re doing and have a good reason, you can even add Anaconda to your system environment variables. Whichever option you choose integrating VSC and Anaconda gives you a powerful Python development environment.
Whether you’re looking to take your GIS skills to the next level, or just getting started with GIS, we have a course for you! We’re constantly creating and curating more courses to help you improve your geospatial skills.
All of our courses are taught by industry professionals and include step-by-step video instruction so you don’t get lost in YouTube videos and blog posts, downloadable data so you can reproduce everything the instructor does, and code you can copy so you can avoid repetitive typing