Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a python script executable?

How can I run a python script with my own command line name like 'myscript' without having to do 'python myscript.py' in the terminal?

like image 356
ctrlz Avatar asked Dec 15 '14 23:12

ctrlz


People also ask

Can Python code be converted to exe?

Learn what an executable file is and why it may be useful while looking at how to convert a Python script to an executable using auto-py-to-exe. Python files have two main methods for execution: using the terminal or in a text editor/IDE of your choosing.


1 Answers

  1. Add a shebang line to the top of the script:

    #!/usr/bin/env python

  2. Mark the script as executable:

    chmod +x myscript.py

  3. Add the dir containing it to your PATH variable. (If you want it to stick, you'll have to do this in .bashrc or .bash_profile in your home dir.)

    export PATH=/path/to/script:$PATH

like image 179
tzaman Avatar answered Oct 11 '22 02:10

tzaman