简体   繁体   English

使用命令行运行python脚本有什么优势?

[英]What is the advantage of running python script using command line?

I am beginner to python and programming in general. 我是python和一般编程的初学者。 As I am learning python, I am tying to develop a good habit or follow a good practice. 在学习python时,我想养成良好的习惯或遵循良好的习惯。 So let me first explain what I am currently doing. 因此,让我先解释一下我现在在做什么。

I use Emacs (prelude) to execute python scripts. 我使用Emacs(前奏)执行python脚本。 The keybinding Cc Cc evaluates the buffer which contains the python script. 绑定Cc Cc评估包含python脚本的缓冲区。 Then I get a new buffer with a python interpreter with >>> prompt. 然后,我得到一个带有>>>提示符的python解释器的新缓冲区。 In this environment all the variables used in the scripts are accessible. 在此环境中,可以访问脚本中使用的所有变量。 For example, if x and y were defined in the script, I can do >>> x + y to evaluate it. 例如,如果在脚本中定义了xy ,则可以执行>>> x + y进行评估。

I see many people (if not most) around me using command line to execute the python script (ie, $ python scriptname.py ). 我看到周围有很多人(如果不是最多的话)使用命令行执行python脚本(即$ python scriptname.py )。 If I do this, then I return to the shell prompt, and I am not able to access the variables x and y to perform x + y . 如果执行此操作,则返回到shell提示符,并且无法访问变量xy来执行x + y So I wasn't sure what the advantage of running python scripts using the command line. 所以我不确定使用命令行运行python脚本有什么好处。

Should I just use Emacs as a editor and use Terminal (I am using Mac) to execute the script? 我应该只使用Emacs作为编辑器并使用Terminal(我使用Mac)执行脚本吗? What is a better practice? 有什么更好的做法?

Thank you! 谢谢!

People use different tools for different purposes. 人们出于不同目的使用不同的工具。

An important question about the interface into any program is who is the user? 关于任何程序接口的一个重要问题是用户是谁? You, as a programmer, will use the interpreter to test a program and check for errors. 作为程序员,您将使用解释器测试程序并检查错误。 Often times, the user doesn't really need to access the variables inside because they are not interacting with the application/script with an interpreter. 通常,用户实际上并不需要访问其中的变量,因为它们没有通过解释器与应用程序/脚本进行交互。 For example, with Python web applications, there is usually a main.py script to redirect client HTTP requests to appropriate handlers. 例如,对于Python Web应用程序,通常有一个main.py脚本将客户端HTTP请求重定向到适当的处理程序。 These handlers execute a python script automatically when a client requests it. 这些处理程序在客户端请求时自动执行python脚本。 That output is then displayed to the user. 该输出然后显示给用户。 In Python web applications, unless you are the developer trying to eliminate a bug in the program, you usually don't care about accessing variables within a file like main.py (in fact, giving the client access to those variables would pose a security issue in some cases). 在Python Web应用程序中,除非您是开发人员尝试消除程序中的错误,否则通常不关心访问诸如main.py之类的文件中的变量(实际上,让客户端访问这些变量会带来安全性)在某些情况下会出现问题)。 Since you only need the output of a script, you'd execute that script function in command line and display the result to the client. 由于只需要脚本的输出,因此您可以在命令行中执行该脚本功能,并将结果显示给客户端。

About best practices: again, depends on what you are doing. 关于最佳做法:同样,取决于您在做什么。

Using the python interpreter for computation is okay for smaller testing of isolated functions but it doesn't work for larger projects where there are more moving parts in a python script. 对于较小的隔离功能测试,可以使用python解释器进行计算,但不适用于python脚本中包含更多可移动部件的大型项目。 If you have a python script reaching a few hundred lines, you won't really remember or need to remember variable names. 如果您的Python脚本有几百行,您将不会真正记住或需要记住变量名。 In that case, it's better to execute the script in command-line, since you don't need access to the internal components. 在这种情况下,最好在命令行中执行脚本,因为您不需要访问内部组件。

You want to create a new script file if you are fashioning that script for a single set of tasks. 如果您要为一组任务设置脚本,请创建一个新的脚本文件。 For example with the handlers example above, the functions in main.py are all geared towards handling HTTP requests. 例如,在上面的处理程序示例中,main.py中的函数均适用于处理HTTP请求。 For something like defining x, defining y, and then adding it, you don't really need your own file since you aren't creating a function that you might need in the future and adding two numbers is a built-in method. 对于诸如定义x,定义y然后将其相加之类的操作,您实际上并不需要自己的文件,因为您不会创建将来可能需要的函数,并且将两个数字相加是一个内置方法。 However, say you have a bunch of functions you've created that aren't available in a built-in method (complicated example: softmax function to reduce K dimension vector to another K dimension vector where every element is a value between 0 and 1 and all the elements sum to 1), you want to capture in a script file and cite that script's procedure later. 但是,假设您创建了很多函数,这些函数在内置方法中不可用(复杂的示例:softmax函数,用于将K维向量简化为另一个K维向量,其中每个元素的值都在0到1之间并且所有元素的总和为1),您想捕获到脚本文件中,并稍后引用该脚本的过程。 In that case, you'd create your own script file and cite it in a different python script to execute. 在这种情况下,您将创建自己的脚本文件,并在其他python脚本中引用该脚本文件以执行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM