• Archives

  • Categories

Apr 08

How to install Python and Django in Windows Vista

This is possibly one of the easier ways to install a Django development environment for your Windows Vista 32-bit machine. (Alternatively, there’s Instant Django, it’s much like Instant Rails where you just extract it out and can start immediately.)

0. Objective

Objective is to create an application with Django. This guide assumes you have no knowledge in Python, Django or Windows Vista. It does, however, assumes you know and already set up MySQL nicely. Just for you information, I use XAMPP Lite for development in PHP. I run XAMPP Lite’s MySQL and administer my MySQL with phpMyAdmin.

Let’s look at the required downloads. We will need 3 things:

  1. Python 2.6: Django requires Python to function, we’ll grab the latest Windows binary here. (Get the one that says something like Python 2.6.2 Windows installer)
  2. Django: You can get Django here. Use option 2 if you don’t know what SVN is about.
  3. MySQL for Python: This lets Python use MySQL, it does not install MySQL. You can get it here. (Download the one that says ‘MySQL-python-1.2.2.win32-py2.5.exe’ inside the ‘mysql-python‘ package.

1. Installing Python 2.6


The installer is pretty straightforward. This guide assumes you install in ‘C:\Python26\’, the default installation directory. (Do not install in your Program Files as Program Files somehow has lesser access rights.) You should not encounter any error.

After completion of the Python 2.6 installation, set up your environment variables. We want to be able to just type ‘django-admin.py startproject mysite’ in the command prompt. In order to do that, you will have to specify the directory that Windows Vista can search for to find ‘django-admin.py’.

We can do so by going to your Control Panel and type ‘environ…’ at the search area. We’ll get the following:

Editing system environment variables in Windows Vista.

Click on ‘Edit the system environment variables’, a dialog box will pop up. Click on ‘Environment Variables…’, you should be greeted with the following dialog box:

Editing the path for environment variables

My path looks like this before I add anything:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem

Append these two directories at the back of what is already there:

;C:\Python26\Scripts;C:\Python26

Remember that each directory has to be separated with a semicolon. My Python is installed in ‘C:\Python26′.

2. Installing Django

Extract the Django package that you download to ‘C:\django\’. Use the Command Prompt to navigate to the folder you extract to, in our case, we can type this command:

cd C:\django

Then we have to set up Django:

python setup.py install

And we wait for the the installation to complete. On completion, Django is now fully installed. Let’s try to create an application now to see if it’s working. I created a folder at ‘C:\projects\’ to store all my projects. Once again, we use the Command Prompt to navigate there:

cd C:\projects

Then we create our first Django application:

django-admin.py startproject mysite

Note your C:\projects, it now has an additional folder called ‘mysite’ with the following files:

mysite/
  __init__.py
  manage.py
  settings.py
  urls.py

Okay, let’s try to see if every thing is working fine.

cd C:\projects\mysite

We will start the server now within the ‘mysite’ directory:

python manage.py runserver

The following should appear:

Validating models…
0 errors found.

Django version 0.97-pre-SVN-unknown, using settings ‘mysite.settings’
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Note that the version number is different, depending on what you download. Now go to your browser and type in ‘http://127.0.0.1:8000/’.

Django worked

And it worked! Congratulations on your first Django-powered page.

3. Installing MySQL for Python

Install MySQL for Python as administrator

Once again, I assumed you had MySQL installed already and know how to run and use it.

Run, in administrative mode, the file you just downloaded – MySQL-python-1.2.2.win32-py2.5.exe.

You may run into errors if you do not install as an administrator due to Windows Vista’s security model.

Now, let’s test if the MySQL installation worked. Run the server if it isn’t started. Edit the database setting in ‘mysite/settings.py’.

Update your database information. Save your file. The server will detect a change once you save your file and start to check for errors again. (I personally find this really cool.)

Here’s what I fill in. Your user may not be ‘root’.

DATABASE_ENGINE = ‘mysql’
DATABASE_NAME = ”
DATABASE_USER = ‘root’
DATABASE_PASSWORD = ”
DATABASE_HOST = ”
DATABASE_PORT = ”

If you see no error messages, the database is correctly set up. To do a bit of testing, we can set our user name wrongly on purpose and watch Python throw out the errors then correct it.

Fill in your DATABASE_NAME correctly and we can start coding already. You can continue on the Django tutorial here.

[Forgive me if there're any mistakes in this tutorial and feedback to me.]

Possibly related:

  1. How to backup and restore in PostgreSQL
  2. How to install PEAR modules using XAMPP
  3. How to import and export MySQL database into an SQL file
  4. How to use TortoiseSVN to get latest Django trunk

“How to install Python and Django in Windows Vista”
24 comments

  1. Jon Sidnell

    Thanks for posting this – it all just worked!! Looking forward to getting my toes wet now :)

    Apr 25

  2. Nice job. Your version is very detailed. Mine is a little more concise:
    http://siliconloop.blogspot.com/2008/04/how-to-run-django-on-windows-vista.html

    Apr 25

  3. edison chan

    nice post, but you miss up apache part!

    Apr 27

  4. KahWee

    @Jon Sidnell: Have fun!

    @edison chan: It doesn’t require Apache to run actually. Django comes with a built-in web server that makes it so easy to quickly start development. Really cool stuff.

    Apr 27

  5. cool job, saved me a lot of googling.. thank you

    Jul 28

  6. Robin

    Thank you for this!

    Aug 13

  7. thanks, great job!!
    not sure in this section, but i use this:
    C:\Python25\Lib\site-packages\django\bin\django-admin.py startproject cms
    and works fine for me :)

    Aug 29

  8. Thanks so much. Been struggling to get django-admin on windows path. Have a nice day!

    Sep 11

  9. Jen

    Hi,
    Thanks so much for the helpful instructions on how to install Django, Python, MySQL.
    I installed them in my new laptop that came with Vista. Everything worked till I tried to edit settings.py file. I kept giving me an error message saying that ‘Access denied for user root@localhost

    Can you think of anything that I may need to change to fix it?

    Thanks,

    Jen

    Oct 30

  10. @jen: Hi, it is likely that there are some problems with your MySQL. Could you try to access your MySQL with your ‘root’ user? Is Django the only app that you have trouble with MySQL?

    Oct 30

  11. very nice tutorial there. I wrote one myself a while ago setting up Django on Windows Vista OS. Never mentioned that you need to be Administrator tho since I only work under that account.
    See this If you like :
    http://normankosmal.com/wordpress/?p=24

    Nov 29

  12. Thanks for this tutorial! I tried following the Django installation guide, but there were a couple of steps/assumptions in their guide that I couldn’t quite sort out, until I read your tutorial.

    You may want to update it to use sqlite3 as the db, as this is included in the latest python versions.

    Thanks again!

    Dec 29

  13. Dwight Taylor

    im getting “type django-admin.py help” for usage after i run django-admin.py startproject mysite but thats about it. the files do not install to the projects folder ive created on the c

    Jan 02

  14. Dwight Taylor

    update you have to change the file association from (cant remember) to “%1″ %* and it should work

    Jan 02

  15. Paul

    RE: running the Django development server on Vista, I also had to open up a port on the firewall before this would work. For example I opened up port 8000, for use only by the local machine. This can only be done if you have Administrators rights on VIsta.

    Feb 17

  16. Ben

    Thanks so much for posting this – saved me lots of grief.

    Now I can get on with my coding!

    Apr 07

  17. rajeev

    im using vista python 2.5 and django 1.1

    I’ve installed django and import django works fine in python interpreter but when i type

    “django-admin startproject mysite”

    a textpad with the following


    #!/usr/bin/env python
    from django.core import management

    if __name__ == “__main__”:
    management.execute_from_command_line()

    pops up, it basically opens up the file django-admin.py how do i fix this.

    Apr 11

  18. For Python 2.6 you can find mysql-python here:
    http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe

    ;D

    Jun 04

  19. jils

    Nice article. Saved lots of time. And could be nice if you include how to handle the issues when installing mysql-python of version 2.5 on top of existing python 2.6. [ version compatibility]

    Sep 27

  20. nrcsousa

    Very useful post. It works. Thanks a lot.

    Oct 18

  21. [...] Описание установки Django под Windows – How to install Python and Django in Windows Vista [...]

    Dec 28

  22. Steve Young

    I was having trouble with the “type django-admin.py help” when starting my project. I am using Vista and found several posts about the problem, but nothing seemed to work. Then I opened regedit and searched for python. I found a few entries that did not have the %* after the “%1″. I changed them and it started working. It seems Vista was not passing the command line variables with the django-admin.py…

    May 13

  23. Steve Young’s instruction works fine!

    regedit
    find:
    “C:\Python26\python.exe” “%1″
    change to:
    “C:\Python26\python.exe” “%1″ %*

    and this
    “C:\Python26\python26.exe” “%1″
    change to:
    “C:\Python26\python26.exe” “%1″ %*
    (in this case start working)

    May 30

  24. rajesh

    hi
    i just installed the python, django & mysql-python. now i dont know how to create the mysql database. will you please give me some idea about how to create the database??

    rajesh

    Jun 04

Leave your comment.


WordPress powered and Django inspired.
Love and elephants come after.
RSS: Posts and comments.