We have a python script in our source code repository which I maintain. Let's imagine it is at location
scripts/python/make_salad/make_salad.py
It is checked into the repository as is. Users of the script want to just click on the script in windows explorer and off they go. They refuse to use the command line. However the script also depends on many external packages that I have to install. I've used the following trick to install any packages required the first time the user runs the script. It goes like
def install(package):
# This is an evil little function
# that installs packages via pip.
# This means the script can install
# it's own dependencies.
try:
__import__(package)
except:
import subprocess
subprocess.call([sys.executable, "-m", "pip", "install", package])
install("colorama")
install("pathlib")
install("iterfzf")
install("prompt_toolkit")
install("munch")
install("appdirs")
install("art")
install("fire")
import os
import tkFileDialog
import getpass
import json
import shutil
import subprocess
import sys
import pprint
import art
# <snip> out all my business logic
print("Making Salad")
However I don't like this because it installs the packages to the global package repository. What I'd like is if all the packages were installed something like
scripts/python/make_salad/make_salad.py
/__packages__
/colorama
/pathlib
/iterfzf
...
/fire
and then this directory would be first on the search path when import is called. Is it possible to hack the above script so the above is possible?
Note the requirements
- Only the single script is stored in repository
- Users have to click on the script in windows explorer
- Require to pip install external packages from within the script
- Packages should not pollute the global packages