dify-sandbox 使用python 虚拟环境本地开发

张开发
2026/4/17 19:24:18 15 分钟阅读

分享文章

dify-sandbox 使用python 虚拟环境本地开发
dify-sandbox 使用python 虚拟环境本地开发为啥使用虚拟环境环境准备启动服务验证相关接口为啥使用虚拟环境本地直接使用 pip install 提示错误信息要在系统范围内安装包建议使用apt install python3-xyz。系统范围的依赖不建议修改各开发环境之间依赖还是隔离开比较好各个环境可能需要不同版本的依赖包$ pipinstallhttpx error: externally-managed-environment × This environment is externally managed ╰─ToinstallPython packages system-wide, tryaptinstallpython3-xyz, where xyz is the package you are trying to install. If you wish toinstalla non-Debian-packaged Python package, create a virtual environment using python3-mvenv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. If you wish toinstalla non-Debian packaged Python application, it may be easiest to use pipxinstallxyz,whichwill manage a virtual environmentforyou. Make sure you have pipx installed. See /usr/share/doc/python3.12/README.venvformoreinformation. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP668forthe detailed specification.环境准备创建虚拟环境$ python3-mvenv .dify-sandbox-python3激活虚拟环境$source.dify-sandbox-python3/bin/activate检查 python 命令$ python Python3.12.3(main, Mar32026,12:15:18)[GCC13.3.0]on linux Typehelp,copyright,creditsorlicenseformoreinformation.查看相关pip 配置$ pip config list global.index-urlhttps://mirrors.ustc.edu.cn/pypi/simple获取 python 默认加载模块路径$ python Python3.12.3(main, Mar32026,12:15:18)[GCC13.3.0]on linux Typehelp,copyright,creditsorlicenseformoreinformation.importsysprint(sys.path)[,/usr/lib/python312.zip,/usr/lib/python3.12,/usr/lib/python3.12/lib-dynload,/home/scd/code/go/dify/.dify-sandbox-python3/lib/python3.12/site-packages]修改 dify-sandbox 的 conf/config.yaml将第5步获取的模块路径复制到 python_lib_pathapp:port:8194debug:Truekey:dify-sandboxmax_workers:4max_requests:50worker_timeout:5python_path:pythonnodejs_path:/usr/local/node-v24.14.1/bin/nodepython_lib_path:-/usr/lib/python3.12-/home/scd/code/go/dify/.dify-sandbox-python3/lib/python3.12/site-packages-/usr/lib/x86_64-linux-gnu-/etc/ssl/certs/ca-certificates.crt-/etc/nsswitch.conf-/etc/hosts-/etc/resolv.conf-/etc/localtime-/usr/share/zoneinfo-/etc/timezoneenable_network:True# please make sure there is no network risk in your environmentenable_preload:False# please keep it as False for security purposeslog_path:./logs# path to store log files, default is ./logsallowed_syscalls:# please leave it empty if you have no idea how seccomp worksproxy:socks5:http:https:启动服务验证相关接口清除sandbox-python相关的 lib 包避免影响验证环境是否可行# rm -rf /var/sandbox/sandbox-python/*激活虚拟环境启动服务# source ../.dify-sandbox-python3/bin/activate# ./main{time:2026-04-16T22:19:35.06861774308:00,level:INFO,msg:initializing nodejs runner environment}{time:2026-04-16T22:19:35.12430063708:00,level:INFO,msg:nodejs runner environment initialized}{time:2026-04-16T22:19:35.12527312208:00,level:INFO,msg:initializing python runner environment}{time:2026-04-16T22:19:35.12668658208:00,level:INFO,msg:python runner environment initialized}{time:2026-04-16T22:19:35.12714741308:00,level:INFO,msg:network has been enabled}{time:2026-04-16T22:19:35.12718986508:00,level:INFO,msg:config init success}{time:2026-04-16T22:19:35.12725655608:00,level:INFO,msg:runner dependencies init success}[GIN-debug][WARNING]Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug][WARNING]Runningindebugmode. Switch toreleasemodeinproduction. - using env:exportGIN_MODErelease - using code: gin.SetMode(gin.ReleaseMode)[GIN-debug]GET /health --github.com/langgenius/dify-sandbox/internal/controller.Setup.func1(5handlers){time:2026-04-16T22:19:35.12733115908:00,level:INFO,msg:setting max requests,max:50}{time:2026-04-16T22:19:35.12734976308:00,level:INFO,msg:setting max workers,max:4}[GIN-debug]POST /v1/sandbox/run --github.com/langgenius/dify-sandbox/internal/controller.RunSandboxController(9handlers)[GIN-debug]GET /v1/sandbox/dependencies --github.com/langgenius/dify-sandbox/internal/controller.GetDependencies(6handlers)[GIN-debug]POST /v1/sandbox/dependencies/update --github.com/langgenius/dify-sandbox/internal/controller.UpdateDependencies(6handlers)[GIN-debug]GET /v1/sandbox/dependencies/refresh --github.com/langgenius/dify-sandbox/internal/controller.RefreshDependencies(6handlers)[GIN-debug][WARNING]You trusted all proxies, this is NOT safe. We recommend you toseta value. Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.[GIN-debug]Listening and serving HTTP on :8194{time:2026-04-16T22:19:35.12744400408:00,level:INFO,msg:installing python dependencies}{time:2026-04-16T22:19:35.12752681108:00,level:INFO,msg:python dependencies installed}{time:2026-04-16T22:19:35.12753982808:00,level:INFO,msg:initializing python dependencies sandbox}运行测试接口test_dify_sandbox.sh 脚本内容$cattest_dify_sandbox.sh#!/bin/bashREQUEST_HOSThttp://localhost:8194X_API_KEYdify-sandbox# 健康检查接口echo健康检查接口:/healthcurl${REQUEST_HOST}/health# Python3 示例echoechoPython3 运行代码:/v1/sandbox/runcurl-XPOST${REQUEST_HOST}/v1/sandbox/run\-HContent-Type: application/json\-HX-Api-Key:${X_API_KEY}\-d{ language: python3, code: import json\nperson {\name\: \John\, \age\: 30, \city\: \New York\}\njson_str json.dumps(person)\nprint(json_str), preload: , enable_network: false }echoechoNode.js 运行代码:/v1/sandbox/run# Node.js 示例curl-XPOST${REQUEST_HOST}/v1/sandbox/run\-HContent-Type: application/json\-HX-Api-Key:${X_API_KEY}\-d{ language: nodejs, code: const person {name: \John\, age: 30, city: \New York\};\nconst jsonString JSON.stringify(person);\nconsole.log(jsonString);, preload: , enable_network: false }echoecho更新依赖 复制python_lib_path依赖:/v1/sandbox/dependencies/updatecurl-XPOST${REQUEST_HOST}/v1/sandbox/dependencies/update\-HContent-Type: application/json\-HX-Api-Key:${X_API_KEY}\-d{language: python3}echoecho刷新依赖dependencies/python-requirements.txt依赖:/v1/sandbox/dependencies/refreshcurl-XGET${REQUEST_HOST}/v1/sandbox/dependencies/refresh\-HContent-Type: application/json\-HX-Api-Key:${X_API_KEY}\-d{language: python3}echoecho获取已安装依赖:/v1/sandbox/dependenciescurl-XGET${REQUEST_HOST}/v1/sandbox/dependencies\-HContent-Type: application/json\-HX-Api-Key:${X_API_KEY}\-d{language: python3}echo执行结果$shtest_dify_sandbox.sh 健康检查接口:/healthokPython3 运行代码:/v1/sandbox/run{code:0,message:success,data:{error:,stdout:{\name\:\John\,\age\: 30,\city\:\New York\}\n}}Node.js 运行代码:/v1/sandbox/run{code:0,message:success,data:{error:,stdout:{\name\:\John\,\age\:30,\city\:\New York\}\n}}更新依赖 复制python_lib_path依赖:/v1/sandbox/dependencies/update{code:0,message:success,data:{}}刷新依赖dependencies/python-requirements.txt依赖:/v1/sandbox/dependencies/refresh{code:0,message:success,data:{dependencies:[{name:jinja2,version:},{name:httpx,version:},{name:requests,version:}]}}获取已安装依赖:/v1/sandbox/dependencies{code:0,message:success,data:{dependencies:[{name:jinja2,version:},{name:httpx,version:},{name:requests,version:}]}}

更多文章