3

How do I 'install' a python script so I can run it when ever I want?

My script looks like:

#/usr/bin/env python
import os;

while True:
    comm = input();
    os.system(comm);

But when i try to run it I get:

/usr/bin/doors.py line2: command not found 
...

How to I fix this?

5

1 Answer 1

4

You have a typo in shebang line, which specifies interpreter to use. It should be:

#!/usr/bin/env python

which will default to python2.7. Since you've tagged your question python3, you may want to use:

#!/usr/bin/env python3

You should use the #!/usr/bin/env python3 line .

As it stands now, your shell is interpreting the script with shebang being treated as a comment as it starts with #. Thus, there's no interpreter specified, and by default script will be executed with your current shell. The shell has no idea of what import is , hence the command not found error is being shown by the shell.

As a side note, for running native shell commands in python, use the subprocess module instead of unsecured and deprecated os.system function.

Also why are you running user input blindly without any sanity checking?

9
  • That should be /usr/bin/env, not /use/bin/env
    – trvrm
    Apr 28, 2016 at 18:21
  • 1
    @trvrm Haha..a typo in my answer too..fixed.. Thanks
    – heemayl
    Apr 28, 2016 at 18:22
  • 1
    OP has tagged the question python3. You might wanna mention that as well, because #!/usr/bin/env python is 2.7 by default Apr 28, 2016 at 18:23
  • @Serg Edited...
    – heemayl
    Apr 28, 2016 at 18:30
  • @heemayl do you mind if I edit it a bit ? Apr 28, 2016 at 18:31

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