St.write not working with vscode

Summary

Running streamlit app in vscode.

Steps to reproduce

Run app using terminal command python -m streamlit run streamlit2.py or streamlit run streamlit2.py both causes an error
Code snippet:

import streamlit as st
st.write("hello world!")

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

to reproduce the error, run python -m streamlit run streamlit2.py or streamlit run streamlit2.py

Code snippet
st.write(“hello world!”)
Expected behavior:

Explain what you expect to happen when you run the code above.

I expect to see A browser window should pop-up showing the newly created Streamlit app. This is the day 2 streamlit challenge I am trying to complete

Actual behavior:


Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Error

PS C:\Users\cef19> python -m streamlit run streamlit2.py
Traceback (most recent call last):
File “C:\ProgramData\Anaconda3\lib\runpy.py”, line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File “C:\ProgramData\Anaconda3\lib\runpy.py”, line 144, in _get_module_details
return _get_module_details(pkg_main_name, error)
File “C:\ProgramData\Anaconda3\lib\runpy.py”, line 111, in get_module_details
import(pkg_name)
File "C:\ProgramData\Anaconda3\lib\site-packages\streamlit_init
.py", line 55, in
from streamlit.delta_generator import DeltaGenerator as DeltaGenerator
File “C:\ProgramData\Anaconda3\lib\site-packages\streamlit\delta_generator.py”, line 38, in
from streamlit import config, cursor, env_util, logger, runtime, type_util, util
File “C:\ProgramData\Anaconda3\lib\site-packages\streamlit\cursor.py”, line 18, in
from streamlit.runtime.scriptrunner import get_script_run_ctx
File "C:\ProgramData\Anaconda3\lib\site-packages\streamlit\runtime_init
.py", line 16, in
from streamlit.runtime.runtime import Runtime as Runtime
File “C:\ProgramData\Anaconda3\lib\site-packages\streamlit\runtime\runtime.py”, line 26, in
from streamlit.proto.BackMsg_pb2 import BackMsg
File “C:\ProgramData\Anaconda3\lib\site-packages\streamlit\proto\BackMsg_pb2.py”, line 5, in
from google.protobuf.internal import builder as builder
ImportError: cannot import name ‘builder’ from ‘google.protobuf.internal’ (C:\ProgramData\Anaconda3\lib\site-packages\google\protobuf\internal_init
.py)
PS C:\Users\cef19>

Error when run with debugging in vs code
Exception has occurred: AttributeError
partially initialized module ‘streamlit’ has no attribute ‘write’ (most likely due to a circular import)
File “C:\Users\cef19\Desktop\ChrisFranklinStreamLit2\streamlit.py”, line 3, in
st.write(‘hello world!’)
File “C:\Users\cef19\Desktop\ChrisFranklinStreamLit2\streamlit.py”, line 1, in
import streamlit as st

Debug info

  • Streamlit version: (get it with $ streamlit version) Requirement already satisfied: streamlit in c:\programdata\anaconda3\lib\site-packages (1.16.0)

  • Python version: (get it with $ python --version) Python 3.8.5

  • Using Conda? PipEnv? PyEnv? Pex? using Conda

  • OS version: PC

  • Browser version: Chrome

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.

Not sure what a requirements file is? Check out [this doc]
My requirements file.
altair

pandas

streamlit

numpy

matplotlib

scikit-learn

(App dependencies - Streamlit Docs) and add a requirements file to your app.

Links

Additional information

Pics

If needed, add any other context about the problem here.
I will try this with pycharm but vscode should work right?

it is working in pycharm but can you still help me find out why vscode is not working with streamlit. I have pip install streamlit multiple times on vs code so I know its loaded but still not working.
Here is me pip installing streamlit again put it not being recognized in my import streamlit as st. in vscode. I cant believe I am the only one trying to use vscode on windows but not sure. I know alot of people use other os’s. does windows and vscode work with for others?


vscode

The screenshot of the error message in the first post indicates there was merely a typo and the ending quotes were missing.

As for VS Code. Make sure you are consistent with which environment you are in.

I created a virtual environment and then created a streamlit.py app in that virtual environment. it does not work in there either. Import streamlit as st does not work so streamlit is not recognized for anything.
I will Send screenshots soon showing my streamlit app created in the virtual environment and the error i get thanks.

I got it to work. created a streamlit_app.py file in the venv virtual environment You can now view your Streamlit app in your browser.

Local URL: http://localhost:8502
Network URL: http://192.168.0.176:8502

Yep! :+1: You see the (.venv) prefix on your command line to tell you that your terminal is running in that virtual enviroment? That’s the python.exe that’s going to be used when you type streamlit run streamlit_app.py.

If you are still having any issues with your code highlighting text correctly, you can make sure VS Code is using that same environment for interpretation, which was what I circled in the lower right of your first screen shot. (That’s how VS Code knows if a module exists or not and what methods are available to use for it so that you don’t get everything underlined in red squiggles.)

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