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?

1
  • Are you able to create a powershell script, which the batch file also executes - you could have it install python Commented Dec 22, 2021 at 15:12

4 Answers 4

0

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

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

Comments

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

1 Comment

If I'm not mistaken, you need to redirect curl output to a file or use -o option
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.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.