3

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?

2 Answers 2

12

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%;\path\to # on Windows

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

Sign up to request clarification or add additional context in comments.

4 Comments

using export PATH=${PATH}:pwd, will allow all scripts in the working directory to be accessed if they have chmod +x. How do I specify just one single file?
Yes it will. You can either put it in its own directory or in one where you keep other such scripts, e.g. ${HOME}/bin.
Put it someplace like /usr/local/bin. You can even put a symlink there if you really want to keep your actual script someplace else
@Falmarri's suggestion means that other users should be able to run your script, if that is your intention.
0

All of those operating systems should support a PATH environment variable which specifies directories that have executables that should be available everywhere. Make your script executable by chmod +x and place it into one of those directories (or add a new one to your PATH - I have ~/bin for instance).

I don't know how to make new kinds of files directly executable on Windows, though, but I guess you could use a .bat file as a proxy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.