ModuleNotFoundError: No module named 'st_pages'/'openpyxl'

Hi Streamlit community,

I am having quite a hard time deploying an app through AWS CDK. I have requirements.txt, and do “import xxx” in my .py files. I even specify versions of st-pages in requirements.txt. The app is working fine on my remote desktop. This is the error I keep receiving:

ModuleNotFoundError: No module named 'st_pages'
Traceback:

File "/opt/program/peru-virtual-env/.venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^File "/opt/program/peru-virtual-env/.venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)File "/opt/program/peru-virtual-env/lib/streamlit_app/main.py", line 10, in <module>
    from st_pages import Page, add_page_title, get_nav_from_toml

I have read many posts related to ModuleNotFoundError but still couldn’t find a solution for my error. Any idea how I could resolve this please?

That works in the Community Cloud but that is not a Streamlit feature. I think that in this case, you need to manage the app dependencies yourself before running the app, for example, doing pip install -r requirements.txt before streamlit run my_app.py.

Hi @edsaac,

The app is running fine locally (or in my remote desktop), but it throws error when I deploy it.

I commented out st_pages and removed functions using st_pages, but now it throws another similar error for Openpyxl:

File "/opt/program/peru-virtual-env/.venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
File "/opt/program/peru-virtual-env/.venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
File "/opt/program/peru-virtual-env/lib/streamlit_app/main.py", line 14, in <module>
    from utils.upload_parse_files import upload_file, generate_ad_audit, change_partner_name
File "/opt/program/peru-virtual-env/lib/streamlit_app/utils/upload_parse_files.py", line 13, in <module>
    import openpyxl

I checked the location of Openpyxl using typing “pip show openpyxl”, it returned:
“~. CDK/src/streamlit_app/env/lib/python3.11/site-packages’”

I also checked “where python”, it returned: “~.CDK/src/streamlit_app/env/bin/python”.

Would it be possible that Openpyxl wasn’t installed for python, but for python 3.11? However, other libraries (e.g. streamlit )are installed in the same location in “python3.11/site-packages” as openpyxl and the deployment didn’t throw errors for that.

Here is a screenshot of the location:

My guess is that you have two (or possibly three) environments.

  1. From the exception, it seems you installed and are running Streamlit from the environment at /opt/program/peru-virtual-env/.venv/
  2. That one is different from the environment that is active in the terminal: ~.CDK/src/streamlit_app/env.
  3. And from the screenshot, it seems there might be a third one at ~.CDK/src/env?

If you run which python and which streamlit, do those paths point to the same environment?

Hi @edsaac, I solved this issue by adding RUN pip install openpyxlin Dockerfile. Thanks for your help.