为什么我通过 bash 脚本打开的 xterm 终端无法找到漂亮的模块?

问题描述

我有一个使用漂亮表的 python 文件。我还有一个 bash 脚本,可以使用 xQuartz 打开并运行它。当我打开 xQuartz 并从那里运行文件时,它按预期工作,但是当我尝试使用脚本运行它时,它无法找到我的漂亮模块。可能会发生什么?

bash 脚本行:

xterm -geometry 55x24+000+000 -hold -e /bin/bash -l -c "python3 server.py 1"

在 xQuartz 终端上运行 python3 server.py 1 没问题。如果我从 mac 终端运行 xterm 并执行相同的操作,它也可以工作。

解决方法

正如@DiegoTorresMilano 所指出的,您可能正在运行不同版本的 python 3,具体取决于 ~/.bash_profile~/.bashrc 中的内容。如果您安装了多个版本的 python 3,这是可能的。

当您运行交互式非登录 bash 会话时,将首先获取您的 ~/.bash_profile,然后是您的 ~/.bashrc。由于您的 ~/.bashrc 将是第二个来源,因此它可以覆盖您的 ~/.bash_profile 中设置的内容。

当您使用 -l 选项运行 bash 时,这会告诉 bash 像登录 shell 一样运行。然后 "Invocation" section of the bash man page 告诉我们 ~/.bash_profile 将来自登录 shell,但不是 ~/.bashrc

您应该尝试在交互式 xQuartz 终端中运行 python3 --version。这将为您提供类似 Python x.y.z 的输出(例如,Python 3.8.5)。然后你可以通过在你的 bash 脚本中使用 pythonx.y 来运行那个特定的 python 版本(例如,如果你的 python3 --version 的输出是 Python 3.8.5,你应该在你的bash 脚本)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...