1

I need to make a py program run on command just by name I can do that in by put the executable in /usr/bin/executable_program

yes. I know I can make it by:

chmod +x file.py
./file.py

I just want when I write the program name "executable_program" in terminal it runs without the "./" and the ".py"

Thank you...

4 Answers 4

4

I just want when I write the program name "executable_program" in terminal it runs without the "./" and the ".py"

You need to do the following things:

  1. Add the shebang at the top of your file, #!/usr/bin/python

  2. Make the file executable chmod +x foo.py

  3. Move it to somewhere that is in your $PATH, for example /usr/local/bin

To get rid of the .py, simply rename the file: sudo cp foo.py /usr/local/bin/foo

burhan@sandbox:~/pytemp$ cat foo.py
#!/usr/bin/python
print('Hello World!')
burhan@sandbox:~/pytemp$ chmod +x foo.py
burhan@sandbox:~/pytemp$ sudo cp foo.py /usr/local/bin/foo
burhan@sandbox:~/pytemp$ foo
Hello World!
Sign up to request clarification or add additional context in comments.

2 Comments

can't I crypt the script so no one can read it?! That's also one of the best advantages of the executables :)
That's an entirely different question.
1

You can create a directory in your home, for example:

$ mkdir ~/bin

Then add that directory to PATH variable in your .bash_profile with your favorite editor:

PATH=$PATH:$HOME/bin
export PATH

and then save the changes.

Now, open a new terminal and put your file in ~/bin. After that, you can run your file without using ./ before it.

Comments

0

Try adding your program to your path.

If you just export PATH=$PATH:. at the command line it will only last for the length of the session though.

If you want to change it permanently add export PATH=$PATH:. to your ~/.bashrc file (just at the end is fine)

https://unix.stackexchange.com/questions/3809/how-can-i-make-a-program-executable-from-everywhere

Comments

0

I give you a example, write "test" file like this, not end with ".py". The first comment line is your python interpreter path:

#!/usr/bin/python2.6
print 'helloworld'

Then chmod 711 test;

Then export your test file path to $PATH(system path);

PATH=$PATH:$HOME/bin
export PATH

Finally you can run test like this :

test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.