问题描述
使用vscode对startup.py进行调试出现报错:
Traceback (most recent call last):
File "/home/xxx/anaconda3/bin/streamlit", line 7, in <module>
from streamlit.cli import main
ModuleNotFoundError: No module named 'streamlit.cli'
解决方案
根据报错分析,其使用的streamlit并不是我所配置conda环境中的streamlit,需要手动更改。
找到startup.py大概450行的以下代码:
cmd = ["streamlit", "run", "webui.py",
"--server.address", host,
"--server.port", str(port),
"--theme.base", "light",
"--theme.primaryColor", "#165dff",
"--theme.secondaryBackgroundColor", "#f5f5f5",
"--theme.textColor", "#000000",
]
改成:
cmd = ["/home/xxx/anaconda3/envs/my_env/bin/streamlit", "run", "webui.py",
"--server.address", host,
"--server.port", str(port),
"--theme.base", "light",
"--theme.primaryColor", "#165dff",
"--theme.secondaryBackgroundColor", "#f5f5f5",
"--theme.textColor", "#000000",
]
但这样做不够通用,可以结合.vscode中的lauch.json做以下修改:
1.设置环境变量
在lauch.json中添加
"env": {"INTERPRETER_PATH": "${command:python.interpreterPath}"}
在langchainchatchat中添加代码debug_interpreter.py:
import os
class interpreter(object):
__python = 'python'
__streamlit = 'streamlit'
def __init__(self):
interpreter = os.getenv('INTERPRETER_PATH')
if interpreter != None:
self.__python = interpreter
self.__streamlit = interpreter[:interpreter.rfind('/')]+'/streamlit'
def get_python(self):
return self.__python
def get_streamlit(self):
return self.__streamlit
INTERPRETER = interpreter()
最后修改startup.py:
cmd = [INTERPRETER.get_streamlit(), "run", "webui.py",
"--server.address", host,
"--server.port", str(port),
"--theme.base", "light",
"--theme.primaryColor", "#165dff",
"--theme.secondaryBackgroundColor", "#f5f5f5",
"--theme.textColor", "#000000",
]
此方案也可以解决conda环境下vscode调试一些嵌套调用的python脚本出现的No module named xx的问题