Skip to content

Commit d17acbf

Browse files
author
Can Ibanoglu
committed
Added virtual environment enforcing for pip for the osx installation tutorial
1 parent be9ffbb commit d17acbf

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

docs/starting/install/osx.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,49 @@ An useful set of extensions to virtualenv is available in virtualenvwrapper,
119119
`RTFD <http://virtualenvwrapper.readthedocs.org/en/latest/>`_ to find out more.
120120

121121

122+
A note about Pip and Virtualenv
123+
-------------------------------
124+
125+
By now it should be clear that using virtual environments is a great way to keep
126+
your development environment clean and keeping different projects' requirements
127+
separate.
128+
129+
When you start working on many different projects, it can be hard to remember to
130+
activate the related virtual environment when you come back to a specific project.
131+
As a result of this, it is very easy to install packages globally while thinking
132+
that you are actually installing the package for the virtual environment of the
133+
project. Over time this can result in a messy global package list.
134+
135+
In order to make sure that you install packages to your active virtual environment
136+
when you use ``pip install``, consider adding the following two lines to your
137+
``~/.bashrc`` file:
138+
139+
.. code-block:: console
140+
export PIP_REQUIRE_VIRTUALENV=true
141+
142+
After saving this change and sourcing the ``~/.bashrc`` file with ``source ~/.bashrc``,
143+
pip will no longer let you install packages if you are not in a virtual environment.
144+
If you try to use ``pip install`` outside of a virtual environment pip will
145+
gently remind you that an activated virtual environment is needed to install
146+
packages.
147+
148+
.. code-block:: console
149+
$ pip install requests
150+
Could not find an activated virtualenv (required).
151+
152+
You will of course need to install some packages globally and this can be accomplished
153+
by adding the following to your ``~/.bashrc`` file:
154+
155+
.. code-block:: console
156+
gpip() {
157+
PIP_REQUIRE_VIRTUALENV="" pip "$@"
158+
}
159+
160+
After saving the changes and sourcing your ``~/.bashrc`` file you can now install
161+
packages globally by running ``gpip install``. You can change the name of the
162+
function to anything you like, just keep in mind that you will have to use that
163+
name when trying install packages globally with pip.
164+
122165
--------------------------------
123166

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

0 commit comments

Comments
 (0)