Visual Studio Code (VS Code)#

Note

These instructions refer to Anaconda Distribution exclusively, but will also work with Miniconda.

Visual Studio Code (VS Code) is a free, cross-platform source code editor that works with Anaconda Distribution and Miniconda. Installing the Python for Visual Studio Code extension enables VS Code to connect to a conda environment’s version of Python as an interpreter for your Python code files.

Tip

More of a visual learner? Sign in to Anaconda Cloud to follow along with our Create a simple Python program with VS Code!

Launching VS Code in Navigator#

With VS Code installed, you can launch the application from Navigator 1.9.12 or later by clicking the VS Code tile on the Home page.

When you launch VS Code from Navigator, it will automatically use the Python interpreter in the currently selected environment.

Creating a conda environment#

Before starting your Python project, Anaconda recommends creating a conda environment to isolate your project’s software packages and manage their dependencies.

  1. Open a terminal in VS Code.

  2. Create a conda environment for your Python project by running the following command:

    # Replace <ENV_NAME> with your environment name
    # Replace <VERSION> with your desired version of Python
    conda create -n <ENV_NAME> python=<VERSION>
    

Note

If you do not specify a version of Python, conda will attempt to install the latest version from its available channels.

For more information about managing environments with conda, see the official conda documentation.

For more information about Python environments in VS Code, see the official VS Code documentation.

Selecting Anaconda as a Python interpreter#

Now that your conda environment has been created, select the environment’s version of Python as an interpreter. This ensures that your code has access to the correct code completion and type information available in the specific Python version and packages installed in your conda environment.

  1. Open any Python file.

  2. To select a Python interpreter, open the Command Palette by pressing Ctrl + Shift + P on Windows/Linux or Cmd + Shift + P on macOS.

  3. Click Python: Select Interpreter in the Command Palette and select the interpreter associated with your Anaconda environment.

Tip

Anaconda interpreter typically appears as Python. 3.x.x (‘<YOUR_ENV_NAME>’: conda). If your environment’s interpreter does not appear in the Select Interpreter list, click the Refresh icon.

You can also manually enter an interpreter path by selecting Enter interpreter path….

For more information on finding a specific interpreter path in your Anaconda installation, see Finding your Anaconda Python interpreter path.

The selected interpreter displays in the bottom right corner of the status bar in VS Code.

Testing your setup#

You are now ready to execute your Python functions in VS Code!

  1. Create a new Python (.py) file.

  2. Add the following code to your file:

    # This will display "Hello, World!" in the terminal
     print("Hello, World!")
    
    # This will display your system version in the terminal
    import sys
    print(sys.version)
    
  3. Save the file.

  4. Click Run Python File.

For more information on working with Python in VS Code, see Python in Visual Studio Code.