Requirement Satisfied, but ModuleNotFound

Hey,

Struggling to get basic usage going. Currently working in a venv and have the following requirements.txt

pymongo==4.2.0
streamlit==1.12.2

The code snippet

import streamlit as st
import pymongo

st.text("This is my first Mongo")

When executed with streamlit run fails with error ModuleNotFoundError: No module named 'pymongo

Seems simple enough. But I can confirm when I run a simple python script in the same ENV // directory // file, pymongo exists and executes without error:

def main():
  import pymongo
  import streamlit
  print("ok")

if __name__ == "__main__":
  main()

Also when I add the line os.system("pip3 install pymongo") into streamlit script, prior to import…

import streamlit as st
import os

os.system("pip3 install pymongo")
import pymongo

st.text("This is my first Mongo")

… terminal outputs that Requirement already satisfied: pymongo in ./ENVmongo/lib/python3.9/site-packages (4.2.0) yet still result in the ModuleNotFoundError:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 556, in _run_script
    exec(code, module.__dict__)
  File "{file}.py", line 6, in <module>
    import pymongo
ModuleNotFoundError: No module named 'pymongo'

Have installed/uninstalled both packages and sudo hard installed pymongo into python3.9 directory, but no changes. Also uninstalled everything and followed this thread’s instructions with no success.

Let me know what I am missing, thanks

Hi @abb5678, welcome to the Streamlit community! :wave:

What you’re facing is very likely an environment mismatch issue. When you execute streamlit run, it’s likely using a different Python installation than the one pymongo was installed in with pip.

Could you please share the output of:

which pip
which python

and

which streamlit

Also, try running your app with python -m streamlit run yourapp.py and see if that helps?

which pip

produces /Users/{me}/.streamlit_ENV/bin/pip

which python

produces /Users/{me}/.streamlit_ENV/bin/python

which streamlit

produces /usr/local/bin/streamlit !!

And python -m streamlit run yourapp.py runs successfully with pymongo import!

This addressed my issue, thank you so much @snehankekre!

Is there a reason my streamlit install was not using the ENV directory?