简体   繁体   English

在没有“./”的命令行中运行 python 程序

[英]running a python program in the command line without "./"

Let's say I have a program called "hello".假设我有一个名为“hello”的程序。 It is a python script and I want to run it at the linux command line.这是一个 python 脚本,我想在 linux 命令行中运行它。

I've been able to remove the.py extension as well as the interpreter.我已经能够删除 .py 扩展名以及解释器。

Now when I run it it is [user..path] $./hello现在当我运行它时它是 [user..path] $./hello

it works, but how do I remove the "./".它有效,但我如何删除“./”。 I've already applied the chmod a+x argument to the file and a shebang line at the front of my script.我已经对文件应用了 chmod a+x 参数,并在我的脚本前面应用了 shebang 行。

Basically, I am asking how to run the script as $ hello基本上,我在问如何将脚本运行为 $ hello

Add .添加. to the end of your $PATH variable.$PATH变量的末尾。

PATH=$PATH:.

Then it will search the current directory when looking for programs to run.然后它会在查找要运行的程序时搜索当前目录。

Note that you should not generally put .请注意,您通常应该将. at the beginning of $PATH .$PATH的开头。 If you cd into someone else's directory, and they have a program that's the same name as a system program (eg ls ), you'll execute their version instead of the real one.如果您cd进入其他人的目录,并且他们有一个与系统程序同名的程序(例如ls ),您将执行他们的版本而不是真正的版本。

You could also create your own directory where you install programs, and put that directory in $PATH .您还可以创建自己的安装程序的目录,并将该目录放在$PATH中。

PATH=~/bin:$PATH

Now you copy hello to your ~/bin directory and run hello .现在将hello复制到~/bin目录并运行hello

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

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