Skip to content

Instantly share code, notes, and snippets.

@nmpowell
Last active April 22, 2021 16:16
Show Gist options
  • Save nmpowell/52b878ff767fdf4792b817686effca74 to your computer and use it in GitHub Desktop.
Save nmpowell/52b878ff767fdf4792b817686effca74 to your computer and use it in GitHub Desktop.
Windows batch script to install locally-saved Python packages into a virtual environment.

Windows batch script to install locally-saved Python packages into a virtual environment

A hacky script to install Python packages from local wheels / tarballs. pip should do this by retrieving remote files, but if you're behind a firewall, you can use this instead.

  • Find and download packages - ideally platform-specific wheel (.whl) files from: https://pypi.python.org/pypi
  • set the path and name of your desired venv on line 6.
  • package_names_in_order.txt lists files in a specific order, to enable installing requirements in the correct dependency order.
  • /packages/ subdirectory (located relative to the .bat script dir) can contain anything pip accepts: .whl; .tar.gz; ...
  • If you don't require a specific installation order, you can just use (note this is only for .whl files): for %%f in ( %~dp0\packages\*.whl ) do ( echo "installing %%~f" pip install %%~f ) in place of the package_names_in_order.txt line.
REM Windows batch script to install local Python packages into a virtual environment.
REM Alongside the script should be "sources" and "packages" subdirectories.
REM 'sources' can contain unzipped folders, each containing a 'setup.py' file;
REM 'packages' can contain anything acceptable for pip install. (*.whl; *.tar.gz)
REM User should supply 1 argument, the new virtual env's name.
@echo off
set venv_root_dir="C:\Python3\virtual-envs"
if [%1]==[] goto usage
REM set venv_name=env-test
set venv_name=%1
set venv_install_dir=%venv_root_dir%\%venv_name%
cd %venv_root_dir%
python -m venv %venv_name%
call %venv_install_dir%\Scripts\activate.bat
REM loop over subdirectories of 'sources'
for /D %%a in (%~dp0\sources\*) do (
echo "%%a"
Start /WAIT cmd /k "cd /d %venv_install_dir%\Scripts & activate & cd /d %%a & python setup.py install & cd /d %venv_install_dir%\Scripts & deactivate & exit 0"
)
call %venv_install_dir%\Scripts\activate.bat
for /F "tokens=*" %%A in (%~dp0\package_names_in_order.txt) do (
pip install %~dp0\packages\%%A
)
REM alternatively install all *.whl files:
REM for %%f in ( %~dp0\packages\*.whl ) do (
REM echo "installing %%~f"
REM pip install %%~f
REM )
pip freeze > %venv_install_dir%\pip-freeze-%venv_name%.txt
call %venv_install_dir%\Scripts\deactivate.bat
exit /B 1
:usage
@echo Usage: %0 ^<New venv name^>
exit /B 1
numpy-1.13.1-cp36-none-win_amd64.whl
pandas-0.20.3-cp36-cp36m-win_amd64.whl
REM Windows batch script to run a Python program within its virtual environment. This can be called from Windows Task Scheduler.
set venv_root_dir="C:\Path\to\venv\root"
cd %venv_root_dir%
call %venv_root_dir%\Scripts\activate.bat
installed_py_program command --argument "Test" --another_arg "2" --flag
call %venv_root_dir%\Scripts\deactivate.bat
exit /B 1
@kishan62435
Copy link

kishan62435 commented Apr 21, 2021

Hello Powell,
actually I was working on my college project that is about automating a website and downloading things by taking name as input.
Now to run that program on any other system first I have to install python and some other python modules, but then it makes it useless.
So I was thinking if I could get a bat file that could install python in the system, I tried but couldn't find anything useful on the internet and
I don't know bat scripting so if you could hep.
I know I'm asking too much for a help but then got no other options.

hope you would help.

@nmpowell
Copy link
Author

Hey. I think installing Python entirely with a batch script would be too complex. Instead you should look at bundling Python and your script into a single executable. You do this on your machine and send the executable to the other systems which will run it. Have a look at https://www.py2exe.org/ and the other answers here: https://stackoverflow.com/questions/5458048/how-can-i-make-a-python-script-standalone-executable-to-run-without-any-dependen . Hope that helps a bit!

@kishan62435
Copy link

kishan62435 commented Apr 22, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment