Equan Ur Rehman
Equan Ur Rehman

Reputation: 269

Running python script without python installed on pc

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?

Upvotes: 11

Views: 79551

Answers (4)

Maxim Martsinkevich
Maxim Martsinkevich

Reputation: 21

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"
)

Upvotes: 0

Rands
Rands

Reputation: 11

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)) 

Upvotes: 0

Filip R&#225;koczy
Filip R&#225;koczy

Reputation: 1

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

Upvotes: 0

Talha &#199;elik
Talha &#199;elik

Reputation: 187

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

Upvotes: 8

Related Questions