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?
CodePudding user response:
You could have the .bat file run a python environment you make and pip install dependencies, would be worth looking into.
CodePudding user response:
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
CodePudding user response:
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 aspython-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.
CodePudding user response:
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.