Python script fails at startup: ModuleNotFoundError: No module named 'msal' — what is the correct way to declare/install MSAL dependency?

Netanel Stern 0 Reputation points
2025-12-16T19:27:54.2266667+00:00

Question: Python script fails with ModuleNotFoundError: No module named 'msal' — recommended way to declare MSAL dependency?

Description

I’m running a Python script that imports the Microsoft Authentication Library for Python (msal) at startup. When executed in a clean virtual environment, the script fails immediately with a ModuleNotFoundError because msal is not installed automatically.

I’d like to confirm the recommended and supported way to declare and install the MSAL dependency so that users don’t encounter a runtime crash.


Error

Traceback (most recent call last):
  File "/home/learnadmin/xss.py", line 65, in <module>
    import msal
ModuleNotFoundError: No module named 'msal'

Environment

OS: Linux

Python version: 3.x

pip: latest

Virtual environment: yes


Steps to reproduce

git clone <REPO_URL>
cd <REPO_DIR>

python3 -m venv .venv
source .venv/bin/activate

python3 -m pip install --upgrade pip
python3 xss.py

Expected behavior

One of the following:

msal is installed automatically as a dependency, or

Documentation clearly states that msal must be installed manually, or

The script exits with a helpful message explaining the missing dependency.


Actual behavior

The script crashes immediately with:

ModuleNotFoundError: No module named 'msal'

Workaround

Manually installing MSAL resolves the issue:

python3 -m pip install msal

Questions

Is msal intended to be a mandatory dependency for scripts that use Microsoft identity authentication flows?

What is the recommended way to declare this dependency for Python users (for example, requirements.txt vs pyproject.toml)?

Is there a preferred pattern for gracefully handling a missing msal dependency at runtime?


Additional context

The script imports msal directly without guarding the import or documenting it as a required dependency. Clarifying the expected setup would make onboarding and testing smoother for new users.

Thanks in advance for any guidance.

Question: Python script fails with ModuleNotFoundError: No module named 'msal' — recommended way to declare MSAL dependency?

Description

I’m running a Python script that imports the Microsoft Authentication Library for Python (msal) at startup. When executed in a clean virtual environment, the script fails immediately with a ModuleNotFoundError because msal is not installed automatically.

I’d like to confirm the recommended and supported way to declare and install the MSAL dependency so that users don’t encounter a runtime crash.


Error

Traceback (most recent call last):
  File "/home/learnadmin/xss.py", line 65, in <module>
    import msal
ModuleNotFoundError: No module named 'msal'

Environment

OS: Linux

Python version: 3.x

pip: latest

Virtual environment: yes


Steps to reproduce

git clone <REPO_URL>
cd <REPO_DIR>

python3 -m venv .venv
source .venv/bin/activate

python3 -m pip install --upgrade pip
python3 xss.py

Expected behavior

One of the following:

msal is installed automatically as a dependency, or

Documentation clearly states that msal must be installed manually, or

The script exits with a helpful message explaining the missing dependency.


Actual behavior

The script crashes immediately with:

ModuleNotFoundError: No module named 'msal'

Workaround

Manually installing MSAL resolves the issue:

python3 -m pip install msal

Questions

Is msal intended to be a mandatory dependency for scripts that use Microsoft identity authentication flows?

What is the recommended way to declare this dependency for Python users (for example, requirements.txt vs pyproject.toml)?

Is there a preferred pattern for gracefully handling a missing msal dependency at runtime?


Additional context

The script imports msal directly without guarding the import or documenting it as a required dependency. Clarifying the expected setup would make onboarding and testing smoother for new users.

