Skip to content

Latest commit

 

History

History
416 lines (224 loc) · 14.4 KB

File metadata and controls

416 lines (224 loc) · 14.4 KB

VSCode Ubuntu Setup

VSCode is a general purpose code editor that can be configured for Python development.

Install VSCode

Open up Ubuntu Software and search for code:

img_001

Select Install:

img_002

VSCode is now installed:

img_003

Miniforge Installation and Setup

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

Updating the conda Package Manager

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

img_004

Since Miniforge is used, the default channel will be the community channel conda-forge:

img_005

Input y in order to proceed with any changes. In this example the conda package manager is already up to date:

img_006

img_007

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.

Creating a VSCode conda-forge Environment

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

img_008

Since Miniforge is used, the default channel will be the community channel conda-forge:

img_009

Input y in order to proceed:

img_010

The Python environment is created:

img_011

Installing Extensions

VSCode is a general purpose code editor and can be configured with extensions. To the left hand side select extensions:

img_012

img_013

For Python development, the Python extension needs to be installed:

img_014

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:

img_015

For interactive Python notebooks, the Jupyter extension can be installed:

img_016

This installs Jupyter Keymaps, Notebook renderers, Cell Tags and Slideshow:

img_017

Intellicode is additional code completion:

img_018

isort is a formatter that sorts imports in Python:

img_019

Code spell checker is an English spell checker:

img_020

Python indent is used to help structure indented Python code:

img_021

Python autodocstring is used to generate a template for docstrings:

img_022

Indent rainbow is used to highlight indented Python code:

img_023

Black is the black formatter:

img_024

AutoPEP8 is the autopep8 formatter:

img_025

Pylint gives additional Python linting:

img_026

Ruff is the Ruff formatter and linter:

img_027

Flake8 gives additional Python linting:

img_028

Data wrangler is used to help visualise variables:

img_029

It is recommended to close and reopen VSCode once installing the above extensions

Project

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:

img_030

In this case, the project folder is the Documents folder:

img_031

Select open:

img_032

Select Yes I trust the authors:

img_033

Selecting the Python Interpreter

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:

img_034

Search for Python select interpreter:

img_035

Select the vscode-env created earlier:

img_036

Python Script and Python Shell

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()

img_037

It displays in its own tab and can be run using the run button:

img_038

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):

img_039

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:

img_040

VSCode has Pylance and IntelliSense installed showing identifiers and docstrings as code is typed:

img_041

img_042

img_043

Python Script and IPython Shell

Selecting the Run Cell button will instead run the Script file using an IPython Shell:

img_044

img_045

img_046

Variables can be seen by selecting Jupyter Variables:

img_047

The ndarray can be explored in the Data Wrangler extension:

img_048

Select Allow:

img_049

The Data Wrangled Variable displays in a seperate tab:

img_050

Plots are shown using the inline backend by default:

img_051

Notebook

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:

img_052

Select the vscode-env, (conda) Python environment which has the binary ~/miniforge3/envs/vscode-env/bin/python:

img_053

The notebook displays similarly to how it displays in JupyterLab:

img_054

img_055

In VSCode, docstrings are truncated by default:

img_056

However they can be viewed as scrollable elements:

img_057

img_058

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:

img_059

Formatters and Linters

Returning the the Python Script file. Press Ctrl+⇧+p to open up the command palette. Select Format Document With...:

img_061

Select autopep8:

img_062

This corrects the spacing around the assignment = and around , delimiters but other operators are not spaced out as expected:

img_063

Press Ctrl+⇧+p to open up the command palette and search for organize imports, select organize imports:

img_064

Then select isort:

img_065

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):

img_066

Press Ctrl+⇧+p to open up the command palette and search for Format Document With...:

img_067

Select the Black Formatter:

img_068

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:

img_069

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"

img_070

img_071

Press Ctrl+⇧+p to open up the command palette and search for Format and select Ruff or Format Document With... and select Ruff:

img_072

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:

img_073

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...

img_074

Select Configure Default Formatter...

img_075

Select Ruff:

img_076

Now select the notebook file. Press Ctrl+⇧+p to open up the command palette and search for Format Notebook:

img_077

The notebook wil now be formatted:

img_078

The underlined code is flagged up by either the spell checker or code linters:

img_079

In this case, it is by the spell checker selecting View Problem gives spelling suggestions:

img_080

Many identifiers and parameter options will not be recognised. Select add to user settings:

img_081

The linting will disappear now that this parameter is recognised:

img_082

Other linters will identify other problems with the code which can usually be addressed in a similar way:

img_083

Other Extensions

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:

img_084

The Excel Viewer Extension can be installed:

img_085

Now the Excel File can be viewed in VSCode:

img_086

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.

Return to Python Tutorials