What makes for a well-constructed local environment? - python

I'm currently building an Azure Function. Currently I'm debugging the script and I've encountered a KeyError 'error in six.moves'. This similar problem suggests that it was a 'badly configured environment', and the solution was to "redo the virtual environment". What would count as a 'well-configured environment'? As far as my understanding goes, all that it has to be is that it meets the version requirements needed, and that no conflicts between dependencies should be present.
I will be attempting to redo my own virtual environment.

Please try the below steps if it helps to fix the issue:
What makes for a well-constructed local environment?
Make Sure you run the Azure Function (Python Stack) in virtual environment (.venv) in the Visual Studio Code.
Check all the required packages are available in the requirements.txt file.
If Virtual environment is not created while creating the Azure Functions Python in VS Code, you can create using the commands in terminal as given in thi s MS Doc.
Check the Azure Functions Core Tools version is the latest for the Python Version above 3.6
Check all the required packages are available by using the command pip list in the VS Code Terminal.
After that, start the function using the command func host start from the same project terminal in VS Code and check.

Related

Azure functions python VSCode Debug The term '-Command' is not recognized as the name of a cmdlet

When trying to debug my python azure function from Visual Studio code I get the error below
Any ideas?
Please make sure you have installed virtualenv, then you can debug your function app.
You can follow below article.
Installing packages using pip and virtual environments

Django Visual Studio The environment '....' is not available

I had a virtual environment setup for Django 3 with visual studio. However at some point it seems that this previous environment has been damaged - and therefore is no longer working (nothing appears on git to have changed).
I therefore attempted to setup a new environment via the visual studio python environment options. I could setup a conda environment but it will not let me setup a python environment as "you will need to install a python interpreter...". However on the same page it states that the latest python 64 is installed.
It will not run with the conda environment that I have setup as it states "The environment '....' is not available."
I have tried creating a new solution with VS and this appears to work as intended. However it is not really an option to create a new solution as I will loose a lot of existing git commits along with this causing issues with existing servers.
Edit - Found my own answer to this question.
As new projects were allowing virtual environments to be created I figured it must be something to do with visual studio storing some reference to an old interpreter.
Therefore, Deleting the .vs folder inside my project allowed regeneration of the visual studio files and creation of a new virtual environment.

Azure functions: Installing Python modules and extensions on consumption plan

I am trying to run a python script with Azure functions.
I had success updating the python version and installing modules on Azure functions under the App Services plan but I need to use it under the Consumption plan as my script will only execute once everyday and for only a few minutes, so I want to pay only for the time of execution. See: https://azure.microsoft.com/en-au/services/functions/
Now I'm still new to this but from my understanding the consumption plan spins up the vm and terminates it after your script has been executed unlike the App Service plan which is always on.
I am not sure why this would mean that I can't have install anything on it. I thought that would just mean I have to install it every time I spin it up.
I have tried installing modules through the python script itself and the kudu command line with no success.
While under the app service plan it was simple, following this tutorial: https://prmadi.com/running-python-code-on-azure-functions-app/
On Functions Comsumption plan, Kudu extensions are not available. However, you can update pip to be able to install all your dependencies correctly:
Create your Python script on Functions (let's say NameOfMyFunction/run.py)
Open a Kudu console
Go to the folder of your script (should be d:/home/site/wwwroot/NameOfMyFunction)
Create a virtualenv in this folder (python -m virtualenv myvenv)
Load this venv (cd myenv/Scripts and call activate.bat)
Your shell should be now prefixed by (myvenv)
Update pip (python -m pip install -U pip)
Install what you need (python -m pip install flask)
Now in the Azure Portal, in your script, update the sys.path to add this venv:
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'myvenv/Lib/site-packages')))
You should be able to start what you want now.
(Reference: https://github.com/Azure/azure-sdk-for-python/issues/1044)
Edit: reading previous comment, it seems you need numpy. I just tested right now and I was able to install 1.12.1 with no issues.
You may upload the modules for the Python version of your choice in Consumption Plan. Kindly refer to the instructions at this link: https://github.com/Azure/azure-webjobs-sdk-script/wiki/Using-a-custom-version-of-Python
This is what worked for me:
Dislaimer: I use C# Function that includes Python script execution, using command line with System.Diagnostics.Process class.
Add relevant Python extension for the Azure Function from Azure Portal:
Platform Features -> Development Tools -> Extensions
It installed python to D:\home\python364x86 (as seen from Kudu console)
Add an application setting called WEBSITE_USE_PLACEHOLDER and set its value to 0. This is necessary to work around an Azure Functions issue that causes the Python extension to stop working after the function app is unloaded.
See: Using Python 3 in Azure Functions question.
Install the packages from Kudu CMD line console using pip install ...
(in my case it was pip install pandas)

PTVS cant find Python interpreter?

I just copied my Flask project from one machine to another. I have same version of Python installed on both the machines. When I loaded the project in the new machine, it said my virtual environment is unavailable. So I initially tried to install it from requirements.txt file but it failed without any helpful error message.
So I deleted the virtual env in Visual Studio and tried to create another one. Now it complains that it cannot find any Python interpreters on my machine. I tried uninstall/reinstall Python but it didn't work. Also, the Python location is added in the PATH environment variable and all the modules in the requirements.txt file are downloaded from pip individually.
So the modules are installed, python is installed and the project is there but the virtual env won't setup because of the below reason. Any way that I can fix this ? This is PTVS15 and Python 3.6.1
Fixed it myself. I went ahead and added the environment manually by specifying the Python installation paths in the Add Environment tab. It took a while to detect the interpreter even in this way but it finally worked. I am now able to build my app.

PTVS + Django: why can i not import python libraries

i am following the tutorial found here: http://www.windowsazure.com/en-us/documentation/articles/cloud-services-web-sites-python-django-app-with-ptvs/
I already had django installed on my system. After following the instructions in this tutorial, i noticed that it seemed to have installed django again, but this time under my project's folder (myproject/env/Lib/site-packages).
Why is this?
The other issue i am having is that if i try to import certain libraries using the visual studio editor, it's unable to find the import. However, if do this in the Interactive window, it does recognize the imports. Also, if create a stand-along python (not django) project, i am able to import the libraries fine.
Any thoughts on why this is happening and how to resolve?
The tutorial uses Python virtual environments (virtualenv), which you need in order to deploy your website to Azure. A virtual environment, basically, is an isolated Python environment with its own set of libraries (site-packages etc) that is distinct from your main interpreter. It allows you to have specific versions of packages that you need just for that particular website, and different from the same for some other website of yours.
If you don't plan to deploy it to Azure, you don't need a virtual env - you can skip the step creating it, and instead install Django and other packages directly into your main interpreter. If you do plan to deploy to Azure, you need the virtual env, because the main interpreter installed on the Azure VM will not have any modules other than those in the standard library.
The reason why Python Interactive window lets you import the packages is that the instance you're working with is for your main interpreter. Every registered interpreter, and every virtual env in an opened project, has its own separate Python Interactive window. You can open the window for your virtual env from the Python Environments window, which is available via Tools -> Python Tools -> Python Environments.

Categories