问题描述
有人可以告诉我如何在任何目录中调用我的脚本吗?
Can someone tell me how to make my script callable in any directory?
我的脚本仅返回目录中的文件数.我希望通过调用它来在任何目录中使用它,而不是先复制到那里然后键入python myscript.py
My script simply returns the number of files in a directory. I would like it to work in any directory by invoking it, instead of first being copied there and then typing python myscript.py
我正在使用Mac OS X,但是有常见的方法可以将其安装在Windows和Linux上吗?
I am using Mac OS X, but is there a common way to get it installed on Windows and Linux?
推荐答案
如果脚本以合适的shebang行开头,例如:
If your script starts with a suitable shebang line, such as:
#!/usr/bin/env python
并且您的脚本具有可执行位设置(对于Linux,OS X和其他类似Unix的系统):
And your script has the executable bit set (for Linux, OS X, and other Unix-like systems):
chmod +x myscript.py
脚本的路径在PATH环境变量中:
And the path to your script is in your PATH environment variable:
export PATH=${PATH}:`pwd` # on Unix-like systems
SET PATH=%PATH%;\path\to # on Windows
然后,无论您身在何处,都可以拨打myscript.py
.
Then you can call myscript.py
from wherever you are.
这篇关于使Python脚本可在整个系统范围内访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!