Windows and python execution

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

    #1

    Windows and python execution

    What I would like to do it type something like[color=blue]
    > myscript.py[/color]
    instead of[color=blue]
    > python myscript.py[/color]
    on a Windows console. I know its possible because Ruby scripts manage to
    do this - I just don't know the registry settings that need to be
    tweaked to enable it. Any ideas (I'd prefer to know the registry
    settings rather than a graphical way to accomplish the goal)?
  • BartlebyScrivener

    #2
    Re: Windows and python execution

    Well, can't help with the registry, but I suppose you could search
    google for "edit system variables path registry" and find the hard way.
    The easy way is:

    You need to add the path of the directory where you keep myscript.py to
    your system path. You do this by going to Control Panel | System |
    Advanced | Environmental Variables. At the bottom under System
    Variables, edit the variable PATH and add the directory where you want
    to keep your Python scripts.

    rpd

    Comment

    • rzed

      #3
      Re: Windows and python execution

      Mark Carter <me@privacy.net > wrote in
      news:43afdec0$0 $15788$14726298 @news.sunsite.d k:
      [color=blue]
      > What I would like to do it type something like[color=green]
      > > myscript.py[/color]
      > instead of[color=green]
      > > python myscript.py[/color]
      > on a Windows console. I know its possible because Ruby scripts
      > manage to do this - I just don't know the registry settings that
      > need to be tweaked to enable it. Any ideas (I'd prefer to know
      > the registry settings rather than a graphical way to accomplish
      > the goal)?[/color]

      As another poster points out, be sure that your Python is on your
      path.

      And there is a PATHEXT environment variable, which contains
      extensions that signal that the program is executable. Add .PY and
      ..PYW to this list and you will be good to go.

      --
      rzed

      Comment

      • Mark Carter

        #4
        Re: Windows and python execution

        rzed wrote:[color=blue]
        > Mark Carter <me@privacy.net > wrote in
        > news:43afdec0$0 $15788$14726298 @news.sunsite.d k:
        >
        >[color=green]
        >>What I would like to do it type something like
        >>[color=darkred]
        >>>myscript.p y[/color]
        >>
        >>instead of
        >>[color=darkred]
        >>>python myscript.py[/color][/color][/color]
        [color=blue]
        > As another poster points out, be sure that your Python is on your
        > path.
        >
        > And there is a PATHEXT environment variable,[/color]

        Aha. You'bve provided a significant clue.

        What you need to do is include the following line in autoexec.bat:
        set .py=c:\python24 \python.exe

        This will achieve the desired result. I'm suprised more people don't use it.

        Comment

        • BartlebyScrivener

          #5
          Re: Windows and python execution

          >> What you need to do is include the following line in autoexec.bat:[color=blue][color=green]
          >> set .py=c:\python24 \python.exe[/color][/color]

          Whatever works for you. I don't have that command in my autoexec.bat
          file and my python scripts execute from any location because the
          directory they are stored in is in my PATH variable.

          Comment

          • rbt

            #6
            Re: Windows and python execution

            Mark Carter wrote:[color=blue]
            > rzed wrote:[color=green]
            >> Mark Carter <me@privacy.net > wrote in
            >> news:43afdec0$0 $15788$14726298 @news.sunsite.d k:
            >>[color=darkred]
            >>> What I would like to do it type something like
            >>>
            >>>> myscript.py
            >>>
            >>> instead of
            >>>
            >>>> python myscript.py[/color][/color]
            >[color=green]
            >> As another poster points out, be sure that your Python is on your path.
            >> And there is a PATHEXT environment variable,[/color]
            >
            > Aha. You'bve provided a significant clue.
            >
            > What you need to do is include the following line in autoexec.bat:
            > set .py=c:\python24 \python.exe
            >
            > This will achieve the desired result. I'm suprised more people don't use
            > it.[/color]

            I'm surprised the installer doesn't do it :)

            Comment

            • BartlebyScrivener

              #7
              Re: Windows and python execution

              The installer can put python.exe in his PATH but if he's storing his
              scripts in some oddball place then he has to tell Windows where to find
              them.

              rpd

              Comment

              • Mark Carter

                #8
                Re: Windows and python execution

                BartlebyScriven er wrote:[color=blue]
                > The installer can put python.exe in his PATH but if he's storing his
                > scripts in some oddball place then he has to tell Windows where to find
                > them.[/color]

                I think you're misunderstandin g what I was after. python.exe is already
                in my PATH. What I was trying to do was execute a python script without
                having to type the word "python" explicitly. For that, you need the
                "set" command that I listed in a previous post. Anyway, I want to be
                able to put my scripts in "oddball" places, as they form part of
                separate projects rather than a generally-runnable script. It doesn't
                matter to me that Windows cannot find the script itself - because I just
                go to the directory where's it's at.

                The upshot is that my original problem has now been solved, and that
                discussing the matter further will only likely confuse the issue.

                Comment

                • BartlebyScrivener

                  #9
                  Re: Windows and python execution

                  >>What I was trying to do was execute a python script without
                  having to type the word "python" explicitly. For that, you need the
                  "set" command that I listed in a previous post.<<

                  I'm not confused and I like to keep my scripts in oddball places too.
                  Namely d:\python. All I'm saying is I don't use the set command. I
                  don't have an autoexec.bat file. I simply place d:\python in the path
                  variable and I can execute python scripts from the command line from
                  any directory without typing "python" first. I just type "myscript.p y"
                  and it executes. Okay. You're welcome.

                  rpd

                  Comment

                  • Bengt Richter

                    #10
                    Re: Windows and python execution

                    On Mon, 26 Dec 2005 14:53:28 +0000, Mark Carter <me@privacy.net > wrote:
                    [color=blue]
                    >rzed wrote:[color=green]
                    >> Mark Carter <me@privacy.net > wrote in
                    >> news:43afdec0$0 $15788$14726298 @news.sunsite.d k:
                    >>
                    >>[color=darkred]
                    >>>What I would like to do it type something like
                    >>>
                    >>>>myscript. py
                    >>>
                    >>>instead of
                    >>>
                    >>>>python myscript.py[/color][/color]
                    >[color=green]
                    >> As another poster points out, be sure that your Python is on your
                    >> path.
                    >>
                    >> And there is a PATHEXT environment variable,[/color]
                    >
                    >Aha. You'bve provided a significant clue.
                    >
                    >What you need to do is include the following line in autoexec.bat:
                    >set .py=c:\python24 \python.exe
                    >
                    >This will achieve the desired result. I'm suprised more people don't use it.[/color]
                    I wasn't aware of that syntax for set. What OS/platform/shell is that from?
                    How did you go from the PATHEXT "clue" to the set command you specify and decide
                    not to set pathext, e.g., by something like
                    set PATHEXT=%PATHEX T%;.py
                    Does your set do the pathext and assoc and ftype all in one swell foop?

                    Regards,
                    Bengt Richter

                    Comment

                    • Mark Carter

                      #11
                      Re: Windows and python execution

                      Bengt Richter wrote:
                      [color=blue][color=green][color=darkred]
                      >>>And there is a PATHEXT environment variable,[/color]
                      >>
                      >>Aha. You'bve provided a significant clue.
                      >>
                      >>What you need to do is include the following line in autoexec.bat:
                      >>set .py=c:\python24 \python.exe
                      >>
                      >>This will achieve the desired result. I'm suprised more people don't use it.[/color]
                      >
                      > I wasn't aware of that syntax for set. What OS/platform/shell is that from?[/color]

                      Windows XP, bog-standard default shell. UNIXers have it easy because
                      they can use the normal "shebang".
                      [color=blue]
                      > How did you go from the PATHEXT "clue" to the set command you specify[/color]

                      I can't remember. It was a bit of luck, I think. I happened upon:
                      Take Command displays console apps in tabbed windows, with Explorer integration. Includes TCC, the CMD command prompt replacement and an IDE / batch debugger.

                      probably as a result of Googling for PATHEXT.
                      [color=blue]
                      > and decide
                      > not to set pathext, e.g., by something like
                      > set PATHEXT=%PATHEX T%;.py
                      > Does your set do the pathext and assoc and ftype all in one swell foop?[/color]

                      Actually, I haven't figured out what PATHEXT is actually supposed to
                      "do". It seemed to me that Windows couldn't possibly know that a py file
                      should be started by python.exe, whereas my
                      set .py= ...
                      would.

                      I had installed python 2.4 in the standard way, so py files were already
                      associated with python when you double-clicked them from Explorer. Using
                      my set meant that if I wanted to use py files from the command line, I
                      could just type out the script name (you have to be in the right
                      directory, of course), and it works. Here's a snippit from my
                      autoexec.bat files:
                      set PATH=C:\python2 4;%PATH%
                      set .py=c:\python24 \python.exe

                      Comment

                      • rzed

                        #12
                        Re: Windows and python execution

                        Mark Carter <me@privacy.net > wrote in
                        news:43b0697b$0 $15789$14726298 @news.sunsite.d k:
                        [color=blue]
                        > Bengt Richter wrote:
                        >[color=green][color=darkred]
                        >>>>And there is a PATHEXT environment variable,
                        >>>
                        >>>Aha. You'bve provided a significant clue.
                        >>>
                        >>>What you need to do is include the following line in
                        >>>autoexec.bat : set .py=c:\python24 \python.exe
                        >>>
                        >>>This will achieve the desired result. I'm suprised more people
                        >>>don't use it.[/color]
                        >>
                        >> I wasn't aware of that syntax for set. What OS/platform/shell
                        >> is that from?[/color]
                        >
                        > Windows XP, bog-standard default shell. UNIXers have it easy
                        > because they can use the normal "shebang".
                        >[color=green]
                        >> How did you go from the PATHEXT "clue" to the set command you
                        >> specify[/color]
                        >
                        > I can't remember. It was a bit of luck, I think. I happened
                        > upon: http://www.jpsoft.com/help/index.htm?exeext.htm
                        > probably as a result of Googling for PATHEXT.
                        >[color=green]
                        >> and decide
                        >> not to set pathext, e.g., by something like
                        >> set PATHEXT=%PATHEX T%;.py
                        >> Does your set do the pathext and assoc and ftype all in one
                        >> swell foop?[/color]
                        >
                        > Actually, I haven't figured out what PATHEXT is actually
                        > supposed to "do". It seemed to me that Windows couldn't possibly
                        > know that a py file should be started by python.exe, whereas my
                        > set .py= ...
                        > would.
                        >
                        > I had installed python 2.4 in the standard way, so py files were
                        > already associated with python when you double-clicked them from
                        > Explorer. Using my set meant that if I wanted to use py files
                        > from the command line, I could just type out the script name
                        > (you have to be in the right directory, of course), and it
                        > works. Here's a snippit from my autoexec.bat files:
                        > set PATH=C:\python2 4;%PATH%
                        > set .py=c:\python24 \python.exe
                        >[/color]

                        I should have asked which Windows version you had. My bad. On Win2k
                        or XP, adding .py (for instance) to the PATHEXT variable means that
                        you can execute "myNeatProgram. py" with this command-line:
                        prompt>myNeatPr ogram

                        Since .py appears in the PATHEXT variable, the system knows that
                        ..py files are executable, so it's not necessary even to specify
                        them. What must happen, I suppose, is that the system looks for
                        myNeatProgram.b at, myNeatProgram.c om, myNeatProgram.e xe, etc.,
                        until it hits upon myNeatProgram.p y and proceeds to run it. So if
                        you have a myNeatProgram.b at that appears earlier in your path than
                        myNeatProgram.p y does, the bat file is what gets run.

                        --
                        rzed

                        Comment

                        • Mark Carter

                          #13
                          Re: Windows and python execution

                          rzed wrote:
                          [color=blue]
                          > I should have asked which Windows version you had.[/color]

                          XP Pro

                          My bad. On Win2k[color=blue]
                          > or XP, adding .py (for instance) to the PATHEXT variable means that
                          > you can execute "myNeatProgram. py" with this command-line:
                          > prompt>myNeatPr ogram[/color]

                          Oh, now I see! It's beginning to make sense to me. I'll have to try that
                          out. Thanks for the tip.

                          Comment

                          • Tim Roberts

                            #14
                            Re: Windows and python execution

                            Mark Carter <me@privacy.net > wrote:
                            [color=blue]
                            >rzed wrote:[color=green]
                            >> Mark Carter <me@privacy.net > wrote in
                            >> news:43afdec0$0 $15788$14726298 @news.sunsite.d k:
                            >>
                            >>[color=darkred]
                            >>>What I would like to do it type something like
                            >>>
                            >>>>myscript. py
                            >>>
                            >>>instead of
                            >>>
                            >>>>python myscript.py[/color][/color]
                            >[color=green]
                            >> As another poster points out, be sure that your Python is on your
                            >> path.
                            >>
                            >> And there is a PATHEXT environment variable,[/color]
                            >
                            >Aha. You'bve provided a significant clue.
                            >
                            >What you need to do is include the following line in autoexec.bat:
                            >set .py=c:\python24 \python.exe
                            >
                            >This will achieve the desired result. I'm suprised more people don't use it.[/color]

                            They don't use it, because it doesn't do anything. I'd be interested to
                            know where you read that.

                            What the OP needs is this:

                            assoc .py=Python.File
                            ftype Python.File="c: \Apps\Python24\ python.exe" "%1" "%*"
                            assoc .pyw=Python.NoC onFile
                            ftype Python.NoConFil e="c:\Apps\Pyth on24\pythonw.ex e" "%1" "%*"

                            Substitute your own Python path.

                            The Python .msi installer DOES do this, if you let it do its default thing.
                            You should check whether these are already set up, by doing this:

                            assoc .py
                            --
                            - Tim Roberts, timr@probo.com
                            Providenza & Boekelheide, Inc.

                            Comment

                            • Peter Hansen

                              #15
                              Re: Windows and python execution

                              BartlebyScriven er wrote:[color=blue][color=green][color=darkred]
                              >>>What you need to do is include the following line in autoexec.bat:
                              >>>set .py=c:\python24 \python.exe[/color][/color]
                              >
                              >
                              > Whatever works for you. I don't have that command in my autoexec.bat
                              > file and my python scripts execute from any location because the
                              > directory they are stored in is in my PATH variable.[/color]

                              But only because you type "python scriptname.py" instead of just
                              "scriptname.py" , right? Or because something or someone (I don't
                              believe the standard distribution does this) changed the PATHEXT
                              environment variable to contain ".PY" somewhere.

                              Merely adding the folder containing the EXE to PATH does *not* let you
                              avoid typing "python" before the script name, as your posts imply.

                              -Peter

                              Comment

                              Working...