在没有 python 命令的终端中运行 python 脚本 [英] run a python script in terminal without the python command

查看:21
本文介绍了在没有 python 命令的终端中运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,我们将它命名为 script1.py.我可以这样在终端中运行它:

I have a python script let's name it script1.py. I can run it in the terminal this way:

python /path/script1.py
...

但我想像命令行程序一样运行:

but I want to run like a command-line program:

arbitraryname
...

我该怎么做?

推荐答案

您使用 shebang line 在脚本的开头:

You use a shebang line at the start of your script:

#!/usr/bin/env python

使文件可执行:

chmod +x arbitraryname

并将其放在 PATH 上的目录中(可以是符号链接):

and put it in a directory on your PATH (can be a symlink):

cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname

这篇关于在没有 python 命令的终端中运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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