Jan 10, 2012

Establishing Dev environment with PyCharm + Virtualenv for Django development

I'm a fan of IDE's. Also I'm a fan of GUI's. I also use console where needed, but why waist time typing commands when you can just point and click. I've been using Eclipse + PyDev for almost a year and recently switched to PyCharm.

Why I prefer PyCharm:- Template Debugging. (you can set a normal breakpoints in templates)- Easy Virtualenv connection. (Set an interpreter from your virtualenv and you're ready to roll)- Has excellent set of most common CVS integrations (Git, SVN, Redmine, etc...)- Has looots of tiny tasty things for coding and proper code highlighting over JS and HTML out of the box)- Has proper and shiny themes out of the box.So I'm a blind minded blond, it seems now. But design of this IDE is really attractive IMHO :)- OH and it's quite quick and has ALL the functions I need.

Here is a brief instruction to setup PyCharm IDE to work with my typical Django project.

1. First thing you need is a PyCharm installation. You can get it here (official website) for a 30 day trial for free. And this project has way of free licensing for Open Source projects. For everyone else it's 100$. Guess what? I'm open-source developer :).

2. I guess you'll be ok to install it by yourself. Nothing too special here. Just a normal setup depending on your platform. (I use Mac OS X Lion. And for me it's just a "drag to ~/Applications folder").3. Now that you have it installed you can dive into it's magic. lets setup a new project like you do most of the times from console first.

Now that we have created typical project with virtual environment and a project dir inside it.

4. Let's connect it to PyCharm. Press "Open Directory" in either ("File > Open Directory") main menu or on this fancy welcome screen.

Actually have to notice here. Instead of "Project" architecture, PyCharm is sticks himself to folders. He lays there his ".idea" hidden directory with project configurations, run configurations etc. PyCharm stores all it's settings there. So, if you need to open new project, press "Open Directory" and select new one. You can not select Projects in the Projects explorer tree like it is in Eclipse PyDev. Which I see a bit of annoying.

After those actions you must get something like this:

A complete project directory. And a virtual environment directory. After opening it start's to update indexes. IDE indexes all python and actually all known files and scans their source. So code autocompletion is not available until it's finished:

5. Now that we have connected PyCharm to our Django Project... We need to setup interpreter. For PyCharm to work correctly with virtualenv and rund project in the specific environment... We simply need to specify Python executable (from our virtualenv) for this project and enable Django support.

It is all done in main menu ("PyCharm > Preferences"). We now need to select "Django support" and add make sure it's checked "enabled". Nw we need to specify manage.py and settings.py scripts. They will bee needed later for setting up run configurations.

6. After this we need to setup our virtual environment to be used with this IDE. PyCharm is quite sophisticated and does almost everything by himself. You basically need to add a new Python interpreter. PyCharm detects that it's a virtual environment and links and indexex all by himself. You need to select "Python Interpreter" in those preferences panel and press "Add New" > "Select other" in the top right corner. After that you need to point out to your "virtual-environment directory > bin > Python executable". PyCharm will scan for environment and connect it to where needed.

Select newly added interpreter and press "OK". TADAAA!!!!

AND we're almost ready to roll...

7. Creating Run configuration.

Now we have our project all set up and connected. We need to setup a proper run for our environment. Notice button "testproject" with Django logo appeared in the main buttons tab at the top. Pres a drop down menu and select "Edit configurations". Imho this part is a masterpiece here. It has nothing more than I usually need to run even sophisticated projects.

I'm specifying the "Host" to be run on local IP: 127.0.0.1 or "localhost" if you prefer so :). And i'm checking "No reload" option to manually reload server after some code changed. I prefer it like so to avoid various server hangouts and need to reboot entire Mac if something goes wrong. And now we're ready toclick "OK". And that's basically It.

9 comments:

Hey thanks! I was able to use your instructions on a Windows 7 Env. There were a few small differences, but nothing too major. I'll try to update my blog soon with some Win 7 specific instructions and will reference your link to this blog entry. Cheers!

What I really miss and one of the few things I miss from my Java days is being able to attach a debugger to a running process not started with debugger. If I had that my python love life would be complete. :)

I recently installed pycharm community edition on a linux machine. I also own a mac os x machine which I use for work. The navigate to feature doesn't work on my linux machine. The mac os x works like a breeze. Any thoughts comments would be helpful.