How do I add tab completion to the Python shell?

后端 未结 9 849
甜味超标
甜味超标 2020-11-30 16:41

When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc.

Python 2.5.1         


        
相关标签:
9条回答
  • 2020-11-30 17:12

    I think django does something like https://docs.python.org/library/rlcompleter.html

    If you want to have a really good interactive interpreter have a look at IPython.

    0 讨论(0)
  • 2020-11-30 17:12

    For older versions (2.x) above script works like charm :)

    fernanr@crsatx4 ~ $ cat .bashrc | grep -i python
    #Tab completion for python shell
    export PYTHONSTARTUP=~/.pythonrc
    fernanr@crsatx4 ~ $ . ~/.bashrc
    fernanr@crsatx4 ~ $ echo $?
    0
    fernanr@crsatx4 ~ $ python2
    Python 2.7.5 (default, Jun 11 2019, 14:33:56)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.
    Display all 249 possibilities? (y or n)
    os.EX_CANTCREAT             os.O_WRONLY                 
    
    0 讨论(0)
  • 2020-11-30 17:20

    I may have found a way to do it.

    Create a file .pythonrc

    # ~/.pythonrc
    # enable syntax completion
    try:
        import readline
    except ImportError:
        print("Module readline not available.")
    else:
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    

    then in your .bashrc file, add

    export PYTHONSTARTUP=~/.pythonrc
    

    That seems to work.

    0 讨论(0)
  • 2020-11-30 17:20

    Fix for Windows 10 shell:

    • pip install pyreadline
    • pip install ipython [shell]
    0 讨论(0)
  • 2020-11-30 17:21

    In Python3 this feature is enabled by default. My system didn't have the module readline installed. I am on Manjaro. I didn't face this tab completion issue on other linux distributions (elementary, ubuntu, mint).

    After pip installing the module, while importing, it was throwing the following error-

    ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory

    To solve this, I ran-

    cd /usr/lib ln -s libncursesw.so libncursesw.so.5

    This resolved the import error. And, it also brought the tab completion in the python repl without any creation/changes of .pythonrc and .bashrc.

    0 讨论(0)
  • 2020-11-30 17:28

    Yes. It's built in to 3.6.

    fernanr@gnuruwi ~ $ python3.6
    Python 3.6.3 (default, Apr 10 2019, 14:37:36)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.
    Display all 318 possibilities? (y or n)
    os.CLD_CONTINUED             os.O_RDONLY                  os.ST_NOEXEC                 os.environ                   os.getpid(                   os.readlink(                 os.spawnvpe(
    os.CLD_DUMPED                os.O_RDWR                    os.ST_NOSUID                 os.environb                  os.getppid(                  os.readv(                    os.st
    
    0 讨论(0)
提交回复
热议问题