【问题标题】:Running python file with arguments works in terminal, but I need to use it in a different script在终端中运行带有参数的python文件,但我需要在不同的脚本中使用它
【发布时间】:2021-07-05 21:20:55
【问题描述】:

当我在终端中执行以下操作时,可以完美运行

python3 number_detection.py --train_predict 'P' --file 'Images/2/_newsize_1.png'

但是,我需要能够在不同的脚本中执行此任务。我正在循环浏览图像并希望返回它们被识别的内容。

所以在终端中运行上述代码可以正常工作,但我如何翻译该代码以在不同的 .py 文件中工作?

【问题讨论】:

  • 会推荐通过 python 中的模块。 docs.python.org/3/tutorial/modules.html。然后,您可以在需要时组织代码并重构模块。因此,您将在您的 different.py 文件中导入 number_detection.py,然后调用该函数。

标签: python python-3.x terminal command atom-editor


【解决方案1】:

也许有一种方法可以在不查看 number_detection.py 文件的情况下为您提供建议,但如果您发布该文件,它肯定会让我给您更具体的建议。

现在我想说的是,您很可能可以将所有内容放入您的 number_detection.py 文件中,然后将其转换为接受您的参数的 number_detection.py 函数

def number_detection(train_predict, file):
    #put your original code here 

然后在你的图像处理文件中,

from number_detection import number_detection

现在您可以在新文件中使用该其他功能

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多