|
1 | 1 | Virtual Environments |
2 | 2 | ==================== |
3 | 3 |
|
4 | | -.. todo:: Explain "Virtual Environments" |
| 4 | +A Virtual Environment, put simply, is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. |
| 5 | + |
| 6 | +For example, you can work on a project which requires Django 1.3 at the same time with a project which requires Django 1.0. |
| 7 | + |
5 | 8 |
|
6 | 9 | virtualenv |
7 | 10 | ---------- |
8 | 11 |
|
9 | | -.. todo:: Write about virtualenv |
| 12 | +`virtualenv <http://pypi.python.org/pypi/virtualenv>`_ is a tool to create isolated Python environments. |
| 13 | + |
| 14 | +Install it via pip:: |
| 15 | + |
| 16 | + $ pip install virtualenv |
| 17 | + |
| 18 | +Basic Usage |
| 19 | +~~~~~~~~~~~ |
| 20 | + |
| 21 | +1. Create a virtual environment:: |
| 22 | + |
| 23 | + $ virtualenv ENVIRONMENT_NAME |
| 24 | + |
| 25 | +This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named ``ENVIRONMENT_NAME``. |
| 26 | + |
| 27 | +2. To begin using the virtual environment, it needs to be activated:: |
| 28 | + |
| 29 | + $ source ENVIRONMENT_NAME/bin/activate |
| 30 | + |
| 31 | +You can then begin installing any new modules without affecting the system default Python or other virtual environments. |
| 32 | + |
| 33 | +3. If you are done working in the virtual environment for the moment, you can deactivate it:: |
| 34 | + |
| 35 | + $ deactivate |
| 36 | + |
| 37 | +This puts you back to the system's default Python interpreter with all its installed libraries. |
| 38 | + |
| 39 | +To delete a virtual environment, just delete its folder. |
10 | 40 |
|
11 | 41 | virtualenvwrapper |
12 | 42 | ----------------- |
13 | 43 |
|
14 | | -.. todo:: Write about virtualenvwrapper |
| 44 | +`virtualenvwrapper <http://www.doughellmann.com/projects/virtualenvwrapper/>`_ provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments into one place. |
| 45 | + |
| 46 | +To install (make sure virtualenv is already installed):: |
| 47 | + |
| 48 | + $ pip install virtualenvwrapper |
| 49 | + $ export WORKON_HOME=~/Envs |
| 50 | + $ source /usr/local/bin/virtualenvwrapper.sh |
| 51 | + |
| 52 | +(Full instructions `here <http://www.doughellmann.com/docs/virtualenvwrapper/#introduction>`_.) |
| 53 | + |
| 54 | +Basic Usage |
| 55 | +~~~~~~~~~~~ |
| 56 | + |
| 57 | +1. Create a virtual environment:: |
| 58 | + |
| 59 | + $ mkvirtualenv ENVIRONMENT_NAME |
| 60 | + |
| 61 | +This creates the ``ENVIRONMENT_NAME`` folder inside ``~/Envs``. |
| 62 | + |
| 63 | +2. Work on a virtual environment:: |
| 64 | + |
| 65 | + $ workon ENVIRONMENT_NAME |
| 66 | + |
| 67 | +**virtualenvwrapper** provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names. |
| 68 | +``workon`` also deactivates whatever environment you are currently in, so you can quickly switch between environments. |
| 69 | + |
| 70 | +3. Deactivating is still the same:: |
| 71 | + |
| 72 | + $ deactivate |
| 73 | + |
| 74 | +4. To delete:: |
| 75 | + |
| 76 | + $ rmvirtualenv ENVIRONMENT_NAME |
| 77 | + |
| 78 | +Other nifty commands include: |
| 79 | + |
| 80 | +``lsvirtualenv`` |
| 81 | + List all of the environments. |
| 82 | + |
| 83 | +``cdvirtualenv`` |
| 84 | + Navigate into the directory of the currently activated virtual environment, so you can browse its ``site-packages``, for example. |
| 85 | + |
| 86 | +``cdsitepackages`` |
| 87 | + Like the above, but directly into ``site-packages`` directory. |
| 88 | + |
| 89 | +``lssitepackages`` |
| 90 | + Shows contents of ``site-packages`` directory. |
15 | 91 |
|
| 92 | +Full list of commands can be found `here <http://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html#managing-environments>`_. |
0 commit comments