allocer allocer - 10 days ago 7
Python Question

Python django modify help file

I would like to modify -h command in django project. I created a project but when I use my own help command

python manage.py my_help
I get an error about not installed modules. And this is ok, because I would like to inform user which modules are needed to install before run a project. So, when I call
python manage.py my_help
or
python manage.py -h
I want to show info about additional modules which are needed and general info about project. Now I got only error messsages like (I'm using reportlab in project)

from reportlab.lib.pagesizes import A4, letter
ImportError: No module named 'reportlab'


Is it possible to modify default -h option or do something with my own help command to show basic info instead of an error?

Answer

You need to modify your import statements like this:

try:
    from reportlab.lib.pagesizes import A4, letter
except ImportError:
    print 'Missing dependency reportlab'

But this is rather unusual. The usual practice is to create a requirments.txt file which can be passed to pip as follows

pip install -r requirements.txt

Which enables the user to easily satisfy all the requirements in one command. If you catch each ImportError and print out a message for each requirement, you are actually forcing the user to type pip install multiple times.