I realize, of course, that virtualenv is a whole tag, and having a single question summarizing the whole topic is beyond the scope of the site. I want an introductory question that goes over enough of the basics to serve as a reasonable dupe target for questions about Python packaging where a virtualenv is obviously the correct answer.
Some of the questions in this search are the kinds of questions I am thinking of. Most of the time, sudo pip install foo
is a Bad Idea, or at least an idea which merits a lot of long and hard thought about whether it's workable in a given scenario. Unfortunately, this response often gets a lot of resistance from the asker, who's already learned just enough to conclude that "Installing software is a simple problem, which in the Python universe is solved with Pip, therefore any solution which involves some random tool that isn't Python or Pip must be over-engineered."
A typical example, minus the OP pushback, can be found in the search result with the most votes:
OSX El Capitan: sudo pip install OSError: [Errno: 1] Operation not permitted
In this case, the OP tried to run sudo pip install
and it broke horribly. The solution was to use a virtualenv instead.
If you remove is:question
from that search, or change it to is:answer
, you can also see that our answerers are giving out a lot of bad or at least questionable advice. Some of these answers may be reasonable depending on the specific circumstances of the question, but I doubt that applies to every last one of these answers.
I want a canonical question/answer that, at a minimum:
- Explains briefly what a virtualenv is.
- Explains why running commands such as
sudo pip install foo
is a bad idea. - Provides basic instructions for creating a virtualenv and installing things into it.
- This should cover both Python 2.7 and Python 3+, given a reasonably typical system configuration (e.g. Windows/Mac/Ubuntu). Note that ensurepip not existing is apparently considered "reasonable" by some people.
- Is sufficiently compelling and results-oriented that users who want everything to "just work" will not reject virtualenv as overly complicated after reading one or two paragraphs of the answer. For example, while inside the virtualenv:
- Show that
python
runs the preferred Python version regardless of the system version. - Show that
pip install foo
works, no sudo required. - Perhaps demonstrate
pip freeze
/pip install -r
.
- Show that
- Briefly name-drops
pip install --user
for people who absolutely insist on making things difficult for themselves.
The title of this question would probably be something like this:
What is a virtualenv and why should I use one?
Is it a good idea to create this canonical question?
(Alternatively, does a suitable Q/A pair already exist for this? I searched a bit but couldn't really find anything.)
And created! Given the positive response to this question, I went ahead and posted a basic Q&A on this. The answer is community wiki because A) I don't need the rep and B) I'd appreciate help editing it into shape. Please feel free to edit the answer, or the question, if you see any way of improving either of them.