Uniextra
Uniextra

Reputation: 143

run python script from a .bat file and using Powershell

I run several python scripts on my W10 server and I'm looking for a way to open them all together.

I run manually my scripts opening a PowerShell on the script folder and executing pythonw script.py

I have tried several ways, with no result...

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe C:\Users\pc2\script.py'"

also tested:

powershell.exe -noexit -command "'pythonw C:\Users\pc2\script.py'"

And:

powershell -command "& {&'C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe ' C:\Users\pc2\script.py}"

None of the above does anything... they should be opening the script.py file in using pythonw.

Can anyone point me in the correct direction to get this done?

Upvotes: 2

Views: 4551

Answers (2)

mklement0
mklement0

Reputation: 440112

  • To execute a single script:

    powershell -c "pythonw 'C:\Users\pc2\script1.py'"
    

Note: The enclosing '...' around the file path are only needed if the latter contains shell metacharacters such as whitespace.
Alternatively, use ""..."" when calling from cmd.exe (batch file), and `"...`" when calling from PowerShell.

  • To execute multiple scripts, in sequence:

    powershell -c "& { $Args | % { pythonw $_ } } 'C:\Users\pc2\script1.py' 'C:\Users\pc2\script2.py'"
    

Note: This only works when calling from cmd.exe (batch file).
(From within PowerShell, simply execute what's inside "..." directly)


As for what you tried, focusing just on what command string PowerShell ended up seeing:

  • & 'C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe C:\Users\pc2\script.py'

The problem is that you're passing the entire command line to &, whereas it only expects the executable name;
C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe C:\Users\pc2\script.py is obviously not a valid executable name / path.

  • 'pythonw C:\Users\pc2\script.py'

By virtue of being enclosed in '...', this is a string literal, which PowerShell simply outputs as such - no command is ever executed.

  • & {&'C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe ' C:\Users\pc2\script.py}

Since you're using a script block ({ ... }), you're passing C:\Users\pc2\script.py as an argument, so you'd have to refer to it as such inside the block, either via the automatic $Args variable, or via a parameter defined in a param() block.
(As an aside: your file path has an extraneous trailing space, but PowerShell seems to ignore that.)

Upvotes: 1

Ranadip Dutta
Ranadip Dutta

Reputation: 9238

From the powershell console itself, you can directly run like this:

PS C:\Python27> python C:\Users\pc2\script.py

If necessary please edit the PATHTEXT environmental variable in the powershell profile:

$env:PATHEXT += ";.py"

In the batch also you can put the same command to execute the python. Save the file as something.bat :

python C:\Users\pc2\script.py

Hope it helps.

Upvotes: 0

Related Questions