Skip to content

Latest commit

 

History

History
437 lines (226 loc) · 15.7 KB

File metadata and controls

437 lines (226 loc) · 15.7 KB

VSCode IDE Windows Setup

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

Install VSCode

VSCode can be downloaded from the VSCode website:

Select the Windows Installer (this will default to the User Installer):

img_001

Launch the setup:

img_002

Accept the License Agreement and select nxt:

img_003

Select next:

img_004

Select next:

img_005

Select next:

img_006

Select install:

img_007

Select finish:

img_008

Miniforge Installation and Setup

In VSCode 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, MikTeX needs to be installed system wide and added to the Windows path which was also previously covered int he tutorial above.

Updating the conda Package Manager

Open the Windows Terminal (if this has been initialised, the) 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_009

Since Miniforge is used, the default channel will be the community channel conda-forge and since Windows 11 is used, the platform is win-64:

img_010

Input y to proceed:

img_011

The conda package manager is now up to date:

img_012

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_013

Once again since Miniforge is used, the default channel will be the community channel conda-forge and since Windows 11 is used, the platform is win-64:

img_014

Input y to proceed:

img_015

The environment will be created:

img_016

The powerShell Terminal can be closed:

Installing VSCode Extensions

VSCode is a general purpose code editor and has a variety of extensions for different programming languages:

img_017

Install the Python Extension:

img_018

Installing this extension, will also install Pylance for code completion and the Python Debugger for debugging:

img_019

For interactive Python notebooks, install the Jupyter extension:

img_020

This extension will automatically install Jupyter Slide Show, Jupyter Notebook Renderers, Jupyter Keymap and Jupyter Cell Tags:

img_021

The IntelliCode extension will give additional code completion:

img_022

The isort extension is a formatter which supports imports:

img_023

The Code Spell Checker is an English Language Spell checker (similar to spell check in a Word Processor):

img_024

In Python, indentation is important and the Python indent extension can make it easier to correctly indent Python code:

img_025

The autodocstring is used to quickly generate a template for a Python docstring:

img_026

In Python, indentation is important and the indent-rainbow extension can make it easier to visualise indentation levels:

img_027

The Black Formatter extension, formats code to comply with PEP8 and adds additional formatting compliant with the black style guide but this style guide differs from the default style used in Python itself:

img_028

The Autopep8 Formatter extension, formats code to comply with PEP8 but isn't as through as the black formatter:

img_029

The Pylint extension, is a linter, which is essentially a "grammar and punctuation" checker for code:

img_030

Ruff is an improved formatter and linter which has been rewritten in Rust for fast performance:

img_031

The Flake8 extension, is another linter, which carries out additional "grammar and punctuation" checks for code:

img_032

The Data Wrangler extension is used for viewing Variables in more detail:

img_033

Installing the above gives 19 extensions:

img_034

It is recommended to refresh VSCode by closing and relaunching to refresh the extensions.

Project

VSCode is project based. A project is essentially a folder of code, markdown and notebook files. The previous files examined can be copied to a folder called project:

img_035

In VSCode select the file explorer to the left and select Open Folder (alternatively select File → Open Folder):

img_036

VSCode has a file explorer to the left:

img_037

Select Open Folder:

img_038

Select the project folder and select, Select Folder:

img_039

Select Yes I Trust the Authors:

img_040

Selecting the Python Interpretter

Selecting the Python Interpretter, is essentially selecting the python.exe and the conda-forge environment associated with that python.exe. To select the interpretter, press Ctrl+⇧+p to open up the command palette:

img_041

Search for Python Select Interpretter:

img_042

Select the vscode-env, (conda) Python environment which has the ~\miniforge3\envs\vscode-env\python.exe:

img_043

It is recommended to refresh VSCode by closing and relaunching to refresh the extensions and Python Interpretter.

Python Script and Python Shell

To the left hand side the Python Script file can be opened:

img_044

Notice Syntax highlighting displays, as well as code linting and spell check:

img_045

The script file can be run:

img_046

Notice that a PowerShell Terminal displays at the bottom and displays the location of the python.exe and the location of the script file. The Terminal Hangs until the plot is closed and the plot is plotting using the QtAgg by default. In other words this is the same behaviour as when a script is run in the Windows terminal using python:

img_047

When the plot is closed, the next prompt displays in the Terminal. VSCode has Pylance and IntelliSense installed showing identifiers and docstrings as code is typed:

img_048

img_049

img_050

Python Script and IPython Shell

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

img_051

A kernel may need to be selected when running the first IPython session:

img_052

Select, select Another Kernel:

img_053

Select Python Environments:

img_054

Select the vscode-env, (conda) Python environment which has the ~\miniforge3\envs\vscode-env\python.exe:

img_055

Note if there is a prompt to install Python. Close down VSCode to refresh the extensions and Python environments and retry.

Each cell can be ran interactively:

img_056

Plots are shown using the inline backend by default:

img_057

Variables can be seen by selecting Jupyter Variables:

img_058

The ndarray can be explored in the Data Wrangler extension:

img_059

Select Allow:

img_060

The Data Wrangled Variable displays in a seperate tab:

img_061

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:

img_062

When the first notebook is run, a prompt for a Python Environment will be displayed, select Python Environments:

img_063

Select the vscode-env, (conda) Python environment which has the ~\miniforge3\envs\vscode-env\python.exe:

img_064

Note if there is a prompt to install Python. Close down VSCode to refresh the extensions and Python environments and retry.

img_065

The notebook displays similarly to how it displays in JupyterLab:

img_066

In VSCode, docstrings are truncated by default:

img_067

However they can be viewed as scrollable elements:

img_068

img_069

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_070

Formatters and Linters

Press Ctrl+⇧+p to open up the command palette:

img_071

Search for organize imports:

img_072

Select isort or Ruff (which also uses isort):

img_073

The imports are now grouped alphabetically into groups (standard modules and third-party modules):

img_074

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

img_075

Select autopep8:

img_076

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

img_077

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

img_078

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_079

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_080

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

img_081

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_082

If the pandas import is examined, there is a complaint that the module is imported but never used which unnecessarily slow down the code run time:

img_083

Many of the identifiers or input parameters will be flagged as spelling mistakes. View problem can be selected and this input parameter option can be added to the user settings:

img_084

Once this input parameter is added to the user settings, it is recognised by the linter and the input parameter option will no longer be aligned.

Other Extensions

If a .csv file is opened, there will be details about the Rainbow Extension, select Install:

img_085

img_086

Each column in the .csv is now colour-coded making it easier to read:

img_087

If the .xlsx file is attempted to be opened, there is a warning:

img_088

The Excel Viewer Extension can be installed:

img_089

Now the Excel File can be viewed in VSCode:

img_090

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