Umut Ural
Umut Ural

Reputation: 59

How to install pip and python modules with a single batch file?

I really don't understand how batch files work. But I made a python script for my father to use in his work. And I thought installing pip and necessary modules with a single batch file would make it a lot easier for him. So how can I do it?

The modules I'm using in script are: xlrd, xlsxwriter and tkinter.

Upvotes: 4

Views: 27907

Answers (4)

To make sure your Windows is running pip, download get-pip.py and place it in the same folder as the .bat file. You can create a .bat file using a text editor and then changing the extension to .bat

Try this:

@echo off


:start
cls

set python_ver=36

python ./get-pip.py

cd \
cd \python%python_ver%\Scripts\
pip install xlrd
pip install XlsxWriter

pause
exit

tkinter in already included in Python since 3.1

Source

Upvotes: 8

ArcticFox
ArcticFox

Reputation: 60

You can create a requirements.txt file then use pip install -r requirements.txt to download all modules, if you are working on a virtual environment and you only have the modules your project uses, you can use pip3 freeze >> requirements.txt This is not a batch file but it will work just fine and it is pretty easy

Upvotes: 4

Reza Rahemtola
Reza Rahemtola

Reputation: 1182

To install pip, you can download get-pip.py and run it like that in your batch file (batch file and get-pip.py should be in the same folder) :

python get-pip.py

Then to install modules, you can create a file requirements.txt (in the same folder) with this content :

xlrd
xlsxwriter
tkinter

and use this command in your batch file :

pip install -r requirements.txt

The problem is that now you have 3 files, get-pip.py, requirements.txt and the batch file. If you want to make it easier, you can hide the py & txt file in the properties like that (for Windows) :

enter image description here

Or you can get rid of requirements.txt by replacing

pip install -r requirements.txt

with

pip install xlrd
pip install xlsxwriter
pip install tkinter

in your batch file.

Upvotes: 2

Edison
Edison

Reputation: 1020

I think the route you are taking is hard and may create a lot of difficulty for the non-technical person. My advice would be to to consider these alternatives:

  1. Create an executable that bundles all requirements up, using something like PyInstaller
  2. This may not be very applicable given that you are using tkinter. For non desktop apps, you could make your script a backend and Create a web app using a micro-webframework like Flask. Host it online on heroku or PythonAnywhere (Both platforms have free plans) and give them the link.

I used to create scripts as a freelancer and my life became easier when I learned to distribute them using those 2 methods.

Upvotes: 4

Related Questions