Visual Studio Code - Getting Started

Python
VS Code
Ubuntu
Linux
Working with VS Code and Python on Ubuntu 22.10
Author

Chantel Davies

Published

January 1, 2023

Introduction

This post is more of a reminder to me on how to work with Python from VS Code. As I intend to improve my Python skills and combine other languages (such as R, SQL, HTML, CSS, Javascript, bash) I have been looking into different development environments that would enable me to switch easily between them.

It is easy to forget how to make this work, so I have created a short summary of the steps I took to get started.

My system

I work with Ubuntu 22.10. I have been using Ubuntu for many years, since the days of Feisty Fawn (2007-ish), which coincided with starting a PhD at the University of Edinburgh. I figured it would be important to learn coding, and I have been fascinated with computing from a young age, though I didn’t get my first Windows PC until I was in my mid-20s. I DID have a Nintendo 8-bit as a kid and played it until my fingers bled (to steal a quote from Bryan Adams). I also started learning R around 2007 as I was not counting on an academic career, even though I wanted one, and therefore no guarantee I would be able to access expensive statistical software.

I took a couple of Unix courses with the University and learned to work with the command line and bash for general system administration.

Installing virtualenvs for Python

Python 3 comes integrated with newer versions of Ubuntu; Python 2 is no longer supported.

Virtual environments are very handy for projects that use various packages that may be updated over time. Package developers and maintainers sometimes need to change functions and deprecate certain features, which means projects and programs created with deprecated functions may break when packages are upgraded. By creating projects in virtual environments with the packages installed in those environments, the programs should run as expected.

For a long time R has not used this approach, so there have been many times when I have upgraded packages to find my code will not run and I had to figure out what has changed and how to fix it. Bit of a faff, but I like to keep things up to date. Fortunately R is now starting to use virtual environments, and with the rise in the use of containerised environments, the issues of package changes are becoming a bit easier to deal with.

The excellent folks at FreeCodeCamp wrote a very helpful tutorial on getting started with virtual environments for Python 3, which can be found here: How to Set Up Virtualenv with Virtualenvwrapper on Ubuntu 18.04. The author is Goran Aviani.

Even though it is written for an earlier version of Ubuntu, it still works for the version I use.

I first navigate to the .virtualenvs directory using the following code:

cd .virtualenvs

Then create a new virtual space:

mkvirtualenv new_project

The output might look like this, depending on your setup:

created virtual environment CPython3.10.7.final.0-64 in 106ms
  creator CPython3Posix(dest=/your_system/your_directory/.virtualenvs/new_project, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/your_system/your_directory/.local/share/virtualenv)
    added seed packages: pip==22.3.1, setuptools==65.5.1, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /your_system/your_directory/.virtualenvs/new_project/bin/predeactivate
virtualenvwrapper.user_scripts creating /your_system/your_directory/.virtualenvs/new_project/bin/postdeactivate
virtualenvwrapper.user_scripts creating /your_system/your_directory/.virtualenvs/new_project/bin/preactivate
virtualenvwrapper.user_scripts creating /your_system/your_directory/.virtualenvs/new_project/bin/postactivate
virtualenvwrapper.user_scripts creating /your_system/your_directory/.virtualenvs/new_project/bin/get_env_details

Visual Studio Code

After trying various IDEs, I have opted for VS Code as it was recommended for its accessibility. There are others I tried that I liked, but I felt I needed to just crack on with something and VS Code is it.

I installed VS Code using the instructions from the page Visual Studio Code on Linux, which worked as expected. Then I followed the tutorial Python in Visual Studio Code to install the Python extension.

In order to run VS Code so that it links to the correct virtual environment for the project I want to work in, I use the Ubuntu terminal to navigate to the new project folder in the virtual environment I just created contained with the .virtualenvs directory.

cd .virtualenvs/new_project

Then I start VS Code within that folder with:

code .

This should initialise the virtual environment and create the necessary files, such as config files, .gitignore, and various dependencies and other packages that might be installed along the way.