4

I am working in Python 3.6+ and want to check if a few different modules are installed from within my script. If not, I want to attempt to install them with a few caveats:

1) the proper way to do this, if I remember reading it correctly, is to look into 'packaging and versioning' .. possibly with setuptools .. im not really sure. There is a Digital ocean page that is hard to follow. It discusses this but I keep running into an issue with documents around this topic: they are all based around the assumption that the project will be uploaded to pypip for use with pip. I specifically do not want this. I want to distribute this directly to individuals, by hand. Maybe in the future have it available in a closed, not-open-to-everyone github repo.

Currently in my script I'm using a try and except. Try to import these modules, if they don't exist i run this exception which i don't know if it works.

except ImportError:
from pip._internal import main as pip
pip(['install', colorama])
import colorama
print('colorama imported successfully')

and for what its worth - i have no idea what pip(['install', colorama]) is doing.

The packaging aspect seems to include imported modules with your code. How does one preform this function? Instead of checking if colorama is installed and then attempting to launch a subprocess to install it .. how do i just include the entire thing assuming this is the 'right' way to do this?

Oscalation
  • 370
  • 3
  • 15

1 Answers1

3

One thing that's usually done to avoid this problem is to build your program in a virtual environment which you know to contain the correct python scripts - and then either

  • package the entire virtual environment with your project as a unit, or
  • write a requirements.txt file that lists all the packages (and versions) that are expected to be installed before the user runs the program (you'd install everything on that list by doing pip install -r requirements.txt on the command line before running the program with python script_name.py)

Ideally, you'd then have your script fail if the required dependencies aren't there, and have the user install them manually to fix the issue.

Here's python 3's documentation on virtual environments

What you're doing now is unconventional - if it's working, it's working, but it's not great practice. Biggest reason for that is that your script, in its current state, is installing software on the user's machine without their consent, i.e. the user did not tell the program to install this software and was not told that the software was necessary, but the program is installing it anyway. In your case this may be harmless, but in general it's something to stay away from because it can get into really shady territory.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • your reasoning for the unconventionality was really helpful as i didn't consider that aspect. Thank you. Can you comment on .. what is this method/tool/process people seem to use where they have a setup.py script that lists all the required packages and running that seems to fetch and install all that stuff along with the ability to have versioning of your software? – Oscalation Nov 19 '18 at 23:48
  • [This Stackoverflow post](https://stackoverflow.com/questions/1471994/what-is-setup-py) does a good job at explaining how you use `setup.py` files. – Green Cloak Guy Nov 19 '18 at 23:57