Django: Installing Eclipse and PyDev for django
In most common cases of programming Django with Eclipse are that Django is installed in system, like Python.I dislike this method, because it makes confusions for beginners. What Django version I'm in. Why did I download an app "X" and added an Eclipse project. Everything seems to be fine, but I cant install proper dependencies... So it's not a secret. Managing Django distributions is a hard work for junior developer. Let's try to make their work simpler, by this article.
I'll try to show you the right way to install Django environment on Eclipse.
2. Unpack downloaded archive into some dir. It will be installation directory of the eclipse. Usually it doesn't mater where it is stored. So I used "/Users/garmoncheg/Developer/tutorial/eclipse" for this purposes.I would not recommend storing it into default "Applications" folder because of many files besides main app. Eclipse wasn't designed under Mac OS X. So it's better to store it in a "Linux" way.
3. Run and create initial setup steps, like create a workspace:
You may also prefer to check "Keep in dock" option, like I did:
Thats pretty much all about initial Eclipse setup.
Maybe Windows setup and initial steps differs a bit. But I dont seem to meet any trouble here.
Let us now talk about THE Environment! :)
First things forst. We need python.
If you're using Mac OS X 10.6, like I do, you probably have python installed. You can check this by starting console and simply typing "python" like so:
If you're seeing something similar to this, like "Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)", you have python installed already.
If you don't - worry not. There are plenty of articles on how to install python under MAC OS X.
But in general you just may download (in my case Latest STABLE production version 2.7.2) from up here: http://www.python.org/download/.
Also Python Mac OS X install page says:
"Python comes pre-installed on Mac OS X, but due to Apple's release cycle, it's often one or even two years old. The overwhelming recommendation of the "MacPython" community is to upgrade your Python by downloading and installing a newer version from the Python standard release page."
So in general it's a good idea to install a bit later version of python than you have already. But do not go high in the sky to 3.2. The latest version numbers are usually supported later by 3-rd party software and you'll probably need to downgrade python in future.
For now I'm comfortable with version 2.7.1, as you could have notice from previous screenshot...
Nevertheless even preinstalled python would be sufficient for you now and let's go ahead.
Eclipse is a monstrous IDE (Interactive Development Environment, if i'm not mistaken). It often is a subject to such called "Eclipse shock". I had such one while studying it. If you're experiencing something similar afraid not! It's a common practice. Books helped me a lot. I started to read them. (Haven't finished them even now. :) ) I used: Eclipse For Dummies (For Dummies (Computer/Tech)) (Because I like "for Dummies" series) and Eclipse Web Tools Platform: Developing Java™ Web Applications. But torrent versions of them will suit just fine, I suppose... :)
It is situated in Help submenu and called as expected "Marketplace" :). Lets tap this and click Next, choosing Eclipse marketplace inside.
Lets search for "django":
We've found one main plugin to work with python source codes. It is called PyDev. It includes IDE enhancements and code completion for python based languages. That's why we need it. Also it is a main known plugin for editing Django source. Select Install on it.
There will be some confirmations, that you'll have to accept. I won't stop on them deeply. In general: you have to confirm full package install, accept licence, wait while install process goes and finally restart Eclipse. I don't think it will cause you much trouble to guess how to do that.
Now that we have PyDev installed, we can see PyDev project in the New Project create wizard:
Now lt us create Example Django Project to setup environment.
If you select to create new "Pydev Django Project" now, you wont finish it. Finish button simply disagree to be used. :) That's because of python not properly configured to work with Eclipse IDE. Let's make it work.
Go to "Eclipse -> Preferences", open collapsed submenu "PyDev" there and select "Interpreter - Python", like so:
"Auto Config"! :)
It will do some scanning work and show you main system interpreter of python. Somehow in my case it is 2.6. Nevertheless let's hit ok. As I've mentioned earlier, It's not hardly important what Python interpreter is installed with Eclipse for now... You'll see something like this:
Let us hit OK. We will see newly Auto-configured Python interpreter and PYTHONPATH variables.
You may hit Apply and check some other options, like editor background color (I prefer light olive.) or so.
This would be sufficient to write "pure" Python apps. But we're using Django!. If you'll try to create Django project at this point, you would probably see something like this:
Let's now "install" Django. First thing you need is a fresh, or not, Django distribution. You can download it at their official site here: https://www.djangoproject.com/download/. I'll use old version to meet my purposes. (Install another django project, that requires Django-1.1 environment and not the new one.) But you may easily use newest one, if you're planning to write apps from scratch, or so...
Ok. Wait for download to be finished. Unpack "Django-1.1.4.tar.gz" (in my case) somewhere and copy it to your work folder. I used "/Users/garmoncheg/Developer/tutorial/Django-1.1.4". We'll need this path to be static, to connect it to Eclipse IDE.
Let's add this path to Eclipse "PYTHONPATH" (connect folder in preferences). Go to PyDev interpreter settings in Preferences again. Add a folder "Django-1.1.4" (or any other you've chosen) distribution. Eclipse will identify that it is a Django distribution automatically and index all variables. So let's insert it to our Python environment like so:
Hit "New folder", select "Django-1.1.4" dir and wait while indexing process continues.
Than hit "OK" and try to create a new Django project.
Eclipse now allows us to create a new Django project properly.
Let's somehow call "New Project -> PyDev Django Project" and click next. You'll se project initial setup here:
"django_example" and changed option to "Add project directory to the PYTHONPATH". Because I dislike another "src" dir inside project.
Hit next and you'll se something like this inside:
"sqlite3" so we're not changing anything. If you prefer to work from any other Django compatible database type, you can specify it's settings here. Fileds in this form are equal to fields in your future setiings.py file. Also I'm changing Django version from "1.2 or later" to "1.1" in order spell checking to work properly.
Thats all with our project. Let's hit finish.
Eclipse will probably ask for adding a new perspective. You should agree with this. You'll find handy bookmark made especially for python development, looking like so:
Notice handy "PYTHONPATH" directories attached to the project, where you can explore nearly all source you're using. Even entire Django distribution.
Ok now we need this thing to work. You'll get default (not working) run configuration profile if you simply press "Run" at this point. IMHO there is no good auto-config way to make this, so we'll construct our newly made configuration by hand.
Go to "Run" button, hit the small arrow near it and choose "Run configurations" there like so:
Double-click "PyDev Django" and you'll get something like this:
Let's now create run configuration for our example Django project.
First thing to do is enter a configuration name, which is "django_example" in my case. You may, of corse, select any other one you like. Hit the "Browse" button here on the "Project" line and select eclipse project that will be Run. Run configuration will add all PYTHONPATH variables used to project "PYTHONPATH that will be used in the run" field. Than you should select "Main Module" that will be run upon project startup. Usually it's "manage.py" inside a project dir.
On the next tab "Arguments" i'd recommend to insert "runserver --noreload" in order for Eclipse not to reload server after any code changes. You'll easily do it manually later, if needed.
On the Interpreter tab you could specify another interpreter. In our case of clean install we'll probably leave Default.
On the next Refresh tab i'd recommend check "Refresh resources upon completion" it will sync project with filesystem every time you'll relaunch it.
The next "desirable one" tab is "Environment". Here we can specify additional interpreters for our project. Let's add one, because it's empty.
Lets hit "New" like so:
And enter "PYTHONPATH" in the "Name" field.
As for value, it's a bit more complicated here. Lt's hit "Variables" button. Notice "Select variable" window appears with many variables we may need, but without any desirable ones. Hit "Edit Variables...". You must see a new window - "Preferences (filtered)" now. It says "Strings substitution".
Let's hit "New" button. A window with adding new variable will appear. It will look like so:
Let's add a name "Django-1.1" and say it a directory to your downloaded Django distribution. You don't have to enter description, like I did.
Now lets hit "Ok", "Ok", select our newly created variable and hit "Ok" and finally "Ok". :)
A-ha. Almost forgot this. Name of your new variable should be "PYTHONPATH".
You'll probably get Environment tab looking like so:
Now finally we're done making run configuration and ready to hit "Run" button below.
Yo'll probably see Django server running in console in case made everything right.
Thats all for now with setting up Django to work with Eclipse.
Any comments/suggestions/better ways to do somthing? Please drop me a comment below.