erip erip - 1 year ago 53
Python Question

Pip install error django

Last summer I wanted to learn to do web dev, so I installed Django 1.8, an unstable release. I installed it without pip. I recently decided to try again, but wanted to do work with the stable release, 1.7.1, and wanted to install with pip for simplicity.

I read that in order to remove Django that was installed without pip, one must delete the entire

, so I did this.

Since then, I've been getting funny errors. When I issue 'pip install django', I get

bash-3.2$ pip install django
Downloading/unpacking django
Downloading Django-1.7.1-py2.py3-none-any.whl (7.4MB): 7.4MB downloaded
Installing collected packages: django
Cleaning up...
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/", line 122, in main
status =, args)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/", line 283, in run
requirement_set.install(install_options, global_options, root=options.root_path)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/", line 1435, in install
requirement.install(install_options, global_options, *args, **kwargs)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/", line 671, in install
self.move_wheel_files(self.source_dir, root=root)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/", line 901, in move_wheel_files
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/", line 215, in move_wheel_files
clobber(source, lib_dir, True)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/", line 205, in clobber
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django'

When I check the spurious directory, I find that /django doesn't exist.
Issuing sudo will silence the errors, but I find that I still can't start a django project.

Has anyone seen this before or have any idea how to fix this?

Answer Source

This is not really an error - as your normal user account cannot write to the global /Library folder, you are getting this permission denied error from the operating system.

The best practice is to work in a virtual environment called virtualenv for short. This makes sure that when you are experimenting with Python packages, you don't accidentally corrupt the system-wide installation of Python.

On a Mac (which you are using) there might be other system utilities that rely on Python and they may stop working if the system default Python is corrupted.

So the first step is to install virtualenv globally on your system. To do this, you need superuser permissions but this is done only once:

sudo easy_install virtualenv

The above will ask for a password - just enter your normal user password.

Once the above command finishes, as your normal user run the following from the command line:

$ virtualenv my_env  # This creates a new Python environment called my_env, completely
                     # separate from your system wide install.
$ source my_env/bin/activate # Activates the virtual envrionment
(my_env) $ pip install django # Install django in this new environment the (my_env)
                              # indicates you are working inside a virtual environment


(my_env) $ deactivate  # Deactivates the environment,
                       # returning you to the normal system Python

Anything you install in the virtual environment is only available within the virtual environment, so you need to make sure you are in the correct environment when running your scripts.