execute script everywhere

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • myang

    execute script everywhere


    After we compile a C program, we can put the executable file under a
    directory (like /bin) so that we can run it everywhere, and don't have to
    type the full path.
    Can we do the same thing to a python script?
    For example, I have a script.py under "/home/john/". Can I just type "python
    script.py" to run it under another directory "/home/tom/"?
    Thanks!

    Yang


  • Erik Max Francis

    #2
    Re: execute script everywhere

    myang wrote:
    [color=blue]
    > After we compile a C program, we can put the executable file under a
    > directory (like /bin) so that we can run it everywhere, and don't have
    > to
    > type the full path.
    > Can we do the same thing to a python script?
    > For example, I have a script.py under "/home/john/". Can I just type
    > "python
    > script.py" to run it under another directory "/home/tom/"?[/color]

    Make sure it has a proper bangpath, make it executable, and then put it
    somewhere in your PATH. Then you can run it no matter what your pwd is
    with

    script.py

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Courage is the fear of being thought a coward.
    -- Horace Smith

    Comment

    • Paul Watson

      #3
      Re: execute script everywhere

      "myang" <myang@clarku.e du> wrote in message
      news:mailman.12 .1079231052.745 .python-list@python.org ...[color=blue]
      >
      > After we compile a C program, we can put the executable file under a
      > directory (like /bin) so that we can run it everywhere, and don't have to
      > type the full path.
      > Can we do the same thing to a python script?
      > For example, I have a script.py under "/home/john/". Can I just type[/color]
      "python[color=blue]
      > script.py" to run it under another directory "/home/tom/"?
      > Thanks!
      >
      > Yang[/color]

      It appears that you are on a UNIX system. Most systems will support
      "shebang" proceesing of the first line in the file. The first line tells
      UNIX to find 'python' in the PATH and run it to exectute this script. Use
      'man env' for more information.

      #! /usr/bin/env python
      import os
      import sys
      ....

      When you do this, you do not need to uses the word 'python' on the command.
      At the '$' prompt, just use:

      script.py

      The first line tells it to use Python to process the file. Of course
      /home/john where the script.py file exists must be in the PATH variable in
      order to run it from anywhere. Just like you have /bin in your PATH
      variable already.


      Comment

      Working...