0

I want to run my python function in console like this:

my_function_name

at any directory, I tried to follow arajek's answer in this question: run a python script in terminal without the python command

but I still need to call my_function_name.py to make it work. If I call only my_function_name, the console will inform me command not found . I also tried to add a symbolic link with this answer: Running a Python Script Like a Built-in Shell Command but it failed

sudo ln -s my_function_name.py /home/thovo/test/my_function_name

ln: failed to create symbolic link ‘/home/thovo/test/my_function_name/my_function_name.py’: File exists

Community
  • 1
  • 1
Raphaël VO
  • 2,413
  • 4
  • 19
  • 32

2 Answers2

4

Change the name of the script to no longer have the .py extension.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • I have never thought to remove the `.py` extension will make it work. It may be not quite right from my point of view only but it really worked. Thank you a lot! I will mark this answer as correct after few minutes. – Raphaël VO Feb 08 '15 at 19:28
2

Add this shebang to the top of your file: #!/usr/bin/env python and remove the file extension.

Monti
  • 643
  • 6
  • 18