0

I created python script ("hello.py" for example). But I would like to open script from any directory using:

darker0n@MacBook ~>hello
2
  • What do you mean? Use: python /path/to/hello.py Commented Apr 23, 2015 at 18:03
  • I don't want use this path. I want easy: hello Commented Apr 23, 2015 at 18:10

3 Answers 3

1

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

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

1 Comment

If the python script is named hello instead of hello.py and has an appropriate shebang (#!) line then the intermediate shell script is unnecessary.
1

Use 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.

Comments

0

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.

2 Comments

Messing with system directories like /usr/bin is almost never the right solution to a problem.
Yes ok /usr/bin is maybe not appropriate but any folder in the PATH can be used. Changed to /usr/local/bin in the answer.

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.