Skip to content

Commit 2454c81

Browse files
committed
Merge pull request realpython#338 from canibanoglu/pip-venv
Added virtual environment enforcing for pip for the osx installation tutorial
2 parents 787bb76 + 2f9fc82 commit 2454c81

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

docs/starting/install/osx.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ copy your code out of it, and then delete the main directory for the environment
118118
An useful set of extensions to virtualenv is available in virtualenvwrapper,
119119
`RTFD <http://virtualenvwrapper.readthedocs.org/en/latest/>`_ to find out more.
120120

121-
122121
--------------------------------
123122

124123
This page is a remixed version of `another guide <http://www.stuartellis.eu/articles/python-development-windows/>`_,

docs/starting/pip-virtualenv.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
.. _pip-virtualenv:
2+
3+
Further Configuration of Pip and Virtualenv
4+
===========================================
5+
6+
Requiring an active virtual environment for ``pip``
7+
---------------------------------------------------
8+
9+
By now it should be clear that using virtual envirtonments is a great way to keep
10+
your development environment clean and keeping different projects' requirements
11+
separate.
12+
13+
When you start working on many different projects, it can be hard to remember to
14+
activate the related virtual environment when you come back to a specific project.
15+
As a result of this, it is very easy to install packages globally while thinking
16+
that you are actually installing the package for the virtual environment of the
17+
project. Over time this can result in a messy global package list.
18+
19+
In order to make sure that you install packages to your active virtual environment
20+
when you use ``pip install``, consider adding the following two lines to your
21+
``~/.bashrc`` file:
22+
23+
.. code-block:: console
24+
25+
export PIP_REQUIRE_VIRTUALENV=true
26+
27+
After saving this change and sourcing the ``~/.bashrc`` file with ``source ~/.bashrc``,
28+
pip will no longer let you install packages if you are not in a virtual environment.
29+
If you try to use ``pip install`` outside of a virtual environment pip will gently
30+
remind you that an activated virtual environment is needed to install packages.
31+
32+
.. code-block:: console
33+
34+
$ pip install requests
35+
Could not find an activated virtualenv (required).
36+
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+
71+
You will of course need to install some packages globally (usually ones that you
72+
use across different projects consistenly) and this can be accomplished by adding
73+
the following to your ``~/.bashrc`` file:
74+
75+
.. code-block:: console
76+
77+
gpip() {
78+
PIP_REQUIRE_VIRTUALENV="" pip "$@"
79+
}
80+
81+
After saving the changes and sourcing your ``~/.bashrc`` file you can now install
82+
packages globally by running ``gpip install``. You can change the name of the
83+
function to anything you like, just keep in mind that you will have to use that
84+
name when trying to install packages globally with pip.

0 commit comments

Comments
 (0)