The Python ecosystem is vast and powerful. Among the many incredible third-party libraries is howdoi
. This package allows developers to type in questions directly from the command line and have them answered. It requires an internet connection to work but can greatly improve workflow efficiency.
This package is available in the Python pip catalog and can be installed as easily as it can be executed. It works by querying the Stackoverflow.com website, looking for the top answers to relevant questions, then returning those answers via terminal printout to developers. It has quite the list of dependencies and has a few quirks of which one should be aware, but overall is a useful tool.
Quick Intro: Install, Setup, & Basic Use
The howdoi
package (aptly named) can be installed much like other Python packages:
pip install howdoi
You should see the terminal output the typical updates of downloading and installing dependencies — there are a lot. Below is the list of dependencies, taken directly from the requirements.txt
of howdoi
:
Pygments>=2.3.1 argparse==1.4.0 cssselect==1.1.0 lxml>=4.2.5 pyquery==1.4.1 requests==2.24.0 cachelib==0.1.1 appdirs==1.4.4 keep==2.9 rich==10.13.0 colorama==0.4.4
These libraries, based on the current system state, may require additional downloads. For example, the total install log of a recent howdoi
reads as follows:
Installing collected packages: pycparser, wrapt, urllib3, idna, charset-normalizer, cffi, certifi, requests, pynacl, pyjwt, deprecated, colorama, terminaltables, PyGithub, lxml, cssselect, click, pyquery, Pygments, keep, cachelib, appdirs, howdoi
That’s 23 dependencies total — not exactly lightweight. It is important to note that, while howdoi
is a Python package, questions are not limited to Python concerns. The howdoi
package can field any question — though those limited to the domain of programming and computer science-related concerns are ideal. For example, the query howdoi find inner peace
returns the following output:
def search(sentences, keyword1, keyword2, window=3): res = [] for sentence in sentences: words = sentence.lower().split(" ") if keyword1 in words and keyword2 in words: keyword1_idx = words.index(keyword1) keyword2_idx = words.index(keyword2) if keyword2_idx - keyword1_idx <= window: res.append(sentence) return res
Very interesting but not very enlightening. This package also provides some advanced customization features such as altering which StackExchange subdomain is queried, defining custom functions, and even aliasing custom functions. Read the documentation for a full overview.
Basic Usage
The howdoi
package provides users with command-line access to Stackoverflow answers (by default) and is used as such:
$ howdoi [QUERY [QUERY ...]]
This command-line notation simply means that one uses the command howdoi
and passes some optional arguments. If no arguments are passed (e.g. $ howdoi
) it is the same as executing the howdoi -help
command. Let’s consider the output from a simple query:
$ howdoi print something in python import time x = input() time.sleep(6) print('Hello World!') $
Immediately after the query howdoi print something in python
, we see output of an example of printing something in Python! It lacks an explanation but does address the question. Another helpful feature of howdoi
is to provide developers with the URL of the chosen answer. Let’s try our question again with a request for the URL:
$ howdoi -l print something in python https://stackoverflow.com/questions/53679932/how-do-i-print-something-after-n-seconds
Here we see only the link to the answer which, upon manual inspection, reveals the source of the initial answer:
This workflow would, essentially, save one from opening their browser, typing in stackoverflow.com
, entering the search phrase print something in python
into the search box, and clicking on the first answer. That’s actually quite an improvement in workflow provided one is happy with the first resulting post. On that note, let’s demo some usage of howdoi
that exemplifies some more advanced usage for those of us that might be pickier.
Advanced Use
The howdoi
package provides several useful features that developers can enable, config, and disable via passing command line arguments. Three of the most notable are as follows:
- viewing the full text of an answer
- returning multiple answers
- save an answer to the local cache
There are certainly more features available but these provide a much wider range of use than the default howdoi [QUERY, [QUERY...]]
functionality. Let’s see a few in action!
View Full Text
Passing in the --all
flag will return the full text of the answer (i.e. portions other than code blocks) as well as the URL from which the answer was obtained. Example code:
$ howdoi --all print something in python ★ Answer from https://stackoverflow.com/questions/53679932/how-do-i-print-something-after-n-seconds ★ use import time import time x = input() time.sleep(6) print('Hello World!')
Return Multiple Answers
Passing in the --num
flag with a proceeding integer argument as --num NUM
will return NUM
many answers (max). For this one, we’ll have to adjust our question since there is only 1 answer to the howdoi print something in python
query.
$ howdoi --num 3 install a package in python ★ Answer from https://stackoverflow.com/questions/34808902/installing-package-in-python ★ C:\Python27\Scripts ================================================================================ ★ Answer from https://stackoverflow.com/questions/65863547/how-to-build-and-install-a-python-package-locally-without-uploading-to-pypi ★ pip install path_containing_fdroid_build_checker/froid_buildchecker ================================================================================ ★ Answer from https://stackoverflow.com/questions/39473266/how-to-install-module-and-package-in-python ★ pip
At first glance, it appears the --all
command option has persisted. However, this is just a question-specific nuance as re-running the above with the --all
flag generates a much more detailed response (only first question shown here for brevity):
$ howdoi --all --num 3 install a package in python ★ Answer from https://stackoverflow.com/questions/34808902/installing-package-in-python ★ Add C:\Python27\Scripts to your PATH in system settings, and you will be able to run: pip3 thunder to install packages for Python 3.x and pip2 thunder or pip2.7 thunder to install packages for Python 2.7. Without modifying PATH, for Python 2.7 you can run the command directly: C:\Python27\Scripts\pip thunder. After modifying the PATH pip command will be called from the directory found first, so if you add Python2.7 directory to the end you'll get pip to run for Python3, if you add Python2.7 to the start, you'll get pip to run for Python2.7. ... (last two items omitted)
Save an Answer to Local Cache
Find yourself asking the same question over and over again? Save time and lower the risk of Google flagging your queries by using the local caching feature of howdoi
! The following code will ask a question and save the result in the local cache:
$ howdoi push an existing project to a github repo --save Detected fresh installation. Initializing environment in ~/.keep directory ...OK STASH LIST: ################################################################################ $ push an existing project to a github repo git init git add . git commit -m "Initial commit" git remote add origin <project url> git push -f origin master
One can view the contents of the local “stash” at any time via the following command:
$ howdoi --view STASH LIST: ################################################################################ $ push an existing project to a github repo git init git add . git commit -m "Initial commit" git remote add origin <project url> git push -f origin master
Running the exact same query will result in a notably faster turn-around time since howdoi
will retrieve the example from the local cache vs. making a query. The “stash” can be cleared either globally via the command howdoi --empty
or discretely via howdoi --remove QUERY
.
Final Thoughts
The howdoi
package is a useful command-line assistant to help save developers some clicks. It has robust essential features and also provides a useful set of advanced features such as those seen above. In addition, howdoi
allows for searching of other stackexchange
properties (e.g. serverfault.stackexchange.com
), changing search engine, disabling SSL requirements, and disabling the cache feature entirely.