| OLD | NEW |
| (Empty) | |
| 1 User Guide |
| 2 ========== |
| 3 |
| 4 |
| 5 Usage |
| 6 ----- |
| 7 |
| 8 Virtualenv has one basic command:: |
| 9 |
| 10 $ virtualenv ENV |
| 11 |
| 12 Where ``ENV`` is a directory to place the new virtual environment. It has |
| 13 a number of usual effects (modifiable by many :ref:`options`): |
| 14 |
| 15 - :file:`ENV/lib/` and :file:`ENV/include/` are created, containing supporting |
| 16 library files for a new virtualenv python. Packages installed in this |
| 17 environment will live under :file:`ENV/lib/pythonX.X/site-packages/`. |
| 18 |
| 19 - :file:`ENV/bin` is created, where executables live - noticeably a new |
| 20 :command:`python`. Thus running a script with ``#! /path/to/ENV/bin/python`` |
| 21 would run that script under this virtualenv's python. |
| 22 |
| 23 - The crucial packages pip_ and setuptools_ are installed, which allow other |
| 24 packages to be easily installed to the environment. This associated pip |
| 25 can be run from :file:`ENV/bin/pip`. |
| 26 |
| 27 The python in your new virtualenv is effectively isolated from the python that |
| 28 was used to create it. |
| 29 |
| 30 .. _pip: https://pypi.python.org/pypi/pip |
| 31 .. _setuptools: https://pypi.python.org/pypi/setuptools |
| 32 |
| 33 |
| 34 .. _activate: |
| 35 |
| 36 activate script |
| 37 ~~~~~~~~~~~~~~~ |
| 38 |
| 39 In a newly created virtualenv there will also be a :command:`activate` shell |
| 40 script. For Windows systems, activation scripts are provided for |
| 41 the Command Prompt and Powershell. |
| 42 |
| 43 On Posix systems, this resides in :file:`/ENV/bin/`, so you can run:: |
| 44 |
| 45 $ source bin/activate |
| 46 |
| 47 For some shells (e.g. the original Bourne Shell) you may need to use the |
| 48 :command:`.` command, when :command:`source` does not exist. There are also |
| 49 separate activate files for some other shells, like csh and fish. |
| 50 :file:`bin/activate` should work for bash/zsh/dash. |
| 51 |
| 52 This will change your ``$PATH`` so its first entry is the virtualenv's |
| 53 ``bin/`` directory. (You have to use ``source`` because it changes your |
| 54 shell environment in-place.) This is all it does; it's purely a |
| 55 convenience. If you directly run a script or the python interpreter |
| 56 from the virtualenv's ``bin/`` directory (e.g. ``path/to/ENV/bin/pip`` |
| 57 or ``/path/to/ENV/bin/python-script.py``) there's no need for |
| 58 activation. |
| 59 |
| 60 The ``activate`` script will also modify your shell prompt to indicate |
| 61 which environment is currently active. To disable this behaviour, see |
| 62 :envvar:`VIRTUAL_ENV_DISABLE_PROMPT`. |
| 63 |
| 64 To undo these changes to your path (and prompt), just run:: |
| 65 |
| 66 $ deactivate |
| 67 |
| 68 On Windows, the equivalent `activate` script is in the ``Scripts`` folder:: |
| 69 |
| 70 > \path\to\env\Scripts\activate |
| 71 |
| 72 And type ``deactivate`` to undo the changes. |
| 73 |
| 74 Based on your active shell (CMD.exe or Powershell.exe), Windows will use |
| 75 either activate.bat or activate.ps1 (as appropriate) to activate the |
| 76 virtual environment. If using Powershell, see the notes about code signing |
| 77 below. |
| 78 |
| 79 .. note:: |
| 80 |
| 81 If using Powershell, the ``activate`` script is subject to the |
| 82 `execution policies`_ on the system. By default on Windows 7, the system's |
| 83 excution policy is set to ``Restricted``, meaning no scripts like the |
| 84 ``activate`` script are allowed to be executed. But that can't stop us |
| 85 from changing that slightly to allow it to be executed. |
| 86 |
| 87 In order to use the script, you can relax your system's execution |
| 88 policy to ``AllSigned``, meaning all scripts on the system must be |
| 89 digitally signed to be executed. Since the virtualenv activation |
| 90 script is signed by one of the authors (Jannis Leidel) this level of |
| 91 the execution policy suffices. As an administrator run:: |
| 92 |
| 93 PS C:\> Set-ExecutionPolicy AllSigned |
| 94 |
| 95 Then you'll be asked to trust the signer, when executing the script. |
| 96 You will be prompted with the following:: |
| 97 |
| 98 PS C:\> virtualenv .\foo |
| 99 New python executable in C:\foo\Scripts\python.exe |
| 100 Installing setuptools................done. |
| 101 Installing pip...................done. |
| 102 PS C:\> .\foo\scripts\activate |
| 103 |
| 104 Do you want to run software from this untrusted publisher? |
| 105 File C:\foo\scripts\activate.ps1 is published by E=jannis@leidel.info, |
| 106 CN=Jannis Leidel, L=Berlin, S=Berlin, C=DE, Description=581796-Gh7xfJxkx
QSIO4E0 |
| 107 and is not trusted on your system. Only run scripts from trusted publish
ers. |
| 108 [V] Never run [D] Do not run [R] Run once [A] Always run [?] Help |
| 109 (default is "D"):A |
| 110 (foo) PS C:\> |
| 111 |
| 112 If you select ``[A] Always Run``, the certificate will be added to the |
| 113 Trusted Publishers of your user account, and will be trusted in this |
| 114 user's context henceforth. If you select ``[R] Run Once``, the script will |
| 115 be run, but you will be prompted on a subsequent invocation. Advanced users |
| 116 can add the signer's certificate to the Trusted Publishers of the Computer |
| 117 account to apply to all users (though this technique is out of scope of this |
| 118 document). |
| 119 |
| 120 Alternatively, you may relax the system execution policy to allow running |
| 121 of local scripts without verifying the code signature using the following:: |
| 122 |
| 123 PS C:\> Set-ExecutionPolicy RemoteSigned |
| 124 |
| 125 Since the ``activate.ps1`` script is generated locally for each virtualenv, |
| 126 it is not considered a remote script and can then be executed. |
| 127 |
| 128 .. _`execution policies`: http://technet.microsoft.com/en-us/library/dd347641.as
px |
| 129 |
| 130 Removing an Environment |
| 131 ~~~~~~~~~~~~~~~~~~~~~~~ |
| 132 |
| 133 Removing a virtual environment is simply done by deactivating it and deleting th
e |
| 134 environment folder with all its contents:: |
| 135 |
| 136 (ENV)$ deactivate |
| 137 $ rm -r /path/to/ENV |
| 138 |
| 139 The :option:`--system-site-packages` Option |
| 140 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 141 |
| 142 If you build with ``virtualenv --system-site-packages ENV``, your virtual |
| 143 environment will inherit packages from ``/usr/lib/python2.7/site-packages`` |
| 144 (or wherever your global site-packages directory is). |
| 145 |
| 146 This can be used if you have control over the global site-packages directory, |
| 147 and you want to depend on the packages there. If you want isolation from the |
| 148 global system, do not use this flag. |
| 149 |
| 150 Windows Notes |
| 151 ~~~~~~~~~~~~~ |
| 152 |
| 153 Some paths within the virtualenv are slightly different on Windows: scripts and |
| 154 executables on Windows go in ``ENV\Scripts\`` instead of ``ENV/bin/`` and |
| 155 libraries go in ``ENV\Lib\`` rather than ``ENV/lib/``. |
| 156 |
| 157 To create a virtualenv under a path with spaces in it on Windows, you'll need |
| 158 the `win32api <http://sourceforge.net/projects/pywin32/>`_ library installed. |
| 159 |
| 160 |
| 161 Using Virtualenv without ``bin/python`` |
| 162 --------------------------------------- |
| 163 |
| 164 Sometimes you can't or don't want to use the Python interpreter |
| 165 created by the virtualenv. For instance, in a `mod_python |
| 166 <http://www.modpython.org/>`_ or `mod_wsgi <http://www.modwsgi.org/>`_ |
| 167 environment, there is only one interpreter. |
| 168 |
| 169 Luckily, it's easy. You must use the custom Python interpreter to |
| 170 *install* libraries. But to *use* libraries, you just have to be sure |
| 171 the path is correct. A script is available to correct the path. You |
| 172 can setup the environment like:: |
| 173 |
| 174 activate_this = '/path/to/env/bin/activate_this.py' |
| 175 execfile(activate_this, dict(__file__=activate_this)) |
| 176 |
| 177 This will change ``sys.path`` and even change ``sys.prefix``, but also allow |
| 178 you to use an existing interpreter. Items in your environment will show up |
| 179 first on ``sys.path``, before global items. However, global items will |
| 180 always be accessible (as if the :option:`--system-site-packages` flag had been |
| 181 used in creating the environment, whether it was or not). Also, this cannot undo |
| 182 the activation of other environments, or modules that have been imported. |
| 183 You shouldn't try to, for instance, activate an environment before a web |
| 184 request; you should activate *one* environment as early as possible, and not |
| 185 do it again in that process. |
| 186 |
| 187 Making Environments Relocatable |
| 188 ------------------------------- |
| 189 |
| 190 **Note:** this option is somewhat experimental, and there are probably |
| 191 caveats that have not yet been identified. |
| 192 |
| 193 .. warning:: |
| 194 |
| 195 The ``--relocatable`` option currently has a number of issues, |
| 196 and is not guaranteed to work in all circumstances. It is possible |
| 197 that the option will be deprecated in a future version of ``virtualenv``. |
| 198 |
| 199 Normally environments are tied to a specific path. That means that |
| 200 you cannot move an environment around or copy it to another computer. |
| 201 You can fix up an environment to make it relocatable with the |
| 202 command:: |
| 203 |
| 204 $ virtualenv --relocatable ENV |
| 205 |
| 206 This will make some of the files created by setuptools use relative paths, |
| 207 and will change all the scripts to use ``activate_this.py`` instead of using |
| 208 the location of the Python interpreter to select the environment. |
| 209 |
| 210 **Note:** scripts which have been made relocatable will only work if |
| 211 the virtualenv is activated, specifically the python executable from |
| 212 the virtualenv must be the first one on the system PATH. Also note that |
| 213 the activate scripts are not currently made relocatable by |
| 214 ``virtualenv --relocatable``. |
| 215 |
| 216 **Note:** you must run this after you've installed *any* packages into |
| 217 the environment. If you make an environment relocatable, then |
| 218 install a new package, you must run ``virtualenv --relocatable`` |
| 219 again. |
| 220 |
| 221 Also, this **does not make your packages cross-platform**. You can |
| 222 move the directory around, but it can only be used on other similar |
| 223 computers. Some known environmental differences that can cause |
| 224 incompatibilities: a different version of Python, when one platform |
| 225 uses UCS2 for its internal unicode representation and another uses |
| 226 UCS4 (a compile-time option), obvious platform changes like Windows |
| 227 vs. Linux, or Intel vs. ARM, and if you have libraries that bind to C |
| 228 libraries on the system, if those C libraries are located somewhere |
| 229 different (either different versions, or a different filesystem |
| 230 layout). |
| 231 |
| 232 If you use this flag to create an environment, currently, the |
| 233 :option:`--system-site-packages` option will be implied. |
| 234 |
| 235 The :option:`--extra-search-dir` option |
| 236 --------------------------------------- |
| 237 |
| 238 This option allows you to provide your own versions of setuptools and/or |
| 239 pip to use instead of the embedded versions that come with virtualenv. |
| 240 |
| 241 To use this feature, pass one or more ``--extra-search-dir`` options to |
| 242 virtualenv like this:: |
| 243 |
| 244 $ virtualenv --extra-search-dir=/path/to/distributions ENV |
| 245 |
| 246 The ``/path/to/distributions`` path should point to a directory that contains |
| 247 setuptools and/or pip wheels. |
| 248 |
| 249 virtualenv will look for wheels in the specified directories, but will use |
| 250 pip's standard algorithm for selecting the wheel to install, which looks for |
| 251 the latest compatible wheel. |
| 252 |
| 253 As well as the extra directories, the search order includes: |
| 254 |
| 255 #. The ``virtualenv_support`` directory relative to virtualenv.py |
| 256 #. The directory where virtualenv.py is located. |
| 257 #. The current directory. |
| 258 |
| OLD | NEW |