Skip to content

BugFix: Correctly extract top-level imports to prevent installation errors#31

Open
asddass wants to merge 1 commit into
ohtaman:mainfrom
asddass:fix-incorrect-import-extraction
Open

BugFix: Correctly extract top-level imports to prevent installation errors#31
asddass wants to merge 1 commit into
ohtaman:mainfrom
asddass:fix-incorrect-import-extraction

Conversation

@asddass
Copy link
Copy Markdown

@asddass asddass commented May 14, 2025

Description:

This pull request updates the extract_imports function in streamlit_desktop_app/build.py to only extract and utilize base module names during import detection. Previously, the function could return extended module paths (such as requests.Session) due to its current implementation. This behavior sometimes resulted in installation errors when the package manager incorrectly processed these extended import paths.

Problem:

The previous implementation of extract_imports iterated through the Abstract Syntax Tree (AST) of the Streamlit script and directly added the full import path to the set of dependencies. This resulted in the inclusion of nested module paths (e.g., requests.Session) as top-level requirements. Consequently, the application might attempt to install non-existent packages or encounter errors during the dependency resolution process. As highlighted in the provided example:

test_lines = """
import os
import streamlit as st
from dotenv import load_dotenv
import requests.Session as ses
from cul_cffi.requests import Session as sessi
"""

# output: ['cul_cffi.requests.Session', 'dotenv.load_dotenv', 'os', 'requests.Session', 'streamlit']

Solution:

This pull request modifies the extract_imports function to extract only the top-level module name from both import and from ... import ... statements. This ensures that only core packages are identified as dependencies.

The provided example demonstrates the effectiveness of this fix. The new implementation correctly extracts the top-level imports:

# new_output: ['cul_cffi', 'dotenv', 'os', 'requests', 'streamlit']

Impact:

I believe this enhancement will contribute to a more stable and user-friendly experience with streamlit-desktop-app by preventing installation issues related to incorrect module path usage with accurate dependency detection and streamlined dependency management.

Refactors the `extract_imports` function to only identify and use base module names.
This change improves the stability and reliability of the dependency management.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant