简体   繁体   English

如何不使用./script.py来运行python脚本?

[英]How to run python script without using ./script.py?

I have a script.py that I'd like to run when the user types "script" in unix. 我有一个当用户在Unix中键入“ script”时要运行的script.py。 How do I accomplish this, instead of forcing him to type ./script.py each time? 我该怎么做,而不是强迫他每次键入./script.py?

You need to put the directory of the script in your PATH environment variable. 您需要将脚本目录放入PATH环境变量中。

eg 例如

/path/to/script.py
export PATH=$PATH:/path/to

To obviate typing the extension create a local symlink 为了避免键入扩展名,请创建本地符号链接

ln -s /path/to/script.py /path/to/script

This prevents polluting /usr/local/bin with manual symlinks (which could be important on a multi-user) system. 这样可以防止使用手动符号链接(在多用户环境中可能很重要)污染/usr/local/bin

You will need to add a symlink to the script, without using the .py extension, and place it somewhere on your system path. 您将需要在不使用.py扩展名的情况下向脚本添加符号链接,并将其放置在系统路径中的某个位置。 For instance, you might do 例如,您可能会做

sudo ln -s myscript.py /usr/local/bin/myscript

Then you will need neither the .py nor the ./ . 然后,您将不需要.py./

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

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