I am trying to install a Python package with this command
pip install <name of package>
I'm getting permission errors and I'm not sure why. I could run it with sudo
, but someone told me that was a bad idea, and I should use a virtualenv instead.
What is a virtualenv? What does it do for me?
Installing packages with
pip
andsudo
will install packages globally, which may break some system tools.By install globally it means you will install your packages in place like
/usr/lib/python2.7/site-package
so if some packages need a previous version of your python packages, this action may break it.virtualenv
allows you to avoid installing Python packages globally by make an isolated python environments. That means it will install packages just in your desire project folder.On mac and linux
After activating you could install your packages using pip
For more information about using it in Windows: How to use virtaulen in Windows
Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it.
Virtual environments, or "virtualenvs" are lightweight, self-contained Python installations, designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.
virtualenv
avoids the need to install Python packages globally. When a virtualenv is active,pip
will install packages within the environment, which does not affect the base Python installation in any way.In Python 3.3 or later, you can create a virtualenv as follows:
For Windows, you should replace
python3
with the full path to python.exe:(This is a typical Python installation; your system may vary.)
In older versions of Python, including Python 2, one of the following commands should work in most cases:
ENV_DIR
should be a non-existent directory. The directory can have any name, but to keep these instructions simple, I will assume you have created your virtualenv in a directory calledvenv
(e.g. withpython3 -m venv ./venv
).To work in your virtualenv, you activate it:
Or use this if you have a windows system:
The
(venv)
in the shell prompt lets you know which virtualenv you have activated, but you can turn this feature off if you do not like it. You can run all the usual Python commands, and they will be local to your virtualenv:python
will run the version of Python that you installed into your virtualenv, so (for example) you don't have to typepython3
to get Python 3. The Python that it runs will have access to all the standard library modules and all the packages you installed into the virtualenv, but (by default) none of the packages installed in the system-widesite-packages
directory.This last rule is important: by restricting your virtualenv to only use locally-installed packages, you can ensure that you control exactly which dependencies your project is using, even if some new system-wide package gets installed or updated next week. If you like, you can get a listing of your installed packages:
pip
can also parse this format and install from it, and it will install the same versions, even if updates have been released in the meantime:You can get out of the virtualenv by deactivating it:
You can create as many virtualenvs as you like, and they won't interfere with each other, nor with your system packages. A virtualenv is "just" a directory with a bunch of binaries and scripts under it, so you can remove a virtualenv the same way you remove any directory (
rm -r venv
on Unix). If the virtualenv is activated when you remove it, you may confuse your shell, so it's probably a good idea todeactivate
first in that case.Some times you are not given root privileges and you might end up not being able to use sudo. Many other times, it's not advisable to use sudo to install packages as it might overwrite some package which might be in use by some other applications.
Virtualenv can help you create a separate environment where you don't need root privileges as well as be able to tailor the environment according to your need. It consists of self-contained python installation which only interacts with your specific created environment.
So basically, it gives you a bit of freedom as well as avoid damaging (or modifying) the root environment which might be hosting many old functionalities of old applications.
Installation is pretty easy too.