3

Trying to learn Django on windows is getting quite discouraging because every time I try to take another step there are errors.

This is my setup; Windows 7 Python 2.7 Installed mySQL-python 1.2.3 Installed virtualenv

I've created a mysite directory on my drive. In the command prompt I changed into that directory, and created a virtualenv (without any extra commands) called "mysite_env". I activated the environment and used the command "pip install django". I used the django-admin.py to start a new project called mysite. So now my folder "mysite" contains "mysite" and "mysite_env" directories.

This is what happens when I try to use the django runserver utility.

(mysite_env) c:\mysite\mysite>python manage.py runserver
Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <dja
ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x000000
0003073908>>
Traceback (most recent call last):
  File "c:\mysite\mysite_env\lib\site-packages\django\core\management\commands\r
unserver.py", line 88, in inner_run
    self.validate(display_num_errors=True)
  File "c:\mysite\mysite_env\lib\site-packages\django\core\management\base.py",
line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "c:\mysite\mysite_env\lib\site-packages\django\core\management\validation
.py", line 28, in get_validation_errors
    from django.db import models, connection
  File "c:\mysite\mysite_env\lib\site-packages\django\db\__init__.py", line 78,
in <module>
    connection = connections[DEFAULT_DB_ALIAS]
  File "c:\mysite\mysite_env\lib\site-packages\django\db\utils.py", line 93, in
__getitem__
    backend = load_backend(db['ENGINE'])
  File "c:\mysite\mysite_env\lib\site-packages\django\db\utils.py", line 33, in
load_backend
    return import_module('.base', backend_name)
  File "c:\mysite\mysite_env\lib\site-packages\django\utils\importlib.py", line
35, in import_module
    __import__(name)
  File "c:\mysite\mysite_env\lib\site-packages\django\db\backends\mysql\base.py"
, line 14, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo
dule named MySQLdb

I have searched stackoverflow and the internet and cannot find anything that works. I wish I could get into some actual coding instead of just getting caught up in all this stuff. Seems like macs are way better for coding in python+django

Brent
  • 283
  • 1
  • 5

6 Answers6

2

for windows 7 32 bit installation of python 2.7 .. activate the virtual environment and then at the environment prompt type

easy_install mysql-python

using pip did not work for me

bir singh
  • 136
  • 4
1

If you have installed mysql-python on C:/python27 or globally, then just copy paste the following files from "C:/python/lib/site-packages" to your virtual environment "/lib/site-packages"

  • MySQL_python-1.2.4-py2.7.egg-info(folder)
  • MySQLdb(folder)
  • _mysql_exceptions.py/.pyc/.pyo
  • _mysql.pyd

**Don't need to copy mysql files from "C:\Python27\Lib\site-packages\django\db\backends" or "C:\Python27\Lib\site-packages\django\contrib\gis\db\backends". It worked like a charm

ruddra
  • 50,746
  • 7
  • 78
  • 101
1

VIrtualenv hasn't inherited your global Python libraries, it doesn't by default anymore.

You can recreate the virtualenv with the site packages, or you can install the mysql python module, which is built in C, and therefore less simple than most pip installs.

To install mysqldb under virtualenv, you need to be able to compile the mysql module, which means you need all the general compliation tools, the python header libraries and the mysql client ones (Under ubuntu/debian these are packages build-essential, python-dev & libmysqlclient16-dev ), at this point pip install MySQL-python should work, and from there you should be able to use mysql within django.

Doing this under windows, however, is complicated. One thing you can also do is add a file to the site-packages directory inside your virtual environment (lib/python2.7/site-packages) called something like mysql.pth (anything.pth will do) which contains a single line with the full path to the location of the mysql python libraries. This will add that directory to the searched path, and should also mean django can find the libraries.

Aquarion
  • 591
  • 3
  • 17
1

Extending previous answer, to inherit from system's site-packages with a recent version of virtualenv, use

virtualenv --system-site-packages test
saaj
  • 51
  • 2
0
user
  • 17,781
  • 20
  • 98
  • 124
0

Looks like MySQLdb is not installed or at least not in the right place.

I used itsadok's answer on Integrating MySQL with Python in Windows for my windows installation. You just download the installer and install it. I don't know how it goes with virtualenv.

Community
  • 1
  • 1
Marius Grigaitis
  • 2,520
  • 3
  • 23
  • 30