Getting a 'ModuleNotFoundError' while making absolute imports to modules present in the same directory level via the python script containing the code for Streamlit application

Hello team,

The structure of my python project is as follows :-

Main_project_directory
|
|--------dev
|          |------data_preparation
|          |                     |
|          |                     ---Module_1.py
|          |                     |
|          |                     ---__init__.py
|          |-------------------models
|          |                        |     
|          |                        ----Module_2.py
|          |                        |
|          |                        ---__init__.py
|          |-------------------Module_3.py
|          |
|          |-------------------__init__.py
|
|-------app
|          |
|          ---streamlit_app.py
|          |   
|          ---__init__.py 
|-------__init__.py

I am currently trying to perform absolute import in the script :- “streamlit_app.py” to get functions and classes implemented in the modules present in ‘Main_project_directory/dev’, ‘Main_project_directory/dev/data_preparations’ and ‘Main_project_directory/dev/models’ like:-
“from Main_project_directory.dev.data_preparation.Module_1 import func1”.

However, I am getting an error when I run the Streamlit application using the script :- “streamlit_app.py” :-

ModuleNotFoundError: No module named 'Main_project_directory'
Traceback:
File "c:\eds\current_tasks\vqi\pull_requests\pull_request_6\Main_project_directory\ui\venv_2\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 554, in _run_script
    exec(code, module.__dict__)
File "C:\EDS\Current_tasks\VQI\Pull_requests\Pull_request_6\Main_project_directory\ui\ui.py", line 33, in <module>
    from ui_config import TEMP_DIRECTORY,PRE_TRAINED_MODEL_LIST,l_category_dict,SPLIT_DIRECTORY,l_train_args_dict,INIT_LR,LICENSES,INFO,HYPERPARAMS
File "C:\EDS/Current_tasks/VQI/Pull_requests/Pull_request_6/Main_project_directory/ui\ui_config.py", line 6, in <module>
    from Main_project_directory.dev.models_pytorch.models import Models

Can someone please suggest how I can resolve this error ?

1 Like

I am facing the exact same issue. I tried the solutions suggested here but this did not resolve it. The issue does not arise when my import modules are located in the same folder as streamlit_app.py

1 Like

Exactly ! The same issue doesn’t happen when all the developmental modules are present in the same directory. Also, all the modules work well in tandem when I am not using them as a Streamlit application regardless of the directory they are maintained in.

1 Like

I found a solution to using a launch.json file in VS code inspired by this. In the project root folder, you need a folder .vscode, where you create the following launch.json file:

{
    "configurations": [
        {
            "name": "Current streamlit file",
            "type": "python",
            "request": "launch",
            "module": "streamlit",
            "args": ["run", "${file}"],
            "cwd": "${workspaceFolder}",
            "env": {"PYTHONPATH": "${workspaceFolder}/...[**potential subdirectories**].../${pathSeparator}${env:PYTHONPATH}"}
        }
    ]
}

Then navigate to your streamlit file, navigate to Run and Debug on the left pane in VS Code, select ‘Current streamlit file’ and press the play button (see screenshot). Now the app should launch and imports should work properly.

Note: if the solution didn’t work, try out different levels of subdirectories in the above PYTHONPATH variable in launch.json file to make it work.

image

Hi @stefkovic , many thanks for the reply. Could you please tell me how I can apply the same workaround on a PyCharm project ?

1 Like

Hi @Shashank_Shekhar, I haven’t tried it for a PyCharm project. Maybe you can find out how to use your own run/debug configurations in PyCharm, for example like this. Not sure if this is the way to go in PyCharm, though.

Also note that running the app with a launch.json file made my app slower. I found out that when I run the app from the VS Code Python interpreter as usual (streamlit run 'path/file.py) , a reload of my app based on a dropdown takes 5 seconds, while with the launch.json file it takes 20 seconds. So even if you get my solution to work in PyCharm, it may make your app slower.

Been having the same problem! Got things running using python -m streamlit run app.py

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.