Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can someone tell me how to make my script callable in any directory?

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

I am using Mac OS X, but is there a common way to get it installed on Windows and Linux?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
882 views
Welcome To Ask or Share your Answers For Others

1 Answer

If your script starts with a suitable shebang line, such as:

#!/usr/bin/env python

And your script has the executable bit set (for Linux, OS X, and other Unix-like systems):

chmod +x myscript.py

And the path to your script is in your PATH environment variable:

export PATH=${PATH}:`pwd` # on Unix-like systems

SET PATH=%PATH%;patho # on Windows

Then you can call myscript.py from wherever you are.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...