ModuleNotFoundError while importing python files as modules in Azure App Services
Asked Answered
M

2

3

I am getting started with Azure App Services.

I followed this tutorial and was able to get the example main.py app running.

The modules (fastapi, uvicorn, etc...) specified in the requirements.txt are all installed and imported correctly.

However, when trying to import Python files located in the same directory as the main.py, deployment is still successful, but I am getting an application error when trying to browse by webpage: enter image description here

For instance, I created a file called settings.py, containing:

MY_TITLE = "My FastAPI prototype"

In this main.py, I simply modified:

app = FastAPI()

With:

import settings
app = FastAPI(title=f"{settings.MY_TITLE}")

But this failed! Please note that when I specify MY_TITLE in the main script, I am able to update the page's title correctly.

How can I import Python files from my main.py script on Azure App Services?

Manno answered 4/9, 2022 at 22:51 Comment(5)
I would suggest you enable diagnostic logging on your webapp and Try to look the application logs under (Diagnose and solve problems--> Availability and performance) to identify what is causing this issue.Ebonieebonite
Thanks for your suggestion @VenkateshDodda-MSFT. I enabled the logs in my Linux App Service under App Services Logs, where I specified the quota and retention period. I then downloaded the zipped logs using https://<app-name>.scm.azurewebsites.net/api/logs/docker/zip.Manno
This was very useful @VenkateshDodda-MSFT. Checking the logs confirms that I am running into a ModuleNotFoundError.Manno
Please have a look at this answer and this answer.Janaye
Thanks for sharing these answers, @Chris. The relative imports (Option 1 in https://mcmap.net/q/1161134/-python-modulenotfounderror-while-importing-a-module-in-conftest-for-pytest-framework) did the trick for me: from .settings import *. If you end up writing a full-fledged answer, I will gladly accept it.Manno
J
1

A ModuleNotFoundError occurs when Python is unable to resolve the module's name. If the module's name was found neither in sys.modules nor in the standard library, Python will try to resolve it in sys.path (see The Module Search Path). If the named module cannot be found, a ModuleNotFoundError is raised.

To ovecome this error, you will have to either add the parent directory of the module (Python file)—or the directory containing the entire project—to the system PATH variable, or use relative imports, as decribed in this answer and this answer. One advantage of relative imports is that they are quite succinct. To specify the location for relative imports, you use the dot notation. A single dot means that the module referenced is in the same directory as the current location. Two dots would mean that it is in the parent directory of the current location, and so on. Example:

from .settings import MY_TITLE

or, to import every function and property contained in the settings module, use the * wildcard:

from .settings import *

However, be careful when using *. The PEP 8 style guide also suggests that:

Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

I should also mention that relative imports can be messy, particularly for shared projects where directory structure is likely to change, and are not as readable as absolute ones. Thus, the PEP 8 style guide recommends using absolute imports in general:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path).

However:

explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose.

Janaye answered 8/9, 2022 at 9:56 Comment(1)
Apparently when using Azure App Services, one may set the path by typing set PATH in the cmd console. I will stick to relative imports for now, though!Manno
E
0

Adding an answer from our comment discussion

I would suggest you enable diagnostic logging on your webapp and Try to look the application logs under (Diagnose and solve problems--> Availability and performance) to identify what is causing this issue.

As you have mentioned in your comments that you are seeing Module not found error. In general, ModuleNotFoundError: means that Python could not find one or more of your modules when the application started. This most often occurs if you deploy your virtual environment with your code. Virtual environments are not portable, so a virtual environment should not be deployed with your application code.

You can refer to this documentation for further troubleshooting on Module not found error

Ebonieebonite answered 7/9, 2022 at 5:28 Comment(1)
Thanks for your detailed answer, @VenkateshDodda-MSFT. I am aware that ModuleNotFoundError means that Python cannot find some of the modules/packages. However, in this specific case, I do not think that this error is related to the virtual environment being deployed with the application code, as I did not upload any virtual environment file onto the Azure DevOps repo that I use as my Source.Manno

© 2022 - 2024 — McMap. All rights reserved.