简体   繁体   English

使用命令行输入修改在终端上运行的 python 脚本以在 IDE 中工作

[英]Modify a python script that runs on terminal with command line input to work in an IDE

I am playing around with a python software-like code that I downloaded.我正在玩我下载的类似 python 软件的代码。 I need to extract some parts of the code and so I am currently tracing it to understand it better.我需要提取代码的某些部分,所以我目前正在跟踪它以更好地理解它。 It would make my life much easier to trace the code in an IDE (I use spyder) so that I can put breakpoints and visually inspect the variables.在 IDE 中跟踪代码(我使用 spyder)会让我的生活变得更加轻松,这样我就可以放置断点并直观地检查变量。 I therefore want to modify the code but I don't know how to proceed.因此,我想修改代码,但我不知道如何进行。

This is the directory structure of the code.这是代码的目录结构。 The main directory is "advance" and 2 of its subdirectories are "advance" and "examples".主目录是“advance”,其 2 个子目录是“advance”和“examples”。 The code was installed using pip and to run the code, one must go to the examples directory, enter a sub-directory which represents a test case, and simply type "advance ."代码是使用 pip 安装的,要运行代码,必须进入 examples 目录,进入一个代表测试用例的子目录,然后简单地输入“advance”。 in the terminal (the "." represents pwd).在终端中(“.”代表密码)。

After some tracing, I found out that doing this calls the file driver.py with address advance/advance/driver.py.经过一些跟踪,我发现这样做会调用文件 driver.py,地址为 Advance/advance/driver.py。 The driver.py code has the structure below: driver.py 代码的结构如下:

# import stuff here

def main():
    
    parser = argparse.ArgumentParser(description="Read working directory")
    
    # some stuff here


if __name__ == "__main__":
    try:
        main()
    except:
        print(traceback.format_exc())
        print("Execution failed")

As you can see, the main function takes an input which is the working directory.如您所见,main 函数接受一个输入,即工作目录。

My question now is: How can I modify this so that I can run this code in my IDE?我现在的问题是:如何修改它以便我可以在我的 IDE 中运行此代码? Or better yet, can I write a script that calls the main function in driver.py and give it the path of the directory for the example test case I want to run?或者更好的是,我可以编写一个脚本来调用 driver.py 中的 main 函数,并为其提供我想要运行的示例测试用例的目录路径吗? If so, how can I do it?如果是这样,我该怎么做?

Thanks and apologies if it sounds too simple.如果听起来太简单,谢谢并道歉。 I am coming from Matlab and transitioning to python.我来自 Matlab 并过渡到 python。

I found in this reddit post that spyder offers to pass command line options, when running a script.我在这篇 reddit 帖子中发现 spyder 提供在运行脚本时传递命令行选项。 See the following setting:请参阅以下设置:

Run -> Configure -> Command line options

Unfortunately, I am not familiar with Spyder, so that's what I can tell so far.不幸的是,我对 Spyder 并不熟悉,所以到目前为止我只能说这么多。 I guess it works similar to PyCharm (which I use).我想它的工作原理类似于 PyCharm(我使用的)。 There it is like this: You adapt your run configuration and can set the parameters you want to pass a script.它是这样的:您可以调整运行配置,并可以设置要传递脚本的参数。 Afterwards, when running that run configuration PyCharm will always pass those particular parameters to the script.之后,在运行该运行配置时,PyCharm 将始终将这些特定参数传递给脚本。 This allows to use the IDE's debugger to examine the code.这允许使用 IDE 的调试器来检查代码。 I would bet that Spyder behaves almost the same way.我敢打赌,Spyder 的行为方式几乎相同。 Good luck trying the suggested settings :)祝您尝试建议的设置好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我使用 Paramiko 的 Python 脚本在 IDE (PyCharm) 和命令行中完美运行,但无法在 AWS Lambda 上运行 - My Python script using Paramiko runs perfectly in the IDE (PyCharm) and command line but doesn't run on AWS Lambda Python脚本的命令行输入 - Command Line input for Python Script Python 代码从 IDE 运行,而不是从终端运行 - Python code runs from IDE, but not from terminal 如何在Windows命令行中使运行python的命令成为脚本? - How to make a command that runs python a script in Windows command line? 如何修改 python 脚本以使用外部文件作为命令行中的输入? - How do I modify a python script to use an external file as the input in the command line? Python脚本在终端中运行,但不作为Apache运行 - Python script runs in terminal but not as Apache Python脚本在命令行上运行,但不是从.sh文件运行 - Python script runs on command line but not from .sh file python脚本从命令行运行,但是使用子进程会出错 - python script runs from command line but using subprocess gives error Python脚本需要在命令行中输入 - Python script requires input in command line 在 Mac(终端)/Windows(命令行)中自动执行 Python 脚本 - Automating Python script execution in Mac(Terminal) /Windows(Command line)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM