tastyminerals
tastyminerals

Reputation: 6538

How to get Python location using Windows batch file?

I need to create a windows batch script which creates and moves one specific file to PYTHONPATH\Lib\distutils folder.

Here is what I am attempting to do:

ECHO [build] >> distutils.cfg
ECHO compiler=mingw32 >> distutils.cfg
MOVE distutils.cfg PYTHONPATH\Lib\distutils

However, PYTHONPATH does not exist but I know that Python location is set in PATH which I can check. How can I parse PATH and extract Python location from it?

Upvotes: 6

Views: 9690

Answers (4)

user1130984
user1130984

Reputation: 1

It works for me by adding "delims=" value to the /f option under the for command:

@ECHO OFF
for /f "delims=" %%p in ('where python') do SET PYTHONPATH=%%p
ECHO %PYTHONPATH%

The result:

C:\TEMP>pypathtest.bat
C:\Program Files (x86)\Python38-32\python.exe

Upvotes: 0

Henrik
Henrik

Reputation: 342

Why search for python.exe using WHERE when you know the answer? It's in the path env var! So all you need to do is scan the %path% var. It can be done like this:

echo %path% | split ; | find /i "python"

OK, here you need the split prog, but it's easily made (in C or batch) and you need it anyway.

Upvotes: 0

ashwinjv
ashwinjv

Reputation: 2967

If you have only one python instance, you can try this:

@ECHO OFF
FOR /f %%p in ('where python') do SET PYTHONPATH=%%p
ECHO %PYTHONPATH%

Upvotes: 8

vrs
vrs

Reputation: 1982

Since python is in your PATH you can use function where, which is a Windows analogue of Linux whereis function:

> where python.exe

See details here. You can set output of where command to a variable and then use it (see this post).

Upvotes: 13

Related Questions