0

I want to be able to make a Python program and then execute it in the terminal no matter which directory I am currently located in. I mean, if I have an executable Python file and I'm in the same folder of the file I can directly execute it but if I'm in other directories I can't.

I want to be able to execute it from anywhere as I do with all the terminal commands. I think it has to do with the PATH but I'm pretty new to Linux, so I'd appreciate any help with this.

karel
  • 114,770
  • 1
    Put the file in /usr/local/bin/ That directory is intented for binaries that are local (ie. yours) – Rinzwind Apr 27 '15 at 11:32
  • ...and a bunch more :) – Jacob Vlijm Apr 27 '15 at 11:35
  • Can the file be put in /bin ? This is where I normally put my scripts – Rumesh Apr 27 '15 at 11:38
  • @RumeshSudhaharan if you mean ~/bin, yes, /bin is not an appropriate location for user scripts. – Jacob Vlijm Apr 27 '15 at 11:40
  • @JacobVlijm I put my scripts in /bin is there any particular reason that location is not advisable? I may have to move my scripts if it will affect my computer – Rumesh Apr 27 '15 at 11:45
  • @RumeshSudhaharan Look here: http://askubuntu.com/a/308048/72216 It has a great answer, saying: /bin : For binaries usable before the /usr partition is mounted. This is used for trivial binaries used in the very early boot stage or ones that you need to have available in booting single-user mode. Think of binaries like cat, ls, etc.. I prefer keeping my "own" scripts in ~/bin. – Jacob Vlijm Apr 27 '15 at 11:51
  • 1
    @JacobVlijm Thanks for the reply. I guess I'll move my files to /usr/local/bin now – Rumesh Apr 27 '15 at 11:57

1 Answers1

0

Add an alias to ~/.bashrc:

<<< "alias mypythonscript='python <path_to_python_script>'" tee -a ~/.bashrc

*<path_to_script> = path to your python script

Then update ~/.bashrc:

. ~/.bashrc

This way you'll be able to call your script from any bash instance by running mypythonscript

kos
  • 35,891