This article is a supplement to "Using Visual Studio Code (VS Code) as an IDE with the Enthought Deployment Manager (EDM)" . Here we offer some optional productivity tips.
Sections in this article
Using a VS Code single folder project / workspace
You can accomplish a lot using VS Code as a file-by-file text editor, using the File menu to open each file individually.
If you will be working on a collection of related files, and especially if you will be switching between different projects, VS Code "workspaces" can be useful. The simplest way to do this is with a "single folder project", which you can create by:
- On the VS Code home page, "Open Folder"
- On the Enthought Tools for Visual Studio Code panel, activate your Enthought Python environment (see "Activate" in the main article referenced above)
- Open one or more files within that folder.
Launching VS Code from a command line in macOS Terminal
See the article Using Enthought Python / EDM with VS Code on a Mac.
Troubleshooting multi-Python problems
1) Be sure that you've already read the section "Heads-up for Windows users who have another Python on PATH" in Using the Enthought Deployment Manager (EDM) with the Visual Studio Code (VS Code) IDE.
2) If a conda Python environment is the default on your system (i.e. your Terminal/Command Prompt prompt begins with a conda environment name, typically
(base)), then you should start VS Code not from a shortcut or icon, but rather in a terminal / command prompt, by typing:
3) If neither of these solves your problem, then:
Sometimes the Microsoft Python Extension for VS Code seems to be "glued" to an undesired Python environment (e.g. system Python on Mac or Linux or the Python from Python Software Foundation which you might accidentally install on Windows.). When this happens, the following steps may help:
- In VS Code's Extensions panel, uninstall the Enthought extension, followed by the Microsoft Python extension.
- Restart VS Code. [In unusual cases, it might be necessary to uninstall and reinstall VS Code.]
- Re-install the Enthought extension (which will automatically reinstall the Microsoft Python Extension)
- Restart VS Code.
Data file not found when running script in VS Code
When a Python script specifies a file just by its filename (without an explicit path), for example in this script:
myarray = np.loadtxt("mydata.csv", delimiter=",")
then Python will always try to open that file in Python's "current working directory".
Counter-intuitively, Python's "current working directory" is not necessarily the same directory where the script file itself lives.
Rather, your Python script inherits the "current working directory" of the terminal shell (Windows cmd, powershell, or bash) from which Python was invoked.
The easiest way to successfully run such a script is: first, in the terminal shell where the script will run, use the
cd command to change the shell's current working directory to be the same directory as where your data file is. The most common case: in VS Code Python, the Run button runs scripts in the terminal session named "Python", so that's where you should change the current directory before running your script with the Run button.
(More complex and robust ways to ensure that your script can open a data file are outside the scope of this article. For example: your script changes the current working directory, or opens the file with an explicit path specification.)
Universal Ctags (intermediate difficulty)
To make it easier to navigate through complex code, you can install the Universal Ctags software. If Ctags is installed and available on the system path, the Python plugin will automatically use it to generate the tags file needed to navigate through your code to class and function definitions. There is no need for a separate Ctags plugin for Python.
Windows users can download 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 (or see alternative method, below).
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.
If you can't easily modify your PATH environment variable, another way to make Ctags available for Python in VS Code is to edit your settings.json file (described above in "About Microsoft's Python Extension for VS Code.")
In Windows, add these lines into an appropriate position in settings.json (assuming that you unzipped ctags into the suggested directory):
To use Ctags in VS Code, you must be working in a VS Code "workspace" (see above).
Select a class or function name in your code.
Type Ctrl-T. A list of Python objects drops down from the top of the VS Code editor; select one of these to go to that object's definition.
Code Runner extension
Enthought Tools for Visual Studio Code is not compatible with the Code Runner extension for VS Code. If you have this extension installed, please disable it when using Enthought Tools for Visual Studio Code. (Code Runner changes the appearance and behavior of the Run button; it becomes unable to find Enthought Python. Its icon on the editor bar is a pinkish circle / head.)
Other VS Code Extensions
There are very many VS Code extensions, with overlapping functionalities. Here are some Python-specific extensions which you might consider:
Bracket Pair Colorizer 2
Markdown All in One
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.