Issue #125 resolved

initialization causes slow shell startup

aculich avataraculich created an issue

The virtualenvwrapper initialization process noticeably slows down starting up a new shell. I've tracked it down to the {{{import pkg_resources}}} statement. On my system the delay is greater than half a second (in the output below {{{Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.63}}}) which can be quite annoying when trying to pop open an interactive shell to do something quickly.

Ideally the initialization would happen lazily; it should not be invoked and the $HOME/.virtualenvs directory should not be created unless explicitly required. Lazy initialization would also be helpful in cases where it is installed globally like in Debian which puts it in {{{/etc/bash_completion.d/virtualenvwrapper}}}; which means if you log in as root it creates directories that it probably shouldn't by default.

Mostly this is all is a minor annoyance, but I thought I'd mention it here in case other people ran into similar issues and I'll submit a patch later when I have a time to clean it up.

{{{ /usr/bin/time --verbose python -c 'from virtualenvwrapper.hook_loader import main;' Command being timed: "python -c from virtualenvwrapper.hook_loader import main;" User time (seconds): 0.53 System time (seconds): 0.09 Percent of CPU this job got: 98% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.63 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 64544 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 4910 Voluntary context switches: 1 Involuntary context switches: 88 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0 }}}

Comments (5)

  1. rephorm

    See pull request 11 for a partial solution that delays initialization until the first invocation of an api function. Note: if you use a default virtualenv (i.e. call "workon foo" from you shell init script), then this will have no effect.

  2. Anonymous

    It might be a stupid and temporary solution, but in order to speed up my shell today I aliased sourcing of the script, so that I only run it when I actually want to work on any of my python projects, but not everytime I open a shell.

    I added

    alias sourcevirtualenvwrappersh="source /usr/local/bin/"

    to my bash_profile and now I just run it before I workon into any of my virtualenvs.

  3. Doug Hellmann
    • changed status to open

    Release 3.4 includes a lazy-loading startup script. I'm not entirely happy with it, because it breaks tab completion until the first command is used, but it's a start.

  4. James Cleveland

    EDIT: Removed a bunch of stuff I re-implemented due to not noticing

    If you use lazy, and you want autocomplete, and happen to have zsh (who doesn't these days?) here's a completion file...

    #compdef workon rmvirtualenv showvirtualenv cpvirtualenv 
    for d in $(ls $WORKON_HOME)
        [[ -d $WORKON_HOME/$d ]] && dirs=($dirs $d)
    _arguments "1: :($dirs)"

    If you want to use it just drop in a path listed in $fpath with the name _venvs or something.

  5. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.