How to setup a Django / python development env on Windows
Install Python
- Download and install the latest Python installer from here
- Change the %PATH% variable to add the path to the python.exe file
Install VirtualEnv
- Download virtualenv from here
- Install virtualenv with the command:
python virtualenv.py YOUR_ENVIRONMENT
- Activate the new virtual env by :
YOUR_ENVIRONMENT\Scripts\activate.bat
- Add the paths to the ‘Scripts’ and ‘Lib’ folders in %PATH%. It should be of the form:
E:\Python27\;E:\Python27\YOUR_ENVIRONMENT\Lib\;E:\Python27\YOUR_ENVIRONMENT\Scripts;
Install Django
- Make sure your virtual env is activated
- Install Django by: pip install django
- In case of the following error:
File "E:\Python27\YOUR_ENVIRONMENT\Scripts\django-admin.py", line 2, in from django.core import management ImportError: No module named django.core
- Check the folder permissions of the “django” folder inside “YOUR_ENVIRONMENT\Lib\site-packages“. Remove any read-only permission settings.
- Use this command to use the “correct” Python (the one inside the virtual environment) to open the .py files:
ftype Python.File="E:\Python27\YOUR_ENVIRONMENT\Scripts\python.exe" "%1" %*
- Run “django-admin.py startproject YOUR_PROJECTNAME”. If this works, that means Django is installed fine.
Install Postgresql
- Download Postgresql from here and install it.
- Modify YOUR_PROJECT\settings.py, and change the lines to include your DB config:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'YOUR_DATABASE_NAME', # Or path to database file if using sqlite3. 'USER': 'YOUR_USER_OR_ROLE_NAME', # Not used with sqlite3. 'PASSWORD': 'YOUR_PASSWORD', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } - Run
python manage.py runserver
This will probably fail… with an error like this:
ImportError: No module named psycopg2.extensions
Don’t worry.. this seems to be normal
Psycopg2 does not work with VirtualEnv. - Download THE CORRECT VERSION OF Psycopg2. You CANNOT install this file. You have to extract the files inside the exe file with an utility like 7Zip. You will get a folder called “psycopg2″ (and possibly a file called “psycopg2-2.4.5-py2.7.egg-info”). Copy these into
E:\Python27\VIRTUAL_ENVIRONMENT\Lib\
Happy coding!
Pingback: Django, Coffeescript, and Backbone.js | Ryan Mendieta