I created python script ("hello.py" for example). But I would like to open script from any directory using:
darker0n@MacBook ~>hello
Create a custom command that'll invoke your Python script.
Let's say your hello.py
is in the path /home/python/hello.py
Create a custom script called hello
containing:
python /home/python/hello.py
.
You can put it a hidden directory so that it remains hidden.
Say you've added it to the following file: /home/python/.custom/hello
. Now add the following line to your .bashrc
(or equivalent) file:
export PATH=$PATH":/home/python/.custom
Next time you open a terminal and type hello
, you get the script to run. To get it immediately in any already open terminal sessions, simply run source ~/.bashrc
hello
instead of hello.py
and has an appropriate shebang (#!
) line then the intermediate shell script is unnecessary.CommentedApr 23, 2015 at 18:21Use a shell interpreter line, (shebang line). For example, in a file called hello.
#!/usr/bin/env python python code here ...
then
chmod +x hello
and put the hello script somewhere in your PATH.
Oh and don't type
~>hello
that is a syntax error, and the > will redirect output to a file hello, overwrite it with nothing, as the "~" is not really a command, but a special shell $HOME directory spec. Unless the ~> is part of your prompt, then ignore this warning.
You can create a symbolic link in your /usr/local/bin
folder (or any folder in your PATH env variable) with the command:
ln -s /path/to/hello.py /usr/local/bin/hello
This will allow to keep your script in your preferred path and having the possibility to modify it anytime.
/usr/bin
is almost never the right solution to a problem.CommentedApr 23, 2015 at 18:20
python /path/to/hello.py