If you’re developing with Python and the Amazon Web Services (AWS) boto3 module, you probably wish you had type hints (aka. auto-complete / Intellisense) in Microsoft Visual Studio Code. Even though the boto3 documentation is exceptionally good, it’s annoying to constantly have to switch back and forth between it and your editor. More context switching equals less productivity, at least in my experience.
If you’d rather have method signatures show up in VSCode, instead of having to read the documentation, you’re in luck! There’s a solution.
Follow these simple steps, and you will be up and running in no time:
- Install Microsoft Visual Studio Code
- Install the Python extension for Visual Studio Code
- Install the botostubs Python module from PyPi:
pip3 install boto3 botostubs
- At the top of your Python script file:
import botostubs, boto3
- When declaring a boto3 client object, add a type hint comment:
iam = boto3.client('iam') # type: botostubs.IAM
- Disable the default “Jedi” Python engine in VSCode. This is incompatible with botostubs for some reason.
Let’s review some of these steps in a bit more detail. I’m going to assume you’ve got Visual Studio Code, Python 3, and pip installed on your system already.
Install Python Modules
In order to use the AWS
boto3 module, of course you need to have it installed. Just in case you don’t have it installed, go ahead and run the following
pip3 install boto3 botostubs
Install Python Extension for Visual Studio Code
Installing extensions for Visual Studio Code is painfully simple. Navigate to the Extensions section on the left-hand navigation bar. Search for the Python extension, click the Install button, and then reload the VSCode window.
Disabling the Jedi Python Engine
The open source Jedi Intellisense engine is the default Intellisense engine in the Python extension for Visual Studio Code. Unfortunately, this extension seems to have issues with stub modules that provide type hints for Python. Until those get fixed (hopefully soon!), I suggest disabling the Jedi engine in the VSCode Python extension. Thankfully it’s easy to toggle on and off.
To disable Jedi, open the Command Palette in VSCode (
CTRL + SHIFT + P (Windows) or
CMD + SHIFT + P (MacOS)), and search for “set JSON.” This command will open your VSCode settings JSON file.
With your JSON configuration opened, add a new setting as follows:
After changing the Python Intellisense engine, you’ll need to invoke the Reload Window command, again using the VSCode Command Palette.
Write Your Python Code
You’re now ready to start writing your Python code in Visual Studio Code, with type hints / Intellisense! Go ahead and create a new Python file. First, at the top of your script, add your imports.
import boto3, botostubs
After the imports, you can create your AWS client objects for services like IAM, S3, CloudFormation, EC2, and so on. The
botostubs module exposes some direct child classes that are used to declare type hints on your real boto3 client objects. Let’s take a look at how that works below.
s3 = boto3.client('s3') # type: botostubs.S3
Now that you’ve declared your
s3 variable, and provided the comment-based type hint, VSCode knows that your
s3 variable is actually an S3 client object. On the next line, when you type
s3.cre, you’ll see a list of API methods that start with
cre, such as
Newer PEP-526 Type Hint Syntax
In addition to comment-based type hints, you can also utilize the newer PEP-526 type hint syntax, similar to the below. Note that this syntax was introduced in Python 3.6, so make sure you’re on version 3.6 or later.
variable: type = object
In practice, here’s how you’d create an Amazon S3 client object, using PEP-526 syntax.
mys3: botostubs.S3 = boto3.client('s3')
With this capability enabled, you should now see Intellisense results showing up for your AWS objects, like the screenshot below.
After configuring Visual Studio Code to use boto3 type hints via the botostubs module, you should be on your way to being a much more productive Python developer. Although it’s easy to get stuck in your old ways, it’s important to reflect on your software development tooling and improve it periodically.