When I type at the BASH terminal, I am accustomed to typing "python a_long_filename.py" but always using the "tab->" key to autocomplete the file name, which saves me time with typos. This works with python2, but not with python3.

is there some package I can install, or some setting I can change, to enable the autocomplete for python3?

  • What is the output of complete -p python3? – wjandrea Mar 20 '18 at 18:53
  • output bash: complete: python3: no completion specification but complete -p python outputs the same – con Mar 20 '18 at 18:55
  • Just to clarify one thing, Bash is doing the completion. Read help complete for some info. So really the issue is that Bash autocompletes file names for python2 but not python3. – wjandrea Mar 20 '18 at 18:57
  • this is Ubuntu 17.10, with GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) – con Mar 20 '18 at 19:00
  • After some testing, it seems like the completion settings don't get loaded until you try to use them. So try this in the terminal: type python, then a space, then press Tab, then erase it, and run complete -p python python2 python3 (python and python2 added for good measure). – wjandrea Mar 20 '18 at 19:10

As we discussed in the comments, you're trying to use Bash autocomplete on scripts with .py3 extensions. The problem is, Bash will only autocomplete scripts with .py extensions.

I recommend using .py extensions even for Python 3 scripts, since .py3 is non-standard. If the scripts require Python 3, you could...

  • Use the shebang: #!/usr/bin/env python3
  • Check if sys.version_info.major == 3
    • Or check if >= 3, to allow forwards compatibility
  • Maybe use from __future__ import ... to allow backwards compatibility
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.