Make your Python script executable from anywhere in Linux

Utpal Kumar   3 minute read      

Python makes life so easy to write scripts for our desired outputs. Sometimes, we simply write a Python utility program that we want access from anywhere in the whole system. I will go through the series of steps you can follow to make your Python script executable in Linux (or Unix-like) system.

Key idea — three things must all be true. To type myscript.py from any directory and have it just run, the script needs (1) a shebang line telling the shell which interpreter to use, (2) the executable bit set (chmod +x), and (3) to live in a directory that’s on your PATH. Miss any one and it won’t work: no shebang → “cannot execute binary”; no +x → “permission denied”; not on PATH → “command not found”. Get all three, and your script behaves like any other command.

Three requirements to run a Python script from anywhere A script runs as a command from any directory only when all three are true: it has a shebang line, it has the executable bit set, and it lives in a directory on your PATH. ① Shebang line #!/usr/bin/env python3 ② Executable bit chmod +x myscript.py ③ On your PATH copy into ~/bin Run from anywhere just type: myscript.py all three must be true
The three conditions that together let you invoke a script by name from any directory.

Define the shebang as the python path

We use shebang (or #!) a lot in computing. When a text file with a shebang is used as if it is an executable in a Linux, the program loader mechanism parses the rest of the file’s initial line as an interpreter directive.

We can make use of this concept. I added the path to the anaconda python (for the specific environment) as the first line of my Python script. The python script will look like below:

#!/home/utpal/miniconda3/envs/stadenv/bin/python
import os, glob
### rest of the script

In addition to this, you will have to make the script executable in linux:

chmod +x myscript.py

Prefer the portable shebang #!/usr/bin/env python3. Hard-coding a full interpreter path like #!/home/utpal/miniconda3/envs/stadenv/bin/python pins the script to one machine and one user — move it elsewhere and it breaks. #!/usr/bin/env python3 instead asks env to find the first python3 on your PATH, so it uses whatever interpreter is active (including your conda/venv environment). Use the hard-coded path only when you deliberately want a script to always run under one specific environment regardless of what’s activated.

Create environment in the home directory

In your terminal, type:

cd ~/ #navigate to the home directory
mkdir bin

Now, open the ~/.bashrc file in your favourite editor.

code ~/.bashrc #to open the bashrc file using vscode

Now, add your bin directory to the system PATH.

export PATH="$HOME/bin:$PATH"

On zsh, edit ~/.zshrc instead. Modern macOS and many Linux setups default to zsh, not bash — check with echo $SHELL. If it ends in zsh, put that same export PATH="$HOME/bin:$PATH" line in ~/.zshrc. Either way, the change takes effect in new terminals; to apply it in the current one without restarting, run source ~/.bashrc (or source ~/.zshrc). Many distributions also add ~/bin to PATH automatically if it exists at login — so you may only need to create the directory and re-login.

Bring the python script in the system path

Finally, all we have to do is to copy the script to the system path (the bin directory).

cp myscript.py ~/bin/

Now, restart your terminal and you should have access to the Python script from any location on your system.

Quick check: Your script has a shebang and is copied to ~/bin (which is on your PATH), but running myscript.py gives “permission denied”. What’s missing?

  • The shebang points to the wrong Python
  • The executable bit — run chmod +x ~/bin/myscript.py
  • ~/bin isn’t really on your PATH
  • You need to reinstall Python

Recap

  • Shebang picks the interpreter. The first line #!... tells the shell what runs the file; prefer the portable #!/usr/bin/env python3 over a hard-coded path.
  • chmod +x makes it runnable. Without the executable bit you get “permission denied”.
  • PATH makes it findable. Drop the script in a directory on your PATH (e.g. ~/bin, added via ~/.bashrc or ~/.zshrc) so you can call it by name from anywhere.
  • Tidy tip: rename the file to drop .py (e.g. mv ~/bin/myscript.py ~/bin/myscript) if you’d rather type myscript than myscript.py.

Where to go next

Disclaimer of liability

The information provided by the Earth Inversion is made available for educational purposes only.

Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.

UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.


Leave a comment