Home > other >  Embedding small amounts of python into batch scripts
Embedding small amounts of python into batch scripts

Time:06-02

I have an application that is primarily written in python. I wish to make and use a virtual environment with all the required packages. In Linux, this was quite easy. Here is my shell script to set up the virtual environment

#!/bin/bash
#It is best practice to create virtual environments for projects that require many packages installed
python3 -m venv ./python_environment  #Creates a new virtual environmnet in PWD
source ./python_environment/bin/activate  #swithces to our new virtual environment

#Now we can install the needed packages using pip
python3 -m pip install --upgrade pip
pip install numpy opencv-python pillow imutils

And here is my script to launch the application

#!/bin/bash
#This is a wrapper that launches program
source ./python_environment/bin/activate   # Uses custom python envionment in PWD
PYWRAP=$(cat <<EOF
import tagging
import tagging.tagger_controller
tagging.tagger_controller.start()
EOF
)#BASH Variable to pass to python

python3 -c "$PYWRAP"  #Executes the python wrapper

When I try to do this for Windows, the scripts are not as elegant. setup.bat runs but I noticed that some of the packages didn't install

python -m venv python_environment
python_environment\Scripts\activate.bat
python -m pip install --upgrade pip
pip install numpy opencv-python pillow imutils

launcher.bat only activates the environment, it doesn't start the app. You may also note that this script calls a .py file, this is less than ideal but I don't know how to embed a few lines of python into a batch file, like I did in Linux.

python_environment\Scripts\activate.bat
python main.py

Please let me know what you think...

CodePudding user response:

This isn't a complete answer, but I wanted to show you how to embed Python in a Windows batch file. Note this isn't original — I found it on the Internet somewhere nearly 20 years ago (2003).

@echo off
rem = """
rem Do any custom setup like setting environment variables etc if required here ...

python -x "%~f0" %*
goto endofPython """

# Your python code goes here ..

if __name__ == "__main__":
    print("Hello World from Python")

rem = """
:endofPython """

CodePudding user response:

My solution is a combination of both the answers above.

@echo off
rem = """
rem Do any custom setup like setting environment variables etc if required here ...

call python_environment\Scripts\activate.bat

call python -x "%~f0" %*
goto endofPython """

# Your python code goes here ..

import tagging
import tagging.tagger_controller
tagging.tagger_controller.start()

rem = """
:endofPython """

You'll notice that this is very similar to the solution posted by @martineau However a key difference is that the call command is used on lines 5 & 7 (credit to @aschipfl for that insight). This makes it so that the batch script can call the python code and not immediately close the app right after calling it.

Thank you for your help and best of luck!!!
KopiousKarp

  • Related