Visual Studio Code

Visual Studio Code is one of the best environments for creating and debugging Python code.   It makes it easy to switch between Python versions (installed), and to create and use a Python virtual environment.   It also supports version control systems (Git, ...).  

Personally, I think the greatest advantages are:

  1. A Python virtual environment is automatically restored when a folder is loaded.  
  2. Separate windows in the IDE for code and output.
  3. Integrated version control system support.

However, those powerful and important code development features result in VS Code having a moderate learning curve.   Start by going to the Docs web page and under 'First Steps' reviewing the Intro Videos, Setup (installation), and most importantly the User Interface Finally, read carefully the VS Code doc What is a VS Code "workspace".   A VS Code 'workspace' is basically a folder dedicated to a code project.   Settings from VS Code, the project's code, and all other relevent items are stored in this folder.  

Assuming you have Python and Visual Studio Code installed, then the first step is to install the Python extension for Visual Studio Code.   From within VS Code, click on the 'Extensions' toolbar on the left vertical panel.   At the top, enter 'Python' in the search input field, and then pick the 'Python' option authored by Microsoft.  

Verify Python is installed correctly in VS Code by opening a Terminal window (menu 'Terminal', 'New Terminal'.   From here, you basically have a Windows Command Prompt window.   Enter the command below to see the version of Python installed, to confirm that PIP is installed, and to see the Python packages installed.


py -V
py -m pip -V
py -m pip list

Create a New Project (Workspace)

Currently (2023), you cannot create a new project / folder directly.   Instead, configure a Python virtual environment, and then open that folder with the VS Code 'Open Folder'm menu option.   Below will create a virtual environment specifically for Python v3.10.


py --list-paths
py -3.10 -m venv my_folder

Now copy a Python script file to that folder, or create a new script file with the VS Code command 'File -> New File -> Python file'.   If you open a terminal (cmd.exe) within VS Code, you will see that the Python virtual environment is activated, and the correct Python interpreter is also automatically selected.  

From a terminal within VS Code, install any required libraries and then create a requirements.txt file to keep track of them.  


py -m pip install --upgrade pip
py -m pip install memory-profiler
py -m pip install numpy
py -m pip install matplotlib
py -m pip list
If you get a long warning in red color about the 'Activate.ps1' file, then you tried to activate the virtual environment in Powershell rather than 'CMD'.   Click on the 'Select Default Profile' option to the right of the terminal window and set CMD to be the default.  

When finished installing packages, enter the following commands to see the installed packages, to create a new 'requirements.txt' file, and then to deactivate the Python virtual environment.  


py -m pip list
py -m pip freeze > requirements.txt
deactivate

Note that the 'requirements.txt' file may be used to share the Python project with someone else.   That person could create a similar virtual environment by entering the commands 'py -m pip install -r requirements.txt'.   But since you have the virtual environment already configured, the only command you need to reactivate the Python virtual environment is '.venv\scripts\activate'.  

Verify that the Python packages just installed don't impact the global PIP configuration by entering the following command in the VS Code Terminal window:


py -m pip list

Reactivate the Virtual Environment

From the VS Code Terminal window, enter the commands:


.venv\scripts\activate
py -m pip list

Create a Python Script

Click on the VS Code 'Explorer' toolbar.   Create a new script file by choosing the menu 'File', 'New File...'.   Dismiss any messages in the code editor window.   Enter the following


import numpy as np
import matplotlib.pyplot as plt

# Predefined paramters
ar_n = 3                     # Order of the AR(n) data
ar_coeff = [0.7, -0.3, -0.1] # Coefficients b_3, b_2, b_1
noise_level = 0.1            # Noise added to the AR(n) data
length = 200                 # Number of data points to generate
 
# Random initial values
ar_data = list(np.random.randn(ar_n))
 
# Generate the rest of the values
for i in range(length - ar_n):
    next_val = (np.array(ar_coeff) @ np.array(ar_data[-3:])) + np.random.randn() * noise_level
    ar_data.append(next_val)
 
# Plot the time series
fig = plt.figure(figsize=(12,5))
plt.plot(ar_data)
plt.show()

Save the new file by using the 'File', 'Save As...', and call it 'test.py'.  

Run the Script

From the terminal window, click on the '...' option and choose 'Run Active File'.  

IMPORTANT: If you try running the code from the code editing windows or 'Ctrl-F5' or 'Run' or 'Run Without Debugging' the script will not execute in the virtual environment.   You must run the script from the terminal window option '...' and 'Run Active File' because you are running the script in the command window to avoid PowerShell script execution blocking.  

Close The Project

From the VS Code terminal window, enter the command 'deactivate'.   Close the Workspace using the menu 'File', 'Close Workspace'.   Open the Workspace again using the menu 'File', 'Open Folder...'.   Run the script 'test.py'.   Notice that the virtual environment settings have been remembered by VS Code.   If they were not, then it would be necessary to activate the Python virtual environment with the '.venv\scripts\activate' command in the VS Code Terminal window.  

Delete a Virtual Environment

While the virtual environment is deactivated, simply use Windows file explorer to delete the virtual environment folder.   Note that this example used the folder name '.venv', but you could use any folder name.  

 

Version Control

Git

GitHub

 

Related Useful Links

Getting Started with Python in VS Code