Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does virtualenv effectively disable Python 3 tab-completion?

When I create an python3 virtual-env, tab-completion (default with python3) does not work any more. Why is that?

virtualenv -p /usr/bin/python3 --system-site-packages ~/venv3.site
. ~/venv3.site/bin/activate

Without --system-site-packages it has the same problem: no tab-completion. If I run python3 without any virtual-env activated, tab-completion works.

like image 987
Robert Siemer Avatar asked Nov 09 '15 13:11

Robert Siemer


2 Answers

This is how I got my tab-completion back:

Added the following to ~/.pythonrc.py:

try:
    import readline
except ImportError:
    print("Module readline not available.")
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

Added the following to ~/.bash_profile:

export PYTHONSTARTUP=$HOME/.pythonrc.py
like image 106
Mike Covington Avatar answered Oct 09 '22 02:10

Mike Covington


Quoting Carl Meyer on this GitHub comment,

Yes, one of the uglier aspects of virtualenv's implementation is that it has to have its own copy of the site module, which is used for all virtualenvs regardless of which version of Python they are created with.

The problem is in the $VIRTUAL_ENV/lib/python3.4/site.py file, which does not setup tab completion. It does not provide the enablerlcompleter function. Compare it with the site.py file distributed with Python 3.

If you're using Python 3.3 or newer, I advise pyvenv instead of virtualenv.

python3 -mvenv ~/venv3.site

Another thing you could do is roll your own Python startup script and refer to it in the PYTHONSTARTUP environment variable. Put tab completion and other startup tweaks in there. See Mike Covington's answer for an example of such script.

like image 22
Rafa Avatar answered Oct 09 '22 00:10

Rafa