如何从 bash 直接执行 python 脚本(没有 python 命令的前缀)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6024350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:01:15  来源:igfitidea点击:

How can I execute a python script directly (without prefixing by the python command) from bash?

bashshell

提问by Javaaaa

I am just starting to use terminal for my programming needs. In a lot of Django tutorials I see people say, for example, I should type this in terminal:

我刚刚开始使用终端来满足我的编程需求。在很多 Django 教程中,我看到有人说,例如,我应该在终端中输入:

manage.py runserver

However when I do this it says:

但是,当我这样做时,它说:

bash: manage.py: command not found

I get it to work when I do: python manage.py runserver, however I would like to understand why this works and the other method doesn't. I guess these are some very basic things but I thought I'd ask here.

当我这样做时我让它工作:python manage.py runserver,但是我想了解为什么这有效而另一种方法无效。我想这些是一些非常基本的东西,但我想我会在这里问。

回答by anubhava

It is because your manage.pyis not an executable script.

这是因为您manage.py的不是可执行脚本。

First put this line at the top of manage.py(assuming your python is in /usr/bin/python):

首先将此行放在manage.py(假设您的python在/usr/bin/python)的顶部:

#!/usr/bin/python

Then make your script executable:

然后使您的脚本可执行:

chmod +x manage.py

Then try to execute your script ./manage.py runserver.

然后尝试执行您的脚本./manage.py runserver

Read this link for more info: http://effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm

阅读此链接了解更多信息:http: //effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm

回答by sarnold

bash(1)will search your PATHenvironment variable to find programs to execute. PATHdoes not normally contain your "current working directory" (.) because that opens people up to trivial security problems:

bash(1)将搜索您的PATH环境变量以查找要执行的程序。PATH通常不包含您的“当前工作目录”(.),因为这会让人们面临一些微不足道的安全问题:

cd /home/unsavory_character/
ls

If unsavory_characterplaces an executable in /home/unsavory_character/lsthat adds his or her ssh(1)key to your ~/.ssh/authorized_keysfile, you'd be in for a surprise -- he or she could log in as you without a password.

如果unsavory_character/home/unsavory_character/ls其中放置一个可执行文件,将他或她的ssh(1)密钥添加到您的~/.ssh/authorized_keys文件中,您会感到惊讶——他或她可以在没有密码的情况下以您的身份登录。

So systems these days don't add the current working directory to the PATH, because it is too unsafe.

所以现在的系统不会将当前工作目录添加到PATH,因为它太不安全了。

The workaround:

解决方法:

./manage.py runserver

Of course, that assumes your current working directory is whichever directory contains the manage.pyscript. That might be a safe assumption. If you'd like to be able to execute it from anywhere in the filesystem, you can add the directory to your PATHby editing your ~/.profileor ~/.bash_profileor ~/.bashrcfile. (If one of them already exists, pick that one. I seem to recall others with PATHproblems on OS X found one or the the other file worked well, and the other one never got executed.)

当然,这假设您当前的工作目录是包含manage.py脚本的目录。这可能是一个安全的假设。如果您希望能够从任何地方文件系统执行它,你可以将目录添加到您的PATH通过编辑~/.profile~/.bash_profile~/.bashrc文件。(如果其中一个已经存在,请选择那个。我似乎记得PATH在 OS X 上有问题的其他人找到了一个或另一个文件运行良好,而另一个从未被执行。)

(In my case, I have a bunch of self-written utilities in ~/bin/, but yours might be elsewhere. Change the paths as appropriate.)

(就我而言,我在 中有一堆自己编写的实用程序~/bin/,但您的可能在其他地方。根据需要更改路径。)

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

回答by Ben

manage.py needs to be executable. Try: chmod +x manage.py

manage.py 需要是可执行的。尝试:chmod +x manage.py

回答by MindTooth

I've cooked together a small "script" to automate this: (just copy whole text, and paste inside your active terminal.)

我已经编写了一个小“脚本”来自动执行此操作:(只需复制整个文本,然后粘贴到您的活动终端中即可。)

tee -a ~/.profile <<EOF

if [ -d "/Library/Python/2.6/site-packages/django/bin" ] ; then
    PATH=/Library/Python/2.6/site-packages/django/bin:$PATH
fi
EOF

Doesn't django-admin.pydo the same? I think so, because I can find manage.pyinside my ../binfolder. And stated at the official documentation, they do the same. So I believe ;)

django-admin.py一样吗?我想是的,因为我可以manage.py在我的../bin文件夹中找到。并在官方文档中说明,他们也这样做。所以我相信;)

Also, have you obtained Django via easy_install? My script expect that you are using Snow Leopard with the system version (Python 2.6).

另外,您是否通过 Django 获得了 Django easy_install?我的脚本希望您使用系统版本(Python 2.6)的 Snow Leopard。