Python Cmd Tab Completion Problems

2019-09-08 09:03发布

I've got an application I'm currently working on for our company. Its currently built around Python's Cmd module, and features tab-completion for a number of tasks.

For some reason however, the Tab completion only currently works on one machine in the building - running the scripts from other machines doesn't allow the tab completion.

Here's the offending code parts:

def populate_jobs_list():
    global avail_jobs
    avail_jobs = os.walk(rootDir()).next()[1]
    print avail_jobs

...

def complete_job(self, text, line, start_index, end_index):
global avail_jobs
populate_jobs_list()
if text:
    return [
        jobs for jobs in avail_jobs
        if jobs.startswith(text)
    ]
else:
    return avail_jobs

def do_job(self, args):
    pass
    split_args = args.rsplit()
    os.environ['JOB'] = args
    job_dir = os.path.join( rootDir(), os.getenv('JOB'))
    os.environ['JOB_PROPS'] = (job_dir + '\\job_format.opm')
    if not os.path.isdir(job_dir):
        print 'Job does not exist. Try again.'
        return
    else:
        print('Jobbed into: ' + os.getenv('JOB'))
        return

populate_jobs_list()
prompt = outPrompt()
prompt.prompt = '\> '
prompt.cmdloop('Loading...')

Am I missing something obvious here? Just to clarify, on machine A, the tab completion works as intended. When its run on any other machine in the building, it fails to complete.

标签: python cmd
1条回答
时光不老,我们不散
2楼-- · 2019-09-08 09:17

Check if the environment variable PYTHONSTARTUP is set properly. It should point to a script which in turn needs to do sth like this:

try:
    import readline
except ImportError:
    sys.stdout.write("No readline module found, no tab completion available.\n")
else:
    import rlcompleter
    readline.parse_and_bind('tab: complete')

Maybe (some part of) this is only done properly on the one working machine?

Maybe the readline module is available only on the one working machine?

查看更多
登录 后发表回答