This post will describe the end-to-end process of setting up VS Code environment for Python and developing & deploying AWS Lambda functions using that VS Code setup.
Visual Studio Code is the most popular IDE in the world right now according to Stack Overflow’s 2019 survey while Python is the most wanted and second most loved programming language and has risen the ranks of programming fame probably more quickly than any other language. So it’s quite an inclination to have these 2 entities setup side by side to complement each other.
After you finish this tutorial, you will be able to:
- Setup Visual Studio Code environment for Python
- Write Python code in Visual Studio Code
- Run and debug Python Code in Visual Studio Code
- Create virtual environment for Python
- Connect your Visual Studio code environment to your AWS account
- Create new AWS Lambda functions locally on Visual Studio Code.
- Deploying your serverless applications using AWS SAM to your account using Visual Studio Code.
The tutorial is broken down into two parts: 1) Setup Visual Studio Environment for Python and 2) Connecting and deploying to AWS from Visual Studio Code. This is the first part.
First install Python on your machine. You can find it here from Python’s official website.
Python’s package manager, pip, comes bundled with the installation. To make sure you have pip, you can run the following command on your terminal:
python3 -m ensurepip --default-pi
Python environment on Visual Studio Code
Now we will setup Visual Studio Environment so we can develop in Python.
Install Visual Studio Code from their official website.
After installation, open Visual Studio Code and click on the Extension button on the left side bar.
In the search bar, type ‘Python’ and search. A list will appear and most probably on the top, you will see the Python extension by Microsoft:
Install it and reload if prompted.
your first Python workspace
Open your command terminal and enter the following commands to create a new workspace:
mkdir python-ignite cd python-ignite
python-ignite is simply a name of your choosing.
The above command will simply open the current working directory in Visual Studio Code.
Select Python Interpreter
Now we will explicitly choose our Python interpreter, as there could be multiple Python interpreters installed on your machine:
Open Command Palette:
- Windows and Linux: Ctrl+Shift+P
- MacOS: Shift+Command+P
Type and select
Python: Select Interpreter and select an interpreter from the list, depending on how many Python installations you have. This value is going to be stored in the following file:
settings.json. You can change your interpreter any time.
Create new file and paste the following code inside it:
launchMessage = 'Ignition Sequence start' print(launchMessage)
running Python Code
Python code can now be run in one of the following ways:
- Click the ‘Run Code’ button
- Right click in the file and select ‘Run Python File in Terminal’
- In order to run a specific selected portion of the code, select the code, right click and select ‘Run Selection/Line in Python Terminal’.
- Open the Command Pallette Shift-Command-P and enter
Python: Start REPL
- Now you can enter the code in the REPL and run it.
debugging python code
Place a breakpoint on Line 2 by either clicking on the left side of a line before the line number or by placing a cursor on a line and pressing F9.
Press F5 to run the program.
A menu will appear that will present you with a list of different options from which you can choose the type of application to run. Choose
Python File for now.
You might be presented with an option on the left hand side to either Debug with Python or Run with Python. For now, choose Debug with Python.
The program will run and will pause on our breakpoint:
There are tons of things that you can do now in the debug mode in VS Code; you can inspect a variable in the VARIABLES pane, hover over variables to see values, in the Debug Console you can perform simulations on variables etc.
Running code form the terminal
If you want to run a Python script from the command terminal, simply enter this in the terminal:
Creating a Python Virtual Environment
Virtual Environments are useful in the scenarios when, let’s say, you have multiple Python installations on your system and some of your applications depend on a different version of Python and some depend on a different version one. With Virtual Environment, you can create a completely isolated environment for an application, in which that application is isolated and runs using the specified Python Interpreter and standard library. Anything in the global environment does not affect it and it itself does not effect anything outside of it.
To set it up:
- Open Command Palette.
- Type ‘Terminal: Create new integrated terminal’
- For macOS/Linux:
python3 -m venv .venv
- For Windows:
py -3 -m venv .venv
- This new environment will now be visible in VS Code when you type Python: Select Interpreter. Select the newly created environment.
installing Python Packages in the Virtual Environment
If you need to install any packages, you can install them using pip this way:
- Mac OS:
python3 -m pip install matplotlib
python -m pip install matplotlib
apt-get install python3-tk
python3 -m pip install matplotlib
These packages are now installed within the domain of the Virtual Environment only. You can run the program in the Virtual Environment now and it will run in isolation from the global context.
To deactivate the Virtual Environment, type
deactivate in the VS Code terminal.
This completes the first part of this tutorial for setting up Visual Code environment for Python. The second part for connecting and deploying to AWS can be found here.
This first part of the tutorial enabled you to setup Visual Studio Code with Python, establish a Python Interpreter of your choice for use in a Visual Studio Workspace. This post also described different ways to run a Python program in Visual Studio Code and also how to debug Python code using Visual Studio Code. It also enabled the use of Python’s Virtual Environment inside Visual Studio Code.
You can now jump to the second part of this tutorial which describes how to connect your Visual Studio Code environment with your AWS Account and to deploy your serverless applications automatically without any manual deployment steps everytime you want to deploy it.