Thanks in advance for any guidance.

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-12-16T19:28:17.9833333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To resolve the ModuleNotFoundError: No module named 'msal', you need to ensure that the Microsoft Authentication Library for Python (MSAL) is installed in your Python environment. The recommended way to declare and install the MSAL dependency is to include it in your requirements.txt file. You can do this by adding the following line to your requirements.txt:

    msal
    

    After adding this line, you can install the dependencies by running:

    pip install -r requirements.txt
    

    Alternatively, if you are using pyproject.toml, you can declare the dependency like this:

    [tool.poetry.dependencies]
    msal = "*"
    

    This way, when users set up their environment, they will automatically install the required msal library along with other dependencies.

    To handle the missing msal dependency gracefully at runtime, you can wrap the import statement in a try-except block. Here's an example:

    try:
        import msal
    except ImportError:
        print("MSAL library is not installed. Please install it using 'pip install msal'.")
        exit(1)
    

    This will provide a clear message to users if they attempt to run the script without having the msal library installed, improving the onboarding experience.


    References:

    0 comments No comments

  2. Netanel Stern 0 Reputation points
    2025-12-16T19:40:05.24+00:00

    Here is the same content rewritten as a clear question for discussion, suitable for posting as a follow-up question/comment on Microsoft Learn:


    Could this ModuleNotFoundError be caused by running the script with sudo and bypassing the virtual environment?

    I’m trying to understand whether this error is environmental rather than a missing dependency.

    Repro steps:

    python3 -m venv venv
    source venv/bin/activate
    pip install msal
    sudo python3 xss.py
    

    The script fails at startup with:

    ModuleNotFoundError: No module named 'msal'
    

    However:

    • import msal works in the Python REPL while the venv is active

    Running the script without sudo works

    Running with sudo ./venv/bin/python xss.py also works

    This suggests that sudo python3 is using the system interpreter instead of the venv interpreter, causing site-packages from the venv to be ignored.

    Is this the expected behavior on Linux, and should the recommended guidance be:

    avoid sudo python3 when using venvs, or

    explicitly invoke the venv’s Python binary when sudo is required?

    Would appreciate confirmation or best-practice guidance from the team.


    If you want it shorter, more beginner-friendly, or more authoritative in tone, tell me and I’ll adjust it.Here is the same content rewritten as a clear question for discussion, suitable for posting as a follow-up question/comment on Microsoft Learn:


    Could this ModuleNotFoundError be caused by running the script with sudo and bypassing the virtual environment?

    I’m trying to understand whether this error is environmental rather than a missing dependency.

    Repro steps:

    python3 -m venv venv
    source venv/bin/activate
    pip install msal
    sudo python3 xss.py
    

    The script fails at startup with:

    ModuleNotFoundError: No module named 'msal'
    

    However:

    import msal works in the Python REPL while the venv is active

    Running the script without sudo works

    Running with sudo ./venv/bin/python xss.py also works

    This suggests that sudo python3 is using the system interpreter instead of the venv interpreter, causing site-packages from the venv to be ignored.

    Is this the expected behavior on Linux, and should the recommended guidance be:

    avoid sudo python3 when using venvs, or

    explicitly invoke the venv’s Python binary when sudo is required?

    Would appreciate confirmation or best-practice guidance from the team.


    If you want it shorter, more beginner-friendly, or more authoritative in tone, tell me and I’ll adjust it.


  3. Netanel Stern 0 Reputation points
    2025-12-17T09:04:32.1666667+00:00

    הנה ניסוח מקצועי וחד שאפשר להעלות כשאלה (או כתגובה לפתרון) ב-Microsoft Q&A או ב-GitHub, המשלב את השימוש ב-pyenv ובגרסת הפייתון החדשה (3.15.0a3):


    Title: Resolution & Discussion: Handling MSAL Dependencies in Early-Alpha Python (3.15.0a3) for SOC Ingestion Pipelines

    Question/Comment:

    I am documenting a resolution for the ModuleNotFoundError: No module named 'msal' error, specifically within the context of high-performance security tooling (SOC pipelines) running on cutting-edge environments.

    The Challenge:

    When running a standalone SOC ingestion script (leveraging msal, bottle, and pandas), the system failed to locate the Microsoft Authentication Library despite standard installation attempts. This is particularly sensitive in environments where multiple Python versions coexist, or when testing on early-alpha releases like Python 3.15.0a3.

    The Solution (Environment Isolation):

    The issue was successfully resolved by using pyenv to isolate the runtime and ensuring the dependency tree is explicitly bound to the alpha interpreter.

    Isolation: Used pyenv to install and set 3.15.0a3 as the local version.

    Explicit Binding: Instead of a generic pip install, the library was installed using the module-path of the specific interpreter:

    Bash

    python -m pip install msal bottle requests pandas
    

    Verification: Verified the ConfidentialClientApplication import within the alpha shell.

    Points for Architectural Discussion:

    Binary Wheel Availability: In Python 3.15.0a3, many common libraries (especially pandas or cryptography used by msal) do not yet have pre-compiled wheels.

    Question: What is the recommended strategy for ensuring stable msal performance when the library must be compiled from source on an alpha interpreter? Are there known C-extension compatibility issues with the new Python internals?

    SSL/TLS Contexts in 3.15:

    The script requires mandatory HTTPS via ssl.wrap_socket.

      _Question:_ Given the changes in the `ssl` module in recent Python versions, is `ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)` still the optimal approach for self-signed SOC dashboards in 3.15, or should we migrate to the newer `SSLContext` security policies?
    
      
      Dependency Management in "Onefile" Tools:
    
      
      For "Onefile" security tools designed for Non-Premium tenants, the dependency on msal adds weight.
    
      
         _Discussion:_ In a `pyenv` managed environment, is there a significant overhead to using MSAL's abstraction vs. a lightweight `requests`-only implementation for simple `client_credentials` flows?
    ```---
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.