Running python script without python installed on pc
Asked Answered
K

3

11

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?

Kaunas answered 15/10, 2019 at 7:0 Comment(7)
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.Leda
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.Leda
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..Leda
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?Kaunas
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.Leda
Docker won't help you here: you'll need to install an interactive desktop application on all 150 systems, and just installing Python will be a little easier.Mada
As per now only solution seems like creating REST API for the data and host it locally , as per right now creating a simple interface for my Flask APIKaunas
P
7

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

Polish answered 16/5, 2020 at 22:41 Comment(0)
M
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
Mackmackay answered 24/3, 2023 at 16:28 Comment(0)
D
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)) 
Daystar answered 4/7 at 3:8 Comment(1)
OK, just show the working sample code for referenceDaystar

© 2022 - 2024 — McMap. All rights reserved.