VS Code¶
Visual Studio (VS) Code is a text editor produced by Microsoft. It can be used for writing and editing any plain text file, and therefore is mostly used for writing code. The main useful features for code editing are @(syntax highlighting) and code linting. Many other excellent text editors are available (e.g., Emacs or vim or ...); if you already have a preferred text editor that you are happy with, please skip this section.
Note
You will only need to install VS Code if using your own machine. If using AppsAnywhere on a Lancaster University machine you can launch VS Code along with Anaconda.
Installing VS Code¶
The instructions below should cover installation of VS Code on Windows, although there should only be minor differences for other operating systems:
- Visit https://code.visualstudio.com/download in your web browser (or just Google "VS Code download"). Click the appropriate large blue button to download the installer file for your operating system.
- Click on the downloaded installer file at the bottom of your browser (the Windows file will be
named something like
VSCodeUserSetup-x64-1.48.2.exe
where1.48.2
was the current version at the time of writing, but this may have increased). Follow the instructions given in the dialogue boxes. - Accept the license agreement and click Next.
- Leave the default install location unchanged (unless you have a preferred install location) and click Next.
- Leave the default "Start Menu Folder" name unchanged and click Next.
- For "Select Additional Tasks" leave the tick boxes unchanged (the "Add to PATH" box should be ticked by default) and click Next.
- Click Install.
- Click Finish and VS Code should launch.
On an Apple Mac, you may need to move the executable file for VS code from your "Downloads" folder to your "Applications" folder.
Using VS Code¶
Introductory videos on using and customising various aspects of VS Code can be found at https://code.visualstudio.com/docs/getstarted/introvideos, but we will cover some of the basics here.
Launching VS Code¶
In you have previously installed the full installation of Anaconda then once VS Code is installed it will appear as an application that can be launched from the Anaconda Navigator. Alternatively, it can be opened via a standard way of opening an application:
Tip
It is recommended that you launch VS Code via Anaconda Navigator as it is simpler to then interact with the Python environments that Anaconda creates.
- Start Anaconda Navigator, e.g., in Windows click on the start button and find and click on the "Anaconda3 (64-bit)" menu, then click on "Anaconda Navigator (anaconda3)".
- In the " Home" tab, choose the environment you want to use by
selecting it in the "Application on" dropdown menu (defaulting to
base (root)
). - Scroll to find the " VS Code" application panel and click the Launch button.
If you have added a VS Code startup icon to your taskbar or desktop you can just click on that, alternatively:
- Click on the start button and find the "Visual Studio Code" menu.
- Click on this and then "Visual Studio Code" menu item to launch it.
Open a terminal and type code
, hit Enter and VS Code should start.
Upon opening VS Code for the first time you will see a Window displaying a welcome screen:
This contains a range of links to help with using VS Code
Note
You can disable this welcome message from showing each time you open VS Code by unticking the tickbox next to the "Show welcome page on startup".
Down the left hand side of the window are several icons that can be clicked on to open various panels (clicking again will close the panel):
- : open the "Explorer" panel showing any currently open files.
- : open the "Search" panel to perform "find-and-replace" in a file.
- : open the "Source Control" panel, which is useful if you code is under version control, e.g., git, mercurial or svn (we will not cover version control in this course).
- : open the "Run" panel to allow you to run and debug code in your current file.
- : open "Extensions" panel to allow you to install VS Code extensions.
- : open the settings manager.
Along the top are various dropdown menus, with the "File" menu providing options for creating new files, opening existing files and saving files.
Below these menus will be tabs containing any currently opened files. Some useful shortcut keys to access some of these options are:
- Ctrl+N: open a new empty file
- Ctrl+S: save a file (opens a file browser to set the file name if saving a new file)
- Ctrl+O: open an existing file
- Ctrl+F: open a search bar to find text in a file
- Ctrl+,: open a tab containing the VS Code settings
- Ctrl+F5: run code
- Ctrl+Shift+`: open a terminal
Installing extensions¶
VS Code allows you to install a large range of extensions that expand its abilities. These include extensions that can, amongst other things, add syntax highlighting, perform code linting, perform spell checking, and rendering/compile LaTeX documents.
The main extension that is useful for this course is the "Python" extension. This enhances VS Code's abilities when writing Python code. If using VS Code launched via the Anaconda Navigator this extension may already be installed by default. Otherwise, it can be installed by clicking on the " Extensions" panel icon on the left hand side, typing "Python" in the search bar, and clicking the Install button next to the extension named "Python" and supplied by Microsoft.
The Python extension will recognise when you have a Python file (see below) and perform:
- syntax highlighting, which is the highlighting of known keywords and formatting styles;
- linting, which is automatic checking for some coding errors.
Other useful extension to install include:
- Code Spell Checker: a spell checker!
- LaTeX Workshop: an extension for helping and enhancing editing of LaTeX documents.
Creating a file¶
To create your first file either click on the "File" dropdown menu and select "New File" or use the Ctrl+N shortcut. This will open a new tab in the VS Code window with the name " Untitled-1" (if you have more than one unsaved file open the name will increment by one, e.g., "Untitled-2", "Untitled-3", etc).
When you create a file a 1
should appear in the left of the screen. This shows the line number. As
you add more lines of text more line numbers will appear. In the bottom right of the screen there
will be some text saying, e.g., "Ln 1, Col 1", which tells you the line and column number where the
blinking cursor is.
Type some text in the file, e.g.,:
print("My first Python file")
and save it by clicking on the "File" dropdown menu and selecting "Save as..." or using the
Ctrl+S shortcut. Type in the name that you want to give the file; for files containing Python
code (see the scripts tutorial) you should give the file a name that
has the file extension ".py
", e.g., "myfirstcode.py
.
Tip
Save files with sensible names and in sensible locations!
When creating a code project it is useful to create a folder/directory specifically for it and make sure you save your files into that folder. Spend some time thinking about where you want to store the project and use a descriptive folder name.
Python file names should ideally not contain spaces or non-alphanumerical characters other
than hyphens "-
" or underscores "_
", e.g., project_utilities.py
is good, but
File Number 1!.py
is bad.
When you save a file with the ".py
" extension VS Code will recognise it as a Python file. In the
bottom right hand corner of the VS Code window, it should now say the Python version and name of
the Anaconda environment that you are in, e.g. "Python 3.8.3 64-bit ('base': conda)" if using the
default base
environment. If you run the file (as discussed below) this will be
the version of Python and environment that is used. If you have multiple environments, you can
switch between them by clicking on this text and choosing the required environment in the menu that
appears.
Customising the theme¶
By default VS Code has a colour scheme, or "theme", with a dark background and light text. You can change this to a variety of provided themes, such as having a light background and dark text.
To change the colour theme, click on the "File" dropdown menu, then hover over "Preferences" and click on "Color theme" (or use the Ctrl+K Ctrl+T shortcut). A panel will open up with a selection of themes, such as "Light (Visual Studio)" (this is what I use!) You can even customise or create your own colour theme if you want!
Running code¶
We will cover running code in a terminal in more detail in another tutorial, but here will describe how to run code within VS Code:
Note for Windows users
If running VS Code through Anaconda Navigator you can ignore this, but if you installed and are running VS Code from outside of the Ananconda Navigator you may need to apply the following instructions to run Python code.
You will need to add the location of the Anaconda installation scripts to something called your
PATH
environment variable. First, locate the directory that contains these scripts:
- Type "anaconda3" into the Windows search bar.
- Look for a result that says "anaconda3" with the words "File folder" underneath.
- Click on that result to open the File Explorer and scroll to find the "Scripts" folder.
- Double click on the "Scripts" folder to open it and click in the address bar at the top to
reveal the folder's full path. If you installed Anaconda on your machine, with you as the
sole user, this path may look something like
C:\Users\<username>\anaconda3\Scripts
, where<username>
is your user name on the machine.
- Copy the path, e.g., right click and select "Copy" or use Ctrl+C.
Now, add this path to your PATH
environment variable:
- Type "env" into the Windows search bar.
- Click on "Edit environment variables for your account".
- In the top panel under "User variable for..." click on "Path" and select Edit....
- Select Edit and then paste the Anaconda scripts path from above (right click and select "Paste" or use Ctrl+V) into the slot below any existing path names, then click OK.
This should allow VS Code to activate any Anaconda environments that you have defined when running code within it.
If you have created and saved a file with a .py
file extension VS Code will know it is a
Python file and can attempt to run the code contained within it. There are several equivalent way to
run the code:
- right click within the file and select "Run Python File in Terminal";
- in the top right corner a green play symbol should appear for Python files. Click on this to run the file.
Both those methods will open a terminal at the bottom of the VS Code screen, showing the command that was used to run the file and any output produced by the code.
Alternatively, you can open a terminal manually by clicking on the "Terminal" dropdown menu, selecting "New Terminal". Within the terminal, which should initialise with the correct environment, you can type:
python myfile.py
where myfile.py
is replaced with your file's name, and potentially the full path if the terminal
does not start in the same directory as the file, e.g.,:
python C:\User\username\Project1\myfile.py