VSCode is a general purpose code editor that can be configured for Python development.
Open up Ubuntu Software and search for code:
Select Install:
VSCode is now installed:
To develop in Python, a conda-forge
environment is required. In order to use conda, Miniforge needs to be installed and in order for the conda-forge
environment to be activated within VSCode correctly, conda
needs to be initialised with the Terminal. This was previously covered in:
Miniforge Install and Initialisation
In order to use TeX in matplotlib plots. TeX needs to be installed system wide using the operating systems package manager (which was also previously covered in the above):
sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic cm-super dvipng
The purpose of the base
environment is to use the conda package manager to install packages in other Python environments. Before using the conda package manager, the conda package manager should be updated to the latest version:
conda update conda
Since Miniforge is used, the default channel will be the community channel conda-forge
:
Input y
in order to proceed with any changes. In this example the conda
package manager is already up to date:
Note there is an issue going from conda
24 to 25 where it doesn't update properly and says:
==> WARNING: A newer version of conda exists <==
current version: 24.w.w
latest version: 25.x.x
Please update conda by running:
conda update -n base -c conda-forge conda
And inputting the command listed takes you back to the same screen. To bypass this use:
conda install conda=25.x.x
Where 25.x.xx
should be replaced by the latest version number.
To create a new environment for VSCode the following command can be used:
conda create -n vscode-env notebook jupyter cython seaborn scikit-learn pyarrow sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate nodejs ipywidgets plotly pyqt ipympl isort autopep8 ruff black
Since Miniforge is used, the default channel will be the community channel conda-forge
:
Input y
in order to proceed:
The Python environment is created:
VSCode is a general purpose code editor and can be configured with extensions. To the left hand side select extensions:
For Python development, the Python extension needs to be installed:
Installing this extension will also install Pylance which is used for Python code completion and the Python debugger which is used for debugging. These three extensions are listed under the installed category:
For interactive Python notebooks, the Jupyter extension can be installed:
This installs Jupyter Keymaps, Notebook renderers, Cell Tags and Slideshow:
Intellicode is additional code completion:
isort is a formatter that sorts imports in Python:
Code spell checker is an English spell checker:
Python indent is used to help structure indented Python code:
Python autodocstring is used to generate a template for docstrings:
Indent rainbow is used to highlight indented Python code:
Black is the black formatter:
AutoPEP8 is the autopep8 formatter:
Pylint gives additional Python linting:
Ruff is the Ruff formatter and linter:
Flake8 gives additional Python linting:
Data wrangler is used to help visualise variables:
It is recommended to close and reopen VSCode once installing the above extensions
When using VSCode, markdown files (normally a readme.md), script files and notebook files are grouped together in a folder, known as the project folder. Select Open Folder:
In this case, the project folder is the Documents folder:
Select open:
Select Yes I trust the authors:
To use Python with VSCode, the Python interpreter needs to be selected. This is similar to activating the conda environment. Press Ctrl
+⇧
+p
to open the command palette:
Search for Python select interpreter:
Select the vscode-env created earlier:
To the left hand side the Python Script file can be opened:
#%% Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#%% Set Style
sns.set_style('whitegrid')
#%% Create Data
x = np.array([0, 1, 2, 3, 4])
y = 2 * np.pi * np.sin(x)
#%% Plot
plt.plot(x, y)
plt.xlabel(R'$x$', usetex=True)
plt.ylabel(R'$x2 \pi x \sin{x}$', usetex=True)
plt.show()
It displays in its own tab and can be run using the run button:
Since this has the instruction plt.show()
, the plot displays. Notice that the terminal is still busy while the plot displays, because it is waiting for the main window of the Qt application to close (the plot backend is qtagg):
When this is closed, the new prompt displays. If the terminal in VSCode, is examined, note that the base Python environment is activated, however when the run button is used, the currently selected Python interpreter is used, which is the python binary in vscode-env
. The full path to this Python binary is displayed in the terminal alongside the script file that was run:
VSCode has Pylance and IntelliSense installed showing identifiers and docstrings as code is typed:
Selecting the Run Cell button will instead run the Script file using an IPython Shell:
Variables can be seen by selecting Jupyter Variables:
The ndarray
can be explored in the Data Wrangler extension:
Select Allow:
The Data Wrangled Variable displays in a seperate tab:
Plots are shown using the inline backend by default:
VSCode can also be used to run a Notebook and the layout is similar to the layout previously examined in JupyterLab. Select Run All to run all the cells and then select Python Environments:
Select the vscode-env
, (conda) Python environment which has the binary ~/miniforge3/envs/vscode-env/bin/python
:
The notebook displays similarly to how it displays in JupyterLab:
In VSCode, docstrings are truncated by default:
However they can be viewed as scrollable elements:
Plots are displayed by default using the inline
backend, however the backend can be changed using %matplotlib
in the same way as previously seen when using JupyterLab:
Returning the the Python Script file. Press Ctrl+⇧+p
to open up the command palette. Select Format Document With...:
Select autopep8:
This corrects the spacing around the assignment =
and around ,
delimiters but other operators are not spaced out as expected:
Press Ctrl+⇧+p
to open up the command palette and search for organize imports, select organize imports:
Then select isort:
The imports are now grouped alphabetically into groups (standard modules and third-party modules, in this case no standard modules aren't used so only one list of third-party modules displays):
Press Ctrl+⇧+p
to open up the command palette and search for Format Document With...:
Select the Black Formatter:
The spacing around other operators is now corrected but unfortunately black changed all string quotations from single to double, differing from the default Python style:
The Ruff formatter uses the black formatter by default. However it can be customised with a ruff.toml
file which is placed within the project folder. For more details see Configuring Ruff:
[format]
# Use single quotes in `ruff format`.
quote-style = "single"
Press Ctrl+⇧+p
to open up the command palette and search for Format and select Ruff or Format Document With... and select Ruff:
Now the quote style is single as desired, more closely matching the formatting used within Python itself. The underlined code is flagged up by either the spell checker or code linters:
Formatting can also be carried out on a Notebook. Before using Format Notebook, the default Formatter should be selected. Press Ctrl+⇧+p
to open up the command palette and search for Format and select Ruff or Format Document With...
Select Configure Default Formatter...
Select Ruff:
Now select the notebook file. Press Ctrl+⇧+p
to open up the command palette and search for Format Notebook:
The notebook wil now be formatted:
The underlined code is flagged up by either the spell checker or code linters:
In this case, it is by the spell checker selecting View Problem gives spelling suggestions:
Many identifiers and parameter options will not be recognised. Select add to user settings:
The linting will disappear now that this parameter is recognised:
Other linters will identify other problems with the code which can usually be addressed in a similar way:
If a .csv
file is opened, there will be details about the Rainbow Extension, select Install. Each column in the .csv
is now colour-coded making it easier to read:
The Excel Viewer Extension can be installed:
Now the Excel File can be viewed in VSCode:
VSCode has additional extensions to enhance markdown capabilities. Also because VSCode is cross-programming language, it also has numerous other extensions for other programming languages, making it suitable for cross-programming language development. These will not be further discussed as this installation guide is for Python development only.