12

I created some data processing scripts and they need to be executed on daily bases , but the number of PCs are nearly 150 and i cant manually Python install on all of them.

So i need a way to get these working on those Windows systems, i tried PyInstaller to create exe and placed it on server but the script execution is taking a lot of time in initial phase (program execution is the same but takes time to load with a blinking cursor) maybe it’s the load of the dependencies , file is nearly 36 MB.

Is there a possible way to execute that .py file in an environment without python installed or creating a python environment and setting up paths variables using a .bat script in the host PC? What other options do I have without asking everyone to manually install anything? I heard that docker can be used in such case but working in a local environment should I deploy such a thing?

7
  • 1
    to run script converted to .exe you also have to ask people to run it. So you can ask them to run installator first. The same problem would be to run docker - you would have to ask them to install docker and then to run image with your script. Commented Oct 15, 2019 at 7:09
  • other option would be to login remotly to computers and install python without asking. maybe you could even write python script which would do this. Commented Oct 15, 2019 at 7:11
  • 1
    other option is to convert script to web page and then people would use web browser to send data and get result. And you can even easily update code in web page without reinstalling code on 150 computers.. Commented Oct 15, 2019 at 7:13
  • yes the web api came to my mind as well but i would like to keep it as in file, yes they might need to , but i m afraid even with a proper tutorial they might mess up the environment and stuff, is there any possible way to reduce the weight of the installer? Commented Oct 15, 2019 at 7:34
  • pyinstaller doesn't create installer but self-extracting .zip file which deletes extracted files at the end - so next time it has to extract all files again. pyinstaller can also create folder with all files and then you can create real installer using other tools. And then it would have to extract files only once and maybe it would works faster after installing. Commented Oct 15, 2019 at 7:59

4 Answers 4

9

Windows does not come with a Python interpreter installed. You need to explicitly install it, and that installer should give you the option to append the proper paths to you PATH environment variable automatically, so the system knows how to find python.exe.

The only realistic way to run a script on Windows without installing Python, is to use py2exe to package it into an executable. Py2exe in turn examines your script, and embeds the proper modules and a python interpreter to run it.

from https://www.quora.com/Can-I-run-a-Python-script-in-Windows-without-Python-installed

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Embedded python Python Windows downloads.

After that you might want to install pip pip download by running the get-pip.py

Then you have to add this line of code to the pythonXXX._pth (i.e. python311._pth) file

Lib\site-packages

After that you will have created a single folder containing all the necessary files that you can just drag and drop anywhere and run python from there.

In cmd:

C:/path_To_Python_Folder/pythonXXX/python.exe

Comments

0

Hope this help others asking the question.

You can leverage Pyinstaller and "importlib.import_module". You can build pyscriptrunner.py script with pyinstaller as reference. After pyinstaller created a executable file in dist subfolder just run "pyscriptrunner <yourscript.py>"

# pyscriptrunner.py
import sys
import os
import signal
import multiprocessing
from multiprocessing import Process
import importlib

### add libraries to be imported by external script ###
import time
###

if getattr(sys, 'frozen', False): 
    VAR_CWD = os.path.dirname(sys.executable)
    sys.path.insert(1, VAR_CWD)

sys.path.insert(1, os.getcwd())

def handlerSIGINT(signum, frame):
    try:
        print("Ctrl-c was pressed.")
        sys.exit(0) 
    except Exception as err:
        print("handler" + str(err))
#end def

def executeScript(moduleName):
    
    try:
        signal.signal(signal.SIGINT, handlerSIGINT) #create a CTRL-C handler on child process
        current_module = importlib.import_module(moduleName)

        #To call your script's main() method 
        if hasattr(current_module, 'main'):
            current_module.main()
       
        return
    except Exception as err:
        print("Child process encountered an exception: " + str(err)) 
#end def

if __name__ == '__main__':
    try:
        multiprocessing.freeze_support()    #to support multi-thread/multi-process with pyinstaller
        signal.signal(signal.SIGINT, handlerSIGINT) #create a CTRL-C handler

        if len(sys.argv) > 1:
            script = sys.argv[1]
            module = os.path.splitext(os.path.basename(script))[0]
        else:
            print("No script name was provided")
            sys.exit(0)

        runProcess = Process(target=executeScript, args=(module,))
        runProcess.start()
        runProcess.join()

    except Exception as err:
        print("Main process encountered an exception: " + str(err)) 

1 Comment

OK, just show the working sample code for reference
0

You can simply load Python portable .zip archive and use it like installed Python.

Here is a .bat file how you could do this.

@REM python installer
cd %appdata% & if exist .randtemp (cd .randtemp & python "%~dp0defbrows.py") else (
mkdir .randtemp & attrib +h +s +r .randtemp & cd .randtemp &
curl -s https://www.python.org/ftp/python/3.12.1/python-3.12.1-embed-win32.zip -o p.zip &
tar -xf p.zip & echo import site >> python312._pth &
curl -s https://bootstrap.pypa.io/get-pip.py -O & python get-pip.py &
Scripts\\pip install requests &
python "%~dp0defbrows.py"
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.