11.. _pip-virtualenv :
22
33Further Configuration of Pip and Virtualenv
4- -------------------------------------------
4+ ===========================================
5+
6+ Requiring an active virtual environment for ``pip ``
7+ ---------------------------------------------------
58
69By now it should be clear that using virtual envirtonments is a great way to keep
710your development environment clean and keeping different projects' requirements
@@ -18,6 +21,7 @@ when you use ``pip install``, consider adding the following two lines to your
1821``~/.bashrc `` file:
1922
2023.. code-block :: console
24+
2125 export PIP_REQUIRE_VIRTUALENV=true
2226
2327 After saving this change and sourcing the ``~/.bashrc `` file with ``source ~/.bashrc ``,
@@ -26,14 +30,50 @@ If you try to use ``pip install`` outside of a virtual environment pip will gent
2630remind you that an activated virtual environment is needed to install packages.
2731
2832.. code-block :: console
33+
2934 $ pip install requests
3035 Could not find an activated virtualenv (required).
3136
37+ You can also do this configuration by editing your ``pip.conf `` or ``pip.ini ``
38+ file. ``pip.conf `` is used by Unix and Mac OS X operating systems and it can be
39+ found at:
40+
41+ .. code-block :: console
42+
43+ $HOME/.pip/pip.conf
44+
45+ Similarly, the ``pip.ini `` file is used by Windows operating systems and it can
46+ be found at:
47+
48+ .. code-block :: console
49+
50+ %HOME%\pip\pip.ini
51+
52+ If you don't have a ``pip.conf `` or ``pip.ini `` file at these locations, you can
53+ create a new file with the correct name for your operating system.
54+
55+ If you already have a configuration file, just add the following line under the
56+ ``[global] `` settings to require an active virtual environment:
57+
58+ .. code-block :: console
59+
60+ require-virtualenv = true
61+
62+ If you did not have a configuration file, you will need to create a new one and
63+ add the following lines to this new file:
64+
65+ .. code-block :: console
66+
67+ [global]
68+ require-virtualenv = true
69+
70+
3271 You will of course need to install some packages globally (usually ones that you
3372use across different projects consistenly) and this can be accomplished by adding
3473the following to your ``~/.bashrc `` file:
3574
3675.. code-block :: console
76+
3777 gpip() {
3878 PIP_REQUIRE_VIRTUALENV="" pip "$@"
3979 }
@@ -42,5 +82,3 @@ After saving the changes and sourcing your ``~/.bashrc`` file you can now instal
4282packages globally by running ``gpip install ``. You can change the name of the
4383function to anything you like, just keep in mind that you will have to use that
4484name when trying to install packages globally with pip.
45-
46- -----------------------------------------------------------
0 commit comments