Project

General

Profile

Pydoc for Python 3x Programs

Pydoc is actually a script that ships with Python. Unfortunately as of 2022 and Ubuntu 20.04, it is hard coded to use Python 2.7 and thus fails when attempting to work with 3.x programs.

The fix is to create you own command so that it supersedes the provided one. This will allow it to work seamlessly for all usage, whether 2.7 or 3.x and will work with Python virtual environments

Your replacement pydoc must be in a folder on your path that precedes the standard /usr/bin operating system folder. Copy the shared pydoc to this location. On Ubuntu 20.04 this would be

cp cp /usr/bin/pydoc ~/.local/bin/pydoc

Then edit the first line. Currently this is

#!/usr/bin/python2.7

Replace that with

#!/usr/bin/env python

By using the env command, the shell will automatically determine the current version of Python in use and run the pydoc module with that.

Now the script must be made executable

chmod +x ~/.local/bin/pydoc

Now when you run pydoc it will use your copy not the shared one and will run the appropriate version of Python that you are using.