我找到了一种至少可以从 IDE 运行代码的方法(在我的例子中是 PyCharm)。这streamlit run code.py
可以直接从 IDE 调用命令。 (这streamlit run code.py
命令实际上调用python -m streamlit.cli run code.py
,这是以前从 IDE 运行的解决方案。)
The -m streamlit run
goes into the interpreter options field of the Run/Debug Configuration (this is supported by Streamlit, so has guarantees to not be broken in the future1), the code.py goes into the Script path field as expected. In past versions, it was also working to use -m streamlit.cli run
in the interpreter options field of the Run/Debug Configuration, but this option might break in the future.
不幸的是,这种方式的调试似乎不起作用,因为 PyCharm 附加的参数被传递给 Streamlit 而不是 pydev 调试器。
Edit: Just found a way to debug your own scripts. Instead of debugging your script, you debug the streamlit.cli
module which runs your script. To do so, you need to change from Script path:
to Module name:
in the top-most field (there is a slightly hidden dropdown box there...). Then you can insert streamlit.cli
into the field. As the parameters, you now add run code.py
into the Parameters:
field of the Run/Debug Configuration.
编辑:添加 @sismo 的评论
如果您的脚本需要使用一些参数运行,您可以轻松地将它们添加为
run main.py -- --option1 val1 --option2 val2
注意第一条--
带空白:需要停止流式参数解析并传递给main.py
参数解析。
1 https://discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3 https://discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3