|
| 1 | +================= |
| 2 | + Tips and Tricks |
| 3 | +================= |
| 4 | + |
| 5 | +This is a list of user-contributed tips for making virtualenv and |
| 6 | +virtualenvwrapper even more useful. If you have tip to share, drop me |
| 7 | +an email or post a comment on `this blog post |
| 8 | +<http://blog.doughellmann.com/2010/01/virtualenvwrapper-tips-and-tricks.html>`__ |
| 9 | +and I'll add it here. |
| 10 | + |
| 11 | +zsh Prompt |
| 12 | +========== |
| 13 | + |
| 14 | +From `Nat <http://www.blogger.com/profile/16779944428406910187>`_: |
| 15 | + |
| 16 | +Using zsh, I added some bits to ``$WORKON_HOME/post(de)activate`` to show |
| 17 | +the active virtualenv on the right side of my screen instead. |
| 18 | + |
| 19 | +in ``postactivate``:: |
| 20 | + |
| 21 | + PS1="$_OLD_VIRTUAL_PS1" |
| 22 | + _OLD_RPROMPT="$RPROMPT" |
| 23 | + RPROMPT="%{${fg_bold[white]}%}(env: %{${fg[green]}%}`basename \"$VIRTUAL_ENV\"`%{${fg_bold[white]}%})%{${reset_color}%} $RPROMPT" |
| 24 | + |
| 25 | +and in ``postdeactivate``:: |
| 26 | + |
| 27 | + RPROMPT="$_OLD_RPROMPT" |
| 28 | + |
| 29 | +Adjust colors according to your own personal tastes or environment. |
| 30 | + |
| 31 | +Updating cached ``$PATH`` entries |
| 32 | +================================= |
| 33 | + |
| 34 | +From `Nat <http://www.blogger.com/profile/16779944428406910187>`_: |
| 35 | + |
| 36 | +I also added the command 'rehash' to ``$WORKON_HOME/postactivate`` and |
| 37 | +``$WORKON_HOME/postdeactivate`` as I was having some problems with zsh |
| 38 | +not picking up the new paths immediately. |
| 39 | + |
| 40 | +Tying to pip's virtualenv support |
| 41 | +================================= |
| 42 | + |
| 43 | +Via http://becomingguru.com/: |
| 44 | + |
| 45 | +Add this to your shell login script to make pip use the same directory |
| 46 | +for virtualenvs as virtualenvwrapper:: |
| 47 | + |
| 48 | + export PIP_VIRTUALENV_BASE=$WORKON_HOME |
| 49 | + |
| 50 | +and Via Nat: |
| 51 | + |
| 52 | +in addition to what becomingguru said, this line is key:: |
| 53 | + |
| 54 | + export PIP_RESPECT_VIRTUALENV=true |
| 55 | + |
| 56 | +That makes pip detect an active virtualenv and install to it, without |
| 57 | +having to pass it the -E parameter. |
| 58 | + |
| 59 | +Creating Project Work Directories |
| 60 | +================================= |
| 61 | + |
| 62 | +Via `James <http://www.blogger.com/profile/02618224969192901883>`_: |
| 63 | + |
| 64 | +In the ``postmkvirtualenv`` script I have the following to create a |
| 65 | +directory based on the project name, add that directory to the python |
| 66 | +path and then cd into it:: |
| 67 | + |
| 68 | + proj_name=$(echo $VIRTUAL_ENV|awk -F'/' '{print $NF}') |
| 69 | + mkdir $HOME/projects/$proj_name |
| 70 | + add2virtualenv $HOME/projects/$proj_name |
| 71 | + cd $HOME/projects/$proj_name |
| 72 | + |
| 73 | + |
| 74 | +In the ``postactivate`` script I have it set to automatically change |
| 75 | +to the project directory when I use the workon command:: |
| 76 | + |
| 77 | + proj_name=$(echo $VIRTUAL_ENV|awk -F'/' '{print $NF}') |
| 78 | + cd ~/projects/$proj_name |
| 79 | + |
| 80 | +Automatically Run workon When Entering a Directory |
| 81 | +================================================== |
| 82 | + |
| 83 | +`Justin Lily posted |
| 84 | +<http://justinlilly.com/blog/2009/mar/28/virtualenv-wrapper-helper/>`__ |
| 85 | +about about some code he added to his shell environment to look at the |
| 86 | +directory each time he runs ``cd``. If it finds a ``.venv`` file, it |
| 87 | +activates the environment named within. On leaving that directory, |
| 88 | +the current virtualenv is automatically deactivated. |
| 89 | + |
| 90 | +`Harry Marr <http://www.blogger.com/profile/17141199633387157732>`__ |
| 91 | +wrote a similar function that works with `git repositories |
| 92 | +<http://hmarr.com/2010/jan/19/making-virtualenv-play-nice-with-git/>`__. |
| 93 | + |
| 94 | +Installing Common Tools Automatically in New Environments |
| 95 | +========================================================= |
| 96 | + |
| 97 | +Via `rizumu <http://rizumu.myopenid.com/>`__: |
| 98 | + |
| 99 | +I have this postmkvirtualenv to install the get a basic setup. |
| 100 | + |
| 101 | +:: |
| 102 | + |
| 103 | + $ cat postmkvirtualenv |
| 104 | + #!/usr/bin/env bash |
| 105 | + curl -O http://python-distribute.org/distribute_setup.p... />python distribute_setup.py |
| 106 | + rm distribute_setup.py |
| 107 | + easy_install pip==dev |
| 108 | + pip install Mercurial |
| 109 | + |
| 110 | +Then I have a pip requirement file with my dev tools. |
| 111 | + |
| 112 | +:: |
| 113 | + |
| 114 | + $ cat developer_requirements.txt |
| 115 | + ipdb |
| 116 | + ipython |
| 117 | + pastescript |
| 118 | + nose |
| 119 | + http://douglatornell.ca/software/python/Nosy-1.0.tar.gz |
| 120 | + coverage |
| 121 | + sphinx |
| 122 | + grin |
| 123 | + pyflakes |
| 124 | + pep8 |
| 125 | + |
| 126 | +Then each project has it's own pip requirement file for things like |
| 127 | +PIL, psycopg2, django-apps, numpy, etc. |
| 128 | + |
| 129 | +Changing the Default Behavior of ``cd`` |
| 130 | +======================================= |
| 131 | + |
| 132 | +Via `mae <http://www.blogger.com/profile/10879711379090472478>`__: |
| 133 | + |
| 134 | +This is supposed to be executed after workon, that is as a |
| 135 | +``postactivate`` hook. It basically overrides ``cd`` to know about the |
| 136 | +VENV so instead of doing ``cd`` to go to ``~`` you will go to the venv |
| 137 | +root, IMO very handy and I can't live without it anymore. if you pass |
| 138 | +it a proper path then it will do the right thing. |
| 139 | + |
| 140 | +:: |
| 141 | + |
| 142 | + cd () { |
| 143 | + if (( $# == 0 )) |
| 144 | + then |
| 145 | + builtin cd $VIRTUAL_ENV |
| 146 | + else |
| 147 | + builtin cd "$@" |
| 148 | + fi |
| 149 | + } |
| 150 | + |
| 151 | + cd |
0 commit comments