Wiki source code of VirtualEnv
Version 14.1 by David De La Harpe Golden on 2026/05/17 16:32
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
![]() |
1.1 | 1 | While historically I use python virtualenv helpers like |
| 2 | |||
| 3 | * [[vex>>https://pypi.org/project/vex/]] | ||
| 4 | * [[virtualenvwrapper>>https://pypi.org/project/virtualenvwrapper/]] | ||
![]() |
12.1 | 5 | * [[virtualenvwrapper-win>>https://pypi.org/project/virtualenvwrapper-win/]] (if on windows) |
![]() |
1.1 | 6 | |
| 7 | ...since virtualenv is now in the python stdlib, I have lately pared it down to two little helpers | ||
| 8 | |||
| 9 | * ##mkve## - makes a new basic virtualenv. | ||
![]() |
13.1 | 10 | * ##ve## - launches a subshell with the virtualenv active - in contrast to the weird way official virtualenv ##activate## does within the current shell session i.e ##ve## is like ##vex## but less fancy. |
![]() |
1.1 | 11 | |
| 12 | {{code language="bash"}} | ||
![]() |
5.1 | 13 | $ mkve BlahVenv |
| 14 | $ ve BlahVenv | ||
| 15 | $ pip install av | ||
| 16 | $ exit | ||
| 17 | {{/code}} | ||
![]() |
1.1 | 18 | |
| 19 | |||
| 20 | == mkve == | ||
| 21 | |||
| 22 | {{code language="bash"}} | ||
| 23 | #!/bin/bash | ||
![]() |
8.1 | 24 | # make a new python venv: |
| 25 | # mkve SomeVenv | ||
![]() |
1.1 | 26 | |
| 27 | if [ -e "$1" ] ; then | ||
| 28 | echo >&2 "Already exists. Use python -m venv ... directly for complex args." | ||
| 29 | else | ||
| 30 | exec python -m venv "$1" | ||
| 31 | fi | ||
| 32 | |||
![]() |
3.1 | 33 | {{/code}} |
| 34 | |||
![]() |
1.1 | 35 | |
![]() |
5.1 | 36 | == ve == |
![]() |
4.1 | 37 | |
![]() |
1.1 | 38 | {{code language="bash"}} |
![]() |
3.1 | 39 | |
![]() |
1.1 | 40 | #!/bin/bash |
![]() |
8.1 | 41 | # use an existing python venv in a subshell, exit to exit: |
| 42 | # ve SomeVenv | ||
![]() |
1.1 | 43 | |
| 44 | if [ -f "$1/bin/activate" ]; then | ||
| 45 | exec bash --rcfile <(cat ~/.bashrc $1/bin/activate) | ||
| 46 | else | ||
| 47 | echo >&2 "Missing Python Virtual Env (should have venv/bin/activate script)" | ||
| 48 | fi | ||
| 49 | |||
![]() |
3.1 | 50 | {{/code}} |
![]() |
1.1 | 51 | |
| 52 | |||
![]() |
8.1 | 53 | == Shell Prompt == |
![]() |
3.1 | 54 | |
![]() |
14.1 | 55 | I have a fancy non-default prompt in my ##.bashrc## that shows a little snake if in a python virtualenv |
![]() |
7.1 | 56 | |
| 57 | {{code language="bash"}} | ||
| 58 | # venvs themselves prefix these days, new ones created by stdlib honor this disable, allowing us to customize | ||
| 59 | export VIRTUAL_ENV_DISABLE_PROMPT=1 | ||
| 60 | PS1='${debian_chroot:+\[\033[01;33m\]🌲($debian_chroot)\[\033[00m\]}${VIRTUAL_ENV:+\[\033[01;36m\]🐍($(basename $VIRTUAL_ENV))\[\033[00m\]}\[\033[38;5;108m\]\u@\h\[\033[38;5;128m\]:\[\033[38;5;139m\]\w\[\033[38;5;129m\]\$\[\033[00m\] ' | ||
| 61 | {{/code}} | ||
| 62 | |||
| 63 | |||
| 64 |
