0

I saved a python file in my desktop and want to run it in the terminal, but everytime I run it I have to type ' python /home/m3gn/Desktop/ex1.py' Is there anyway to shorten this so that I only have to type 'python ex.1'?

Thank you!

1
  • You can cd into /home/m3gn/Desktop first and then run python ex1 Aug 17, 2013 at 23:04

2 Answers 2

1
  1. You can create a bin folder inside your home directory: /home/m3gn/bin Ubuntu in theory can add this directory to your path automatically.
  2. Give the python file executable permissions, that is: chmod +x ex1.py
  3. Move the ex1.py file to the home/m3gan/bin directory
  4. You should be able to run the file by using only the filename: ex1.py
  5. As a plus, since Ubuntu adds bin to your path, you will have bash completion, that is if you type e + TAB or ex + TAB or ex1 + TAB, the terminal will auto complete the rest of the filename.

If Ubuntu does not automatically add bin to your path, you can manually add it at your .bashrc file by adding the following lines:

if [ -d ~/bin ] ; then
    PATH=$PATH:~/bin
fi

Save and close .bashrc and issue: source .bashrc in terminal, it should be ok and ready for use.

2
  • Good answer, however as well as marking it as executable with 'chmod +x', isn't it also necessary to add a 'shebang' at the top of the file telling the system use the python interpreter? e.g. '#!/usr/bin/env python' Aug 17, 2013 at 23:38
  • That is correct and may or may not be the case since the OP does not provide the source which may or may not include a shebang, good observation.
    – Stef K
    Aug 17, 2013 at 23:46
0

Add variable

ex1="/home/m3gn/Desktop/ex1.py"

to your ~/.bashrc file and run

source ~/.bashrc

Now you can just use

python $ex1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .