Installing Django and MongoDB in your virtualenv


Nitin Venkatesh's Gravatar

Nitin Venkatesh
published Feb. 20, 2014, 1:09 a.m.


This post is another in the series where I experiment with Django being a total newbie.

Things got a little messy when I wanted to try and use MongoDB in Django, (things tend to get messy when you are a complete noob, I know :P ). Anyways, here is the error I encountered when I tried installing django-mongodb-engine:

ImportError: Could not import settings 'project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named project.settings

----------------------------------------
Cleaning up...

Setting two environment variables - PYTHONPATH and DJANGO_SETTINGS_MODULE. So first, cd into your project folder. If you set the project directory in your virtualenv, it should happen automatically when you type workon project-name.

In this example, the project name is headache-diary and assumes you have started a Django project with django-admin.py startproject project_name.

Once you're in your project folder, print out it's full path using echo $PWD and copy it. To directly copy it in your clipboard - echo $PWD | xclip -sel clip

(headache-diary)nitin@jane-saucy:~/headache_diary$ echo $PWD
/home/nitin/headache_diary

Now, open up the postactivate file of your virtualenv. It should be in a location similar to ~/.virtualenvs/project-name/bin/postactivate and paste the following lines. Please make sure to replace the project_name with the name of your Django project.

export PYTHONPATH=$PYTHONPATH:/home/nitin/project_name
export DJANGO_SETTINGS_MODULE="project_name.settings"

So, your postactivate file should be looking something like this now.

(headache-diary)nitin@jane-saucy:~/headache_diary$ cat ~/.virtualenvs/headache-diary/bin/postactivate 
#!/bin/bash
# This hook is run after this virtualenv is activated.

export PYTHONPATH=$PYTHONPATH:/home/nitin/headache_diary
export DJANGO_SETTINGS_MODULE="headache_diary.settings"

Now, source the file,

(headache-diary)nitin@jane-saucy:~/headache_diary$ source ~/.virtualenvs/headache-diary/bin/postactivate

Now that everything's set, let's install django-mongodb-engine from PyPI using pip. (I deviated from the installation instructions on the Django-mongodb-engine site here since it fetches the GitHub repo since it's much more easier and also a lot of bandwidth)

(headache-diary)nitin@jane-saucy:~/headache_diary$ pip install django-mongodb-engine
Downloading/unpacking django-mongodb-engine
Downloading/unpacking pymongo (from django-mongodb-engine)
Downloading/unpacking djangotoolbox>=1.6.0 (from django-mongodb-engine)
Installing collected packages: django-mongodb-engine, pymongo, djangotoolbox
  Running setup.py install for django-mongodb-engine

  Running setup.py install for pymongo
    ....
    .... # Output removed to keep it short
    ....

  Running setup.py install for djangotoolbox

Successfully installed django-mongodb-engine pymongo djangotoolbox
Cleaning up...

(headache-diary)nitin@jane-saucy:~/headache_diary$ pip list
argparse (1.2.1)
Django (1.6.2)
django-mongodb-engine (0.5.1)
djangotoolbox (1.6.2)
pip (1.5.2)
pymongo (2.6.3)
setuptools (2.1)
wsgiref (0.1.2)

And that's it! You're done! You can double-check if pacakges were installed without any problems. Just try to import the django_mongodb_engine file from the python shell.

(headache-diary)nitin@jane-saucy:~/headache_diary$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django_mongodb_engine
>>>

As you can see, no errors - meaning, django_mongodb_engine package was successfully installed!