我怎么能直接执行python脚本(不通过python命令prefixing)的庆典? [英] How can I execute a python script directly (without prefixing by the python command) from bash?

查看:124
本文介绍了我怎么能直接执行python脚本(不通过python命令prefixing)的庆典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

manage.py runserver

不过,我这样做时,它说:

However when I do this it says:

bash: manage.py: command not found

我得到它时,我做的工作:蟒蛇manage.py的runserver ,但是我想明白为什么这个工程,而另一个方法不。我想这些都是一些很基本的东西,但我想我会在这里问。

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.

推荐答案

的bash(1)将搜索你的 PATH 环境变量来查找程序来执行。 PATH 通常不包含你的当前工作目录( ),因为打开人到琐碎的安全问题:

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

cd /home/unsavory_character/
ls

如果 unsavory_character 放置一个可执行文件 /家庭/ unsavory_character / LS ,增加了他或她的 SSH(1)键你的的〜/ .ssh / authorized_keys中文件,你会大吃一惊 - 他或她可以为您登录无需输入密码。

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

所以系统这几天不要在当前工作目录添加到 PATH ,因为它太不安全了。

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

解决方法:

./manage.py runserver

当然,假定当前工作目录是哪个目录包含 manage.py 脚本。这可能是一个安全的假设。如果您希望能够从任何地方文件系统中执行它,您可以将目录添加到您的 PATH 通过编辑〜/。简介〜/ .bash_profile中〜/ .bashrc中文件。 (如果其中一个已存在,挑那一个。我似乎记得别人与 PATH 在OS X上发现问题的一个或另一个文件效果很好,而另一从来没有得到执行。)

Of course, that assumes your current working directory is whichever directory contains the manage.py script. 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 PATH by editing your ~/.profile or ~/.bash_profile or ~/.bashrc file. (If one of them already exists, pick that one. I seem to recall others with PATH problems on OS X found one or the the other file worked well, and the other one never got executed.)

(在我的情况,我在〜/斌/ 一堆自己编写的实用程序,但你可以在别的地方。改变路径,适当的。)

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

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

这篇关于我怎么能直接执行python脚本(不通过python命令prefixing)的庆典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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