Using Visual Studio Code (VS Code) as an IDE with the Enthought Deployment Manager (EDM)

For context, please see Canopy GUI end of life -- transition to the Enthought Deployment Manager (EDM) and Visual Studio Code

Note: the tools described in this article are usually updated monthly, providing easier use, more capabilities, and bug fixes. VS Code and the Enthought Tools for VS Code will tell you when updates are available. For EDM updates, please check its download page.

Install the Enthought Deployment Manager (EDM) 

Product info and download. This is Enthought's preferred tool for Python installation and package management. 

Caution -- almost all of EDM's functionality is command-line only.  The separate "EDM" GUI desktop application, which is included with many EDM installers, is not a GUI for general EDM functionality. Rather, this GUI's primary purpose is discovering and installing applications written for and by Enthought consulting customers.

If you encounter an error when starting the EDM GUI: 

  • As of May 2020, some new users may find that EDM GUI crashes on first startup. In this case, please open a command line (terminal) and type "edm info", after which the EDM GUI should start up ok.
  • If the EDM GUI gives you a connectivity error when starting it, it is likely that your system needs proxy firewall configuration. This is outside the scope of this article. Since the EDM GUI is not necessary for using EDM and VS Code together, please quit the EDM GUI application and proceed with the rest of this article. 

Install Microsoft's Visual Studio Code

Product info and download. This is Microsoft's free, open-source, extensible, multi-platform IDE, not to be confused with Microsoft's commercial Visual Studio.

Install Enthought Tools Extension for VS Code

From within VS Code, install Enthought Tools for VS Code.  An easy way to do this is to go to the Extensions / Marketplace tab in VS Code, type "enthought", then select and install the Enthought extension, or you can download the extension separately. It provides an easy interface for installing and working with Enthought Python environments.

After installation, the Enthought extension can be used by clicking on the Enthought icon in the VS Code left-side toolbar:

vscode-enthought-extension.png

To see all the Enthought extension commands, open VS Code's Command Palette (Ctrl (Cmd) + Shift + P), then type edm to filter the list. These commands can be invoked by clicking buttons on the UI, or directly from that list.

Create an Enthought Python environment

You can quickly create a new Python environment by using Enthought's curated "GettingStarted" environment bundle, which provides over 140 core packages and is accessible online via the Enthought Extension. Click on the "+" sign next to the bundle's name, then give the target environment a name of your choice:

vscode-enthought-extension2.png

Creating this environment may take between a few minutes and an hour, depending on your network and system speed and especially on your anti-virus settings. After the environment has been created, it will appear in the Environments list at the top left of the Enthought Tools panel. 

Alert: The GettingStarted bundle is only directly available on internet-connected machines. If you are on a machine which is offline or where a firewall is blocking access to EDM, then:

Activate an Enthought Python environment 

When an Enthought Python environment is activated, it will be the Python used when you open a Terminal or when you click the editor's Run button. You can see that it is activated because there will be a bright green dot to the left of the environment's name.

When you use Enthought Tools to create a new environment, it will automatically be activated  for the duration of the session. To re-activate it after restarting VSCode, or to activate a different environment, hover over the environment name, then click the triangular (Play) "Activate" button next to it. 

Troubleshoot: If Enthought Tools shows that environment A is activated, but other parts of VSCode are telling you that there's a different Python active, just deactivate environment A (by clicking on the X next to its name), then reactivate it.

"Linters" and the flake8 package

 If a message pops up telling you that pylint is not installed, click "Select linter" and choose flake8 from the list, if it is available.  If it is not yet available, install it.

Flake8 is automatically installed with our default GettingStarted bundle. Otherwise, the following command can be run in each EDM environment that will be used with VS Code:

edm install -e <environment-name> flake8

Background: the flake8 package is a Python linter (syntax checker) that is known to VS Code; installing it will stop VS Code's reminders to select a linter. An added benefit is that linting code while editing is generally a good practice.

About Microsoft's Python Extension for VS Code 

