Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RGGH/d3aab5109e2c3f2befe5a48130371ddc to your computer and use it in GitHub Desktop.
Save RGGH/d3aab5109e2c3f2befe5a48130371ddc to your computer and use it in GitHub Desktop.
How to run python files without typing python and extension

How to run python files without typing python and extension (in Linux)

Steps to follow to run your python code without typing python filename.py.

You can simply run by typing filename if you follow these simple steps.

Step 1 : Add shebang line as first line in your python code

#!/usr/bin/python3

This line tells about the location of interpreter.

If you are using another version of python, find that interpreter and type it in here. (I am having two version of python, python 2.7 and python 3.5 and to run it with python3 interpreter we have to type in /usr/bin/python3)

If you are using virtualenv, you can simply modify the shebang line to your interpreter.

Step 2 : Make your python file executable

In terminal, go to your directory where your python file is located and type the below command.

chmod +x filename.py

For example we have hello.py as our file, so we will move to the directory where it is located and will do

chmod +x hello.py

Now you can run your python file like ./hello.py

Step 3 : Move your file to bin to run it from anywhere

In terminal type the following command

cp filename.py ~/bin/new_name_you want_to_run_with

For example I want to run this file by calling welcome, I will do

cp hello.py ~/bin/welcome

Now you can run this welcome from anywhere on your system by just typing welcome in terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment