Windows and python execution

M

Mark Carter

What I would like to do it type something like
> myscript.py instead of
> python myscript.py
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)?
 
B

BartlebyScrivener

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
 
R

rzed

What I would like to do it type something like
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)?

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.
 
M

Mark Carter

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

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.
 
B

BartlebyScrivener

What you need to do is include the following line in autoexec.bat:
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.
 
R

rbt

Mark said:
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.

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

BartlebyScrivener

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
 
M

Mark Carter

BartlebyScrivener said:
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.

I think you're misunderstanding 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.
 
B

BartlebyScrivener

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.py"
and it executes. Okay. You're welcome.

rpd
 
B

Bengt Richter

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.
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=%PATHEXT%;.py
Does your set do the pathext and assoc and ftype all in one swell foop?

Regards,
Bengt Richter
 
M

Mark Carter

Bengt said:
I wasn't aware of that syntax for set. What OS/platform/shell is that from?

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

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.
and decide
not to set pathext, e.g., by something like
set PATHEXT=%PATHEXT%;.py
Does your set do the pathext and assoc and ftype all in one swell foop?

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:\python24;%PATH%
set .py=c:\python24\python.exe
 
R

rzed

Windows XP, bog-standard default shell. UNIXers have it easy
because they can use the normal "shebang".


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.


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:\python24;%PATH%
set .py=c:\python24\python.exe

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>myNeatProgram

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.bat, myNeatProgram.com, myNeatProgram.exe, etc.,
until it hits upon myNeatProgram.py and proceeds to run it. So if
you have a myNeatProgram.bat that appears earlier in your path than
myNeatProgram.py does, the bat file is what gets run.
 
M

Mark Carter

rzed said:
I should have asked which Windows version you had.

XP Pro

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>myNeatProgram

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

Tim Roberts

Mark Carter said:
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.

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.NoConFile
ftype Python.NoConFile="c:\Apps\Python24\pythonw.exe" "%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
 
P

Peter Hansen

BartlebyScrivener said:
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.

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
 
P

Peter Hansen

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

D'oh... okay, people (including me) are reading others' posts with
preconceptions about what they are talking about in mind.

The PATHEXT thing is required to be able to type just "scriptname"
_without_ the .py extension. Alternatively, it appears there's yet
another obscurely documented feature involving setting environment
variables that resemble file extensions, as posted by others. (Where do
these things come from? It's like Microsoft releases the OS, then
periodically sends private emails to random people, pointing out obscure
new features, so that they can tell others in some feeble effort to make
using Windows look like a grassroots effort or something. How are
regular mortals supposed to find out about things like "set .py="?)

The ability to run the script with just "scriptname.py" comes from, I
believe, having a file association set up with "ftype" and "assoc" or
the equivalent registry entries. For this to work from _any_ location
one must have the folder containing the *script* in the PATH, as with
any executable, while the path specified by FTYPE points to the Python
executable. (This ftype/assoc file association is set up by the
standard installer, which is why it works for BartlebyScrivener).

-Peter
 
D

Daniel Dittmar

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

This is a feature of 4NT, a commercial replacement for cmd.exe. And
unlike cmd.exe, it even allows redirection of stdin/stdout when a script
is executed through the extension (the OP's next post is probably going
to be 'why doesn't redirection work?').

Daniel
 
B

BartlebyScrivener

Some of the confusion probably comes from which installation people
use. I used the latest ActiveState distribution of Python. As one of
the other posters observed it automatically adds the .py extension in
the PATHEXT system variable. At least I didn't put it there. Then, as
we've discussed, as long as the directory containing your scripts is in
the path, you can type nothing by "myscript.py" at the command line and
your script will execute from any location on the machine.

rpd
 
R

Roger Upole

The PATHEXT allows you to execute the script
without typing the .py extension.

Roger
 
T

Thorsten Kampe

* Mark Carter (2005-12-26 23:06 +0100)
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:\python24;%PATH%
set .py=c:\python24\python.exe

If you installed Python the normal way, .py files are already
associated with Python. Windows uses the registry and doesn't make any
difference whether you doubleclick a file or type the name on the
command line
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,373
Members
48,065
Latest member
SusannaSpe

Latest Threads

Top