With serverless becoming more prevalent and cloud vendors offering more and more languages and tools support, the choices for developers are increasing. However, with more choices, the set up of a development environment becomes more challenging. I love VS Code and Python [ad] is my favorite development language. Also, I think that Microsoft’s approach for allowing developers to run Azure Functions locally is a winning one. Of course, setting up your local environment for developing Azure Functions in Python is not a one-click task.
What Do You Need?
Here are the things that you will need:
- VS Code installed on your development machine
- Python interpreter (when on Windows, I prefer Anaconda but pure Python will be fine too)
Setting Up Python Development in VS Code
For Python development in VS Code, you would obviously want to install the official Python plugin from Microsoft. It is highly rated, includes
pylint, IntelliSense, highlighting, and quite a few other cool features.
Now, here is one kink that you need to be aware of. When you initially set up the Python plugin, you are asked to select your interpreter:
This will be the default interpreter that will be used every time when you work on a Python script. This same interpreter will also be used to create the virtual environments for your Azure Functions projects, but about this later.
Now, if you have more than one Python version installed on your machine, you can easily switch between versions from the lower-left corner of the VS Code window:
Once again, the last version that you select for the Python interpreter will be us as the default version for the Python plugin. You can always verify the current
PYTHON_PATH in the plugin settings in VS Code (File –> Preferences –> Settings):
This default setting can become problematic. For example, Azure Functions [ad] V2 do not support Python 3.8, and you have to use 3.7 as an interpreter. If the wrong Python version is selected when you debug your Azure Functions project, you will experience issues (about this later). So, I would recommend that you pay attention to this every time you restart your VS Code or start a new Python project.
Setting Up VS Code for Azure Functions Development
More Python Set Up in VS Code
Now, let’s look at what you need to do to set up VS Code for the development of Azure Functions. Obviously, you will need the Azure Functions plugin for VS Code. But this is not enough! First, let’s look at a few other things that you need to pay attention to while creating the Functions project.
After selecting Python as a language, you are asked to select a Python interpreter. The plugin is smart enough to give you only the supported options. In my case below, the Python 3.8.3 environment is not shown.
VS Code will use the selected interpreter to create a virtual environment for the project. The virtual environment will be available under the
.venv subfolder of the project. While you may think all is good, here are a few things to notice:
- The first thing is that the linter cannot find the
- Second, the selected Python [ad] interpreter is the one used to create the virtual environment, and not the one from the virtual environment:
If you select the correct Python interpreter (the one from the virtual environment), those two errors will disappear. However, you will get a popup that the
pylint linter is not installed:
The reason for that is that the linter binaries are installed for the original Python environment. Clicking on the Install will try to install the linter in the virtual environment set up for the project that you just created.
Here you will hit some challenges.
- First, you may see the following PowerShell error:
PS C:\Users\someuser\Documents\Temp\az-function-test> & c:/Users/memladen/Documents/Temp/az-function-test/.venv/Scripts/Activate.ps1 & : File C:\Users\someuser\Documents\Temp\az-function-test\.venv\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3 + & c:/Users/someuser/Documents/Temp/az-function-test/.venv/Scripts/Act ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) , PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
That is a bummer because this means that your virtual environment will not get activated in the Terminal.
- The second issue that you will see is the following:
PS C:\Users\someuser\Documents\Temp\az-function-test> & c:/Users/someuser/Documents/Temp/az-function-test/.venv/Scripts/python.exe c:\Users\someuser\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\pyvsc-run-isolated.py pip install -U pylint WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Collecting pylint WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pylint/ WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pylint/ WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pylint/ WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pylint/ WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pylint/ Could not fetch URL https://pypi.org/simple/pylint/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pylint/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping ERROR: Could not find a version that satisfies the requirement pylint (from versions: none) ERROR: No matching distribution found for pylint WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Unfortunately, you will need to solve both errors. Now, here an interesting thing! I opened the problematic PowerShell [ad] script by double-clicking on it, and it executed in a new Terminal window without errors (about this later):
After some investigation, here is what I discovered. Depending on what you have selected as your Default Shell for the Terminal window, you may or may not encounter errors executing the activation script. Here is what I had as options:
You can get to this selection from the Terminal window if you click on the arrow next to the shell:
Here is what happens, if you select each one of those options and open a new Terminal window:
- Selecting the Command Prompt as the default shell triggers the execution of the
activate.batfile that successfully activates the Python virtual environment.
- Selecting Git Bash (you may not have this one if you have not installed Git [ad] Bash, but most VS Code users do have it because VS Code prompts you early enough to install it) calls the Bash activation script and successfully activates your Python virtual environment.
- I do have PowerShell 7 installed on my machine. Selecting that option executes the
Activate.ps1script without problems and activates the Python virtual environment.
- So, the only unfortunate option is the Windows Power Shell [ad] option, which (for whatever reason) is the default Terminal option in VS Code.
So, the recommendation here is to change the Default Shell in VS Code from Windows Power Shell to Command Prompt, Bash, or if you have PowerShell 7.
About the second error… Well, the reason for that error is that there is no path to the
libssl* DLLs needed for
pip to establish the HTTPS connection. Here is a link to a GitHub Issue related to this problem, that can be helpful if you are using Anaconda. Even if you are not using Anaconda, you need to have the path to the DLLs in your
PATH environment variable. Adding the following locations to the
PATH environment variable and restarting VS Code will help you solve the issue:
~\Anaconda3 ~\Anaconda3\Library\mingw-w64\bin ~\Anaconda3\Library\usr\bin ~\Anaconda3\Library\bin ~\Anaconda3\Scripts ~\Anaconda3\bin ~\Anaconda3\condabin
~ is your home folder on Windows. If you do not use Anaconda, find the appropriate folders for your Python distribution. After doing this, you will be able to install the
pylint Linter and use Python and
pip from the VS Code Terminal.
It seems we’ve spent most of the time fixing the Python setup instead of configuring the Azure Functions development environment. To summarize, the two things that you need to do to properly complete the set-up of VS Code with Python is:
- Select a Default Shell for the VS Code Terminal that is different from the Windows Power Shell
- Add the Python binaries locations to the
Installing the Azure Functions Core Tools
Now, let’s get back to setting up the Functions development environment. We’ve cleared the
import error but running the Azure Function code locally (by pressing F5) fails with the following:
The Learn more button sends us to the Azure Functions Core Tools page on GitHub.
To install the Azure Functions Core Tools, you have two options:
- To use
- Or to use
chocolatey, which is the (new) package manager for Windows software
If you select the option to install the “necessary tools”, you will (most probably) end up (like me) with one more version of Python on your machine. Remember to choose the right one when you create your Azure Functions project next time.
Installing the Azure Functions Core Tools with either one of those is trivial if you follow the Microsoft instructions on GitHub. However, make sure that you install the correct version of the tools – you can choose between and V2 and V3. You can learn about the differences on Azure’s Documentation Site.
To wrap up – although VS Code is quite a convenient Editor/IDE, setting it up for the development of Azure Functions in Python is a quite elaborate process. In addition to installing a bunch of things, you need to be careful when selecting Python versions and default shells. Hopefully, this article will help you overcome those hurdles and get your development going.
Here some additional references.
- How to configure environment variables on Windows, Linux, and macOS?