I am developing an API in Unix environment for virtual machines. Most of the modules are developed in python. I have few questions on this.

I have the file extension as abc.py. I would like to make this as a command. For example , virtman dominfo [vmid] should be the command syntax. Now I have to give ./virtman.py dominfo [vmid] to achieve this. And I have first line as #!/usr/bin/python in the python file. So how can make this as a command?

My echo $PATH looks like '/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin0'

I read a lot of articles but I didn't get the clear picture,so any hints/suggestions would be helpful.

  • @Anthon - I edited the question – Dany Nov 1 '14 at 8:08
  • Are you going to post the other part as a question as well? – Anthon Nov 1 '14 at 8:20
  • Yes I will add another question – Dany Nov 1 '14 at 8:22

You seem to be mistaken in that abc.py would not be a command. If you can execute it, then is, just one with a dot in the name. Execute in the sense that you can do ./abc.py, so the execute bits must be set. If you have to do python abc.py than you it is not a command (yet).

In general, to make a normal python file abc.py executable you should make sure the first line reads:

#!/usr/bin/env python

(This assumes you have /usr/bin/env as a program, and that will find the python command, which might be in /usr/local/bin. It also assumes that you want to run the default python (which is normally a link to a particular python version like python2.7), you could also use python3 if that is available as a command).

After that do

chmod +x abc.py
mv abc.py abc

And then you can run ./abc. If the current directory is in your path, or if you move abc to a directory in your path, you should be able to execute abc from anywhere.¹

There are however disadvantages of renaming and moving the file:

  • you can no longer to from abc import SomeClass, as the file is renamed
  • If the file is under revision control it might no longer be

So instead, what I normally do, is make a new file /usr/local/bin/abc that looks like:

#!/usr/bin/env python

from abc import main

and have at the bottom of abc.py:

def main():
    doing the real stuff

if __name__ == '__main__':

The directory of abc.py needs to be in the PATH python searches for modules, but this way it doesn't have to be changed, and can be used by any program as an import, and started as python abc.py.

¹ The mv is necessary to get rid of the dot in the command name, but not really necessary, you can invoke ./abc.py if you don't rename it.

  • This seems to be working, But I want to execute only 'abc' not './abc' . For example how 'uptime' works or any ohter commands in unix – Dany Nov 1 '14 at 8:36
  • @DineshAppavoo For that the command must be in one of the directories in your $PATH, can you add the output of echo $PATH at the end of your question? – Anthon Nov 1 '14 at 8:41
  • I added the echo $PATH output – Dany Nov 1 '14 at 8:54
  • @DineshAppavoo I put some more PATH related stuff in the answer to your other question. I hope that moves you forward a bit. – Anthon Nov 1 '14 at 10:48
  • In /usr/local/bin/abc how to you set the path to abc.py for the import? Do you just use sys.path.insert(0, '/path/to/git/repo/abc.py')? – dotancohen Nov 1 '14 at 13:24

Anthon's answer is correct, however for non-trivial Python projects you should have a look at the distutils module in the standard library. It's designed to deploy python modules and executables on a system the "right way", and :

  • Python devs are familiar with this module (or should be) because it's in the standard library and most well-known Python packages (Django, NumPy, Celery to name a few) are using it. Newcomers will know instantly how to deploy your project.

  • Once you've defined which files and packages you have in your project, just run python setup.py install to deploy and update your package on the system.

    No more manually updating the PATH variable copying files to /usr/local/bin or setting them to be executable, it's all handled for you!

  • You can use pip install -e to install your package in development mode.

  • It's portable (sort of) in Windows or OSX environments as well.

Also, I can't recomment the use of an argument parser such as argparse enough if you're designing command-line tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.