运行Python脚本的方式差异 [英] Differences in the ways to running Python scripts

查看:88
本文介绍了运行Python脚本的方式差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的Python问题,使用Django的例子。运行Python脚本时,我必须先使用 python 命令在脚本文件名之前吗?



在Django教程中,我正在关注以下命令:

  django-admin.py startproject mysite 

但是,其他的都是这样的:

  python manage.py runserver 

为什么顶部的人不需要 python 命令?



或者,如果我的系统知道所有Python脚本都要由我的python解释器执行,那么为什么底部需要使用 python 命令

解决方案

答案在于两件事的结合:


  1. shebang ,第一行一份文件。

  2. 该文件的权限,即可执行文件标志是否设置。

考虑第一行 django-manage.py 。在我的系统上,它位于 /usr/local/bin/django-admin.py ,第一行是:

 #!/ usr / bin / python2.7 

和文件的权限:

  -rwxr-xr-x 1 root root 127 Jan 9 13:38 / usr / local /bin/django-admin.py 

shebang告诉我的操作系统,我想执行文件口译员位于 /usr/bin/python2.7 。权限中的 x 字符表示它是一个可执行文件。我不需要事先指定 python ,因为操作系统可以从上述信息中自动识别出来。






现在,通过运行 django-创建一个新创建的 manage.py admin.py startproject mysite ,看起来像这样:

 #!/ usr / bin / env python 

权限:

  -rw-r  -  r-- 1 wim wim 249 Feb 17 17:33 manage.py 

shebang表示使用我的 env 指示的任何python解释器,所以这个部分已经到位了。但是文件缺少可执行权限,所以只需运行 ./ manage.py 将无法正常工作。



我可以通过使用 chmod + x manage.py django-manage.py 相同$ C>。之后,当您在目录中执行 ls -l <​​/ code>时,应该会看到 x 标志,你应该能够运行文件,而不必事先指定 python



注意: 仍然需要使用 ./ manage.py 不仅仅是 manage.py ,这个差异是因为 django-admin.py 住在code> / usr / local / bin / 包含在 PATH ,而 manage.py 文件可能不会。所以我们明确地指定shell的路径,其中指的是当前目录。


I have a very simple Python question, with examples using Django. When running a Python script, do I always have to precede the script filename with the python command?

In the Django tutorial I am following, some commands are as follows:

django-admin.py startproject mysite

However, other are like this:

python manage.py runserver

Why does the top one not need the python command?

Alternatively, if my system knows that all Python scripts are to be executed by my python interpreter, why does the bottom one need the python command at all?

解决方案

The answer lies in a combination of two things:

  1. The shebang, the first line of a file.
  2. The permissions on the file, namely whether the executable flag is set or not.

Consider the first line of django-manage.py. On my system it is at /usr/local/bin/django-admin.py and the first line is:

#!/usr/bin/python2.7

And the permissions on the file:

-rwxr-xr-x 1 root root 127 Jan  9 13:38 /usr/local/bin/django-admin.py

The shebang tells my OS that I want to execute the file with the interpreter at /usr/bin/python2.7. The x characters in the permissions say that it is an executable file. I don't need to specify python beforehand, because the OS can figure out that stuff automatically from the above pieces of information.


Now for a freshly created manage.py, which I made by running django-admin.py startproject mysite, it looks like this:

#!/usr/bin/env python

And the permissions:

-rw-r--r-- 1 wim wim 249 Feb 17 17:33 manage.py

The shebang indicates to use whatever python interpreter my env is pointing at, so that part is in place already. But the file lacks executable permissions, so just running ./manage.py won't work (yet).

I can make it behave the same as django-manage.py by explicitly setting the executable flag using chmod +x manage.py. After that, you should see the x flags set when you do ls -l in the directory, and you should be able to run the file without specifying python beforehand.

Note: you do still have to use ./manage.py not just manage.py, this discrepancy is because django-admin.py lives in /usr/local/bin/ which is contained in PATH whereas the manage.py file likely doesn't. So we explicitly specify the path at the shell, where . refers to the current directory.

这篇关于运行Python脚本的方式差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