简体   繁体   English

在调用Python脚本时,“。/ script.py”和“python script.py”之间有什么区别

[英]When invoking a Python script, what is the difference between “./script.py” and “python script.py”

One difference is that "./script.py" only works if script.py is executable (as in file permissions), but "python script.py" works regardless. 一个区别是“./script.py”仅在script.py可执行时有效(如在文件权限中),但“python script.py”无论如何都有效。 However, I strongly suspect there are more differences, and I want to know what they are. 但是,我强烈怀疑存在更多差异,我想知道它们是什么。

I have a Django website, and "python manage.py syncdb" works just fine, but "./manage.py syncdb" creates a broken database for some reason that remains a mystery to me. 我有一个Django网站,“python manage.py syncdb”工作正常,但“./manage.py syncdb”由于某种原因创建了一个损坏的数据库,这对我来说仍然是一个谜。 Maybe it has to do with the fact that syncdb prompts for a superuser name and password from the command line, and maybe using "./manage.py syncdb" changes the way it interacts with the command line, thus mangling the password. 也许这与syncdb从命令行提示输入超级用户名和密码的事实有关,并且可能使用“./manage.py syncdb”更改它与命令行交互的方式,从而破坏了密码。 Maybe? 也许? I am just baffled by this bug. 我对此错误感到困惑。 "python manage.py syncdb" totally fixes it, so this is just curiosity. “python manage.py syncdb”完全修复它,所以这只是好奇心。

Thanks. 谢谢。

Edit: Right, right, I forgot about the necessity of the shebang line #!/usr/bin/python. 编辑:对,对,我忘记了shebang line#!/ usr / bin / python的必要性。 But I just checked, "python manage.py syncdb" and "./manage.py syncdb" are using the same Python interpreter (2.7.2, the only one installed, on Linux Mint 12). 但我刚刚检查过,“python manage.py syncdb”和“./manage.py syncdb”正在使用相同的Python解释器(2.7.2,在Linux Mint 12上安装的唯一一个)。 Yet the former works and the latter does not. 然而前者的作品却没有。

Could the environment variables seen by the Python code be different? Python代码看到的环境变量可能不同吗? My code does require $LD_LOADER_PATH and $PYTHON_PATH to be set special for each shell. 我的代码确实要求$ LD_LOADER_PATH和$ PYTHON_PATH为每个shell设置特殊。

Calling ./script.py uses the "shebang line" in the script to determine which interpreter to use to run the script. 调用./script.py使用脚本中的“shebang行”来确定用于运行脚本的解释器。 Such a line might look like 这样的线可能看起来像

#!/usr/bin/env python

or 要么

#!/usr/bin/python2.7

or whatever path to the python interpreter is used. 或者使用python解释器的任何路径。 If it resolves to the same Python interpreter that is called by just 如果它解析为由just调用的相同Python解释器

python

from the shell command line, there is no difference between ./script.py and python script.py , but the two version can end up using different Python interpreters. 从shell命令行,则没有什么区别./script.pypython script.py ,但两个版本可以使用不同的Python解释结束了。

./script.py = "Attempt to execute a file called script.py in the current shell" ./script.py =“尝试在当前shell中执行名为script.py的文件”

python script.py = "Send script.py as an argument to the first python executable in the current $PATH " python script.py =“将script.py作为参数发送到当前$PATH的第一个python可执行文件”

The first only works if the file has the execute bit set for the user attempting to execute the file and it has the so-called shebang line, which tells the shell how to run it. 第一个只有在文件具有为尝试执行文件的用户设置的执行位它具有所谓的shebang行时才有效,该行告诉shell如何运行它。

./script.py runs the interpreter defined in the #! ./script.py运行#!定义的解释器#! at the beginning of the file. 在文件的开头。 For example, the first line might be #! /usr/bin/env python 例如,第一行可能是#! /usr/bin/env python #! /usr/bin/env python or #! /usr/bin/python #! /usr/bin/env python#! /usr/bin/python #! /usr/bin/python or something else like that. #! /usr/bin/python或类似的东西。 If you look at what interpreter is invoked, you might be able to fix that problem. 如果您查看调用的解释器,您可能能够解决该问题。

In Linux using terminal you can execute any file -if the user has execute permission- by typing ./fileName . 在使用终端的Linux中,您可以通过键入./fileName来执行任何文件 - 如果用户具有执行权限。 When the OS sees a valid header like #! /usr/bin/python 当OS看到像#! /usr/bin/python这样的有效标题时#! /usr/bin/python #! /usr/bin/python (or for perl #! /usr/bin/python ), It will call the python or perl (appropriate) interpreter to execute the program. #! /usr/bin/python (或perl #! /usr/bin/python ),它将调用python或perl(相应的)解释器来执行程序。 You can use the command python script.py directly because, python is a executable program located at /usr/bin (or somewhere else) which is in a environmental variable $PATH, that corresponding to directory of executables. 您可以直接使用命令python script.py ,因为python是位于/usr/bin (或其他地方)的可执行程序,它位于环境变量$ PATH中,对应于可执行文件的目录。

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

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