The Enthought extension ensures that Microsoft's Python Extension for VS Code is also installed automatically, so you do not need to install Microsoft's extension yourself. 

The Microsoft extension has many settings; most of these are personal preferences. Here are a few basics; we plan to add more suggestions in the future. 

To open settings.json, the easiest way is to type Ctrl-Shift-P (Cmd-Shift-P on Mac) and then type Open Settings (JSON) in the Command Palette that appears at the top of VS Code.

The following is a simple sample settings.json file for VS Code:

{
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120",
    ],
    "python.linting.pylintEnabled": false,
    "files.trimTrailingWhitespace": true,
    "[markdown]": {
        "files.trimTrailingWhitespace": false
    },
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
    "editor.rulers": [80, 120],
    "editor.hover.enabled": true,
}

 

Heads-up for Windows users who have another Python on PATH

If you have another Python (e.g. from Anaconda, Intel, or python.org) installed on your system and included in your PATH environment variable, then  the "Run" button in the VS Code editor will not work as expected in Enthought Python environments. You may experience errors trying to run many scripts, including those that use numpy or other packages with Python-specific DLL-based components. You may also experience errors running other Python-based VS Code extensions.

A simple test for this situation is to open a Command Prompt window, or a new VS Code Terminal panel, and try to run the python command. It should not succeed. If it does, then you are likely to experience this problem.

Solutions and workarounds

This behavior is inherited from the Microsoft Python extension; we are investigating ways to improve the experience.

1) Please note that having any Python on your PATH when you have multiple Pythons installed is a system configuration bug. The best practice for users who have multiple Pythons installed is not to have any of them on your PATH. For instructions for fixing this, see Editing environment variables on Windows. If this Python is in the system-controlled part of your PATH, then you might require help from a system administrator to remove it.

2) If fixing your PATH is not possible, the cleanest workaround in most cases is to start VS Code from within an already-activated Enthought Python environment. This temporarily places it on the PATH ahead of the other Python. To do that:

  • Completely quit VS Code
  • From the Windows Start Menu, open a Command Prompt window
  • Type edm shell -e your-Python-environment-name which temporarily modifies PATH and the prompt
  • Type code to restart VS Code with these modifications.

(Note that this workaround will fail for some combinations of environment and edm. We are investigating.)

3) Another workaround, which is easy, but somewhat inconvenient, is simply not to use the Run button. Instead, you can run all your scripts manually in VS Code Terminal window where Enthought Python has been activated. You'll know that you are in such a terminal, when you see the environment's name at the top of the VS Code's Terminal panel:

edm-terminal-in-vscode.png

 

Universal Ctags Recommendation

To make navigating through the code easier, we recommend that you install the Universal Ctags software. If Ctags is installed and available on the system path, the Python plugin will automatically use Ctags to generate the tags file needed to navigate through your code to class and function definitions. At least for Python, there is no need for a separate Ctags plugin.

For Windows users, get the latest compiled release from Universal Ctags. It will be a zip file that simply has to be unzipped into a directory. We recommend that you unzip it into your user Documents\ctags directory. Afterwards, make sure that this directory is listed in  your Windows environment variable PATH.

Installation notes for MacOS using brew can be found on Homebrew Tap for Universal Ctags.

Linux users will need to clone the Git repository and compile as documented on Universal Ctags.

To use Ctags, select a class or function name in your code, type Ctrl-T, and then select one of the definitions listed in the drop-down at the top of VS Code.

 

Other VS Code Extensions

There are very many VS Code extensions, with overlapping functionalities. Here are some Python-specific extensions which you might consider:

autoDocstring
Bracket Pair Colorizer 2
CobiSymbolOutline
Language-Cython
Markdown All in One
Projects+
reStructuredText
Rewrap
Settings Sync

 

Please do not enter support requests in article comments

Please use article comments for suggestions to improve the article. For individual support requests, please follow these guidelines.

Have more questions? Submit a request

Comments

Powered by Zendesk