0

I am doing coursework and one of the important requirements is to create a .bat file for scripts. The bat file should run the script. I took this step until one thought struck me.

Code .bat file:

@echo off
py -u "Task_1.py"
echo %ERRORLEVEL%
pause

In my code, I use many third-party libraries, including for creating an interface. Let's assume that this script will run on a computer without python. Is it possible to somehow write a .bat file that would check for the presence of python and third-party libraries, and in their absence would download them? Has anyone done something similar, could you suggest how this can be implemented?

martineau
  • 119,623
  • 25
  • 170
  • 301
kostya ivanov
  • 613
  • 5
  • 15
  • Are you able to create a powershell script, which the batch file also executes - you could have it install python – Bashton Dec 22 '21 at 15:12

4 Answers4

0

You could have the .bat file run a python environment you make and pip install dependencies, would be worth looking into.

B.Quinn
  • 77
  • 1
  • 7
0

This answer says that curl comes with Windows version 10 or newer. Therefore I would just curl the installer:

curl https://www.python.org/ftp/python/3.10.1/python-3.10.1-amd64.exe

now run it with /quit as by this answer:

.\python-3.10.1-amd64.exe /quiet

Then you can install pip as suggested as an alternative method from the official pip website:

py -m pip install --upgrade pip

Now you can use pip install to get whatever libraries you might need:

pip install numpy

Note: I have not been able to test this on my Windows so use with caution

styrix358
  • 33
  • 1
  • 7
0

I found a post to check if python is installed or not (How do I test if Python is installed on Windows (10), and run an exe to install it if its not installed?). It explains it quite well. For the modules I would suggest using pip and installing the required modules by a requirements file.

ata
  • 1,254
  • 1
  • 9
  • 30
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '21 at 16:41
0

In your .bat script, you can first run py --version and check the return code of the command to see if Windows could find py.

If it couldn't:

  • Download a Python installer. I don't know which utility is supposed to do that on Windows, but it should look similar to this (wget on Linux, saves the installer as python-installer.exe):
    wget -O python-installer.exe https://www.python.org/ftp/python/3.10.1/python-3.10.1-amd64.exe
    
  • Then launch it and let the user go through the installation:
    ./python-installer.exe
    

Then, execute pip install --user your libs to install the libaries if needed.

Then, run your Python script.

Expurple
  • 877
  • 6
  • 13