Skip to main content

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.


$ cd developer
$ mkdir testproject
$ cd testproject
$ virtualenv testvenv --no-site-packages
New python executable in testvenv/bin/python
Installing setuptools............done.
Installing pip...............done.
$ source testvenv/bin/activate
(testvenv)username:testproject leopard$ 
$ pip install django
Downloading/unpacking django
  Downloading Django-1.3.1.tar.gz (6.5Mb):  53%  3.5Mb
...
$ django-admin.py startproject mysite
$ cd mysite
$ ls -l
total 32
-rw-r--r--  1 leopard  staff     0 Jan 10 11:13 __init__.py
-rw-r--r--  1 leopard  staff   503 Jan 10 11:13 manage.py
-rw-r--r--  1 leopard  staff  5031 Jan 10 11:13 settings.py
-rw-r--r--  1 leopard  staff   565 Jan 10 11:13 urls.py
$


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.
You can now press "Run" button and check your browser showing 127.0.0.1:8000 shows Django test page. Or go directly to testing delicious debugger by running "Debug" button. 

Comments are welcome!

Comments

  1. I really Like your page!, keep the great work :)

    ReplyDelete
  2. 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!

    ReplyDelete
    Replies
    1. Lol, I switched over to a Macbook, and ended up here a second time. So, uh, thanks again!

      Delete
  3. That connected some dots for me as well, thanks Iurii!

    ReplyDelete
  4. 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. :)

    ReplyDelete
  5. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Pretty git Log

SO you dislike git log output in console like me and do not use it... Because it looks like so: How about this one? It's quite easy... Just type: git log - - graph - - pretty = format : '%Cred%h%Creset -%C ( yellow ) %d%Creset %s %Cgreen ( %cr) %C ( bold blue ) <%an>%Creset' - - abbrev - commit - - It may be hard to enter such an easy command every time. Let's make an alias instead... Copypaste this to your terminal: git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --" And use simple command to see this pretty log instead: git lg Now in case you want to see lines that changed use: git lg - p In order for this command to work remove  the -- from the end of the alias. May the code be with you! NOTE: this article is a rewritten copy of  http://coderwall.com/p/euwpig?i=3&p=1&t=git   and have b

Django: Resetting Passwords (with internal tools)

I have had a task recently. It was about adding a forms/mechanism for resetting a password in our Django based project. We have had our own registration system ongoing... It's a corporate sector project. So you can not go and register yourself. Admins (probably via LDAP sync) will register your email/login in system. So you have to go there and only set yourself a password. For security reasons you can not register. One word. First I've tried to find standart decision. From reviewed by me were: django-registration and django password-reset . These are nice tools to install and give it a go. But I've needed a more complex decision. And the idea was that own bicycle is always better. So I've thought of django admin and that it has all the things you need to do this yourself in no time. (Actually it's django.contrib.auth part of django, but used out of the box in Admin UI) You can find views you need for this in there. they are: password_reset password_reset_

Time Capsule for $25

The real article name might be something like:  Configuring Raspbery Pi to serve like a Time Capsule with Netatalk 3.0 for Mountain Lion.  But it's too long ;) Here I will describe the process of using Raspberry Pi like a Time Machine in my network. To be able to backup your MAC's remotely (Like it would be NAS of some kind). It assumes you have a Raspberry Pi and have installed a Raspbian there and have a ssh connection, or somehow having access to it's console. Refer to my previous article for details . Now that we have a Pi that is ready for action let's animate it. So to make it suit you as a Time Capsule (NAS) for your MAC's you need to do those basic steps: - connect and configure USB hard drive(s) - install support of HFS+ filesystem to be able to use MAC's native filesystem - make mount (auto-mount on boot) of your hard drive - install Avahi and Netatalk demons - configure Netatalk daemon to make it all serve as a Time Machine - configure