首页 > 解决方案 > 从 Windows 到 Ubuntu 的 python 错误

问题描述

我有一个 python 脚本,它接受一个命令行参数,一个包含数据的目录,即python3 test.py /data/

我在 Windows 机器上用 Jupyter 编写了它。我在 Windows 终端中测试脚本,一切正常。我需要它在 Ubuntu 机器上运行。我下载了 Ubuntu LiveCD 只是为了测试它。我进入脚本和数据所在文件夹中的 Ubuntu 终端。我输入了这个确切的行:

python3 test.py /data/ 

我在第 91 行得到一个语法错误

image = plt.imread(directory_name + "image-" + f"{i:02d}" + ".jpg")

箭头指向 f"{i:02d}" 中的第二个“。我也尝试输入

python3 test.py

没有目录,我得到了同样的错误。奇怪的是我写了 try/except 来将目录存储到变量中,如果格式错误则抛出异常。同样,它在 Windows 中运行良好。这是 try/except 的代码:

#import directory location of images, catch exceptions
import sys

try:
    directory_name = sys.argv[1]
    print(directory_name)

except:
    print('Please pass directory_name')

请注意,它不会在 Ubuntu 中打印目录名称。我假设我收到错误是因为 Windows 的目录语法与 Ubuntu 的不同(我不太熟悉)。我该如何解决?

这是我看到的错误:

在此处输入图像描述

代码如上所述。脚本中的第一件事是 try/except,然后是几个自定义函数,然后是给出错误的行。

标签: pythonwindowsubuntu

解决方案


检查 Ubuntu 上的安装库。它没有预装在系统中,您使用。

import mathplotlib as plt

image = plt.imread(directory_name + "image-" + f"{i:02d}" + ".jpg")

推荐阅读