I am writing an application with a server client type of framework. To implement the logic,

  1. I need to transfer a zipped folder to a Windows server,
  2. unzip the folder and
  3. then run a certain python file within the folder.

I am trying to recreate the steps manually, so that I can then redo it using python libraries (such as paramiko and zipfile or other python libraries)

So far I have tried the following tests to check whether I can reliably run a python script using ssh:

  1. Using OpenSSH from Windows 10: https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH After some configuration, I was able to copy files from the client side to the server using scp. The next plan was to use ssh to remotely run a specific file using python filename.py

    Then cd'ed into the folder and tried running python filename.py. However, there was no output from python.

  2. Next I tried MobaSSH, and the problem was the same as the test with OpenSSH from Microsoft. `Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp28591

    Current thread 0x00000874 (most recent call first):`

  3. From the above 2 experiments, I initially thought there was a problem with my python installation. So I directly logged into the machine, and use cmd to run the same file as , python filename.py, it does run and produce the expected outputs. So python does run fine on the Windows machine

  4. My next hypothesis was that its impossible to run python if run through SSH. To test this I logged into my ubuntu VM, and did the same experiment. In this case, python ran fine, with no errors.

From these experiments, it seems that if I log into Windows using ssh, and try to run python, I cant run it maybe because of a quirk of python or of Windows. But if I am directly logged into Windows, I can run the same python script fine. Further, it seems ssh does work flawlessly in a linux environement.

I do need to run the python script in a Windows environment, not a linux environment because of some other considerations. So even though running the test file worked fine in the ubuntu VM, I cant use that environment. .

Is there a way to run a Python script when logged in via SSH in a Windows environment?

  • 1
    What is 'filename.py' exactly?
    – bjoster
    Mar 13, 2018 at 14:13

1 Answer 1


I had some success with the following:

1.After logging in start cmd.exe


2.Set a codepage, for example 850 (from within cmd.exe)

chcp 850

3.Set the PATH and other environment variables (from within cmd.exe)

set PATH=c:\Python27;c:\Python27\Scripts;C:\Program Files (x86)\...
set OTHER_VAR=...

You can put the chcp and set commands into a batch file that you run after logging in.

To find out the desired values you could start a lokal cmd shell on the target machine or on another windows machine that is similar enough and run:


To write all variables directly to a file run on the target machine from a local cmd shell:

set >> myvars.cmd

This has to be edited manually to prepend each line with a ´set ´ to create a script you can then run after logging in remotely.

It is even possible to login, start ssh and execute the script in one line or in a bash script:

ssh -lwindowsuser 'cmd /K myvars.cmd'

The /K switch of cmd will run the following command (without exit after running it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.