配置文件中的Bash函数未运行SimpleHTTPServer文件未找到

2024-06-15 20:40:34 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我在~/.profile中存储了一个简单的bash函数,如下所示:

function testingServer() { local port="${1:-8000}" python SimpleHTTPServer "$port" open "http://localhost:${port}/" }

但是当我运行函数时,它会给我一个错误:

python: can't open file 'SimpleHTTPServer': [Errno 2] No such file or directory

这对我来说似乎很奇怪,因为当我像这样直接运行命令时,一切都很正常:

python -m SimpleHTTPServer 8080

有什么线索可以告诉我该怎么做才能追查到这个吗?在


Tags: 函数bashlocalhosthttpportlocal错误function
1条回答
网友
1楼 · 发布于 2024-06-15 20:40:34

当您调用Python时,您不是缺少了-m参数吗?加上这一点,它应该可以工作了。在

function testingServer() {
    local port="${1:-8000}"
    python -m SimpleHTTPServer "$port"
    open "http://localhost:${port}/"
}

正确的是,SimpleHTTPServer不是一个文件(尤其不是本地目录中的文件)。但是-m表示它是一个模块,因此也指明了如何/在哪里找到它。在

从测试的角度来看,这是否正是您想要/需要做的事情是另一回事。但至少你可以摆脱can't open file错误。在

相关问题 更多 >