As a side project, I started fooling around a bit with python. Now as usual, first i started to look around a bit, see what’s out there, what’s the best practices, best IDE and all that.
My first idea was to install python, mysql and django side by side with everything else on my windows machine, but the more i thought about it, the messier it got. So i went with option no.2 and that would be a VM.
Now, my linux knowledge is pretty bad, and i really didn’t feel like setting up and fiddling with another OS with GUI, especially since i didn’t really need all that fluff, i just needed my linux web server to run my python scripts and serve my database.
Now, if i could only work on my project on my windows machine and have it run on linux box, with debugging and everything.
Enter Vagrant.
Vagrant is a pretty cool tool which was made by 2 guys who had pretty much the same idea. The killer features of vagrant are a) it’s really easy to download a VM b) it’s really easy to set it up with recipes (same enviroment every time you start it up) c) it synchronizes your project folder with folder on your computer.
The other cool tool you will need for development is PyCharm. I simply love Resharper and i was more than pleased to find out that the same company is developing one of the best tools for Python development. I grabbed the licence on JetBrains’ Christmas sale, but if you’re serious about python development it’s well worth the price. Pycharm has a Vagrant plugin and it supports remote debugging and running which is the killer feature of the product.
The goal of our tutorial will be to have a django dev environment with minimal installation on our dev computer. So let’s start.
Ofcourse, yo do need a few tools on your host and that would be:
VirtualBox https://www.virtualbox.org/wiki/Downloads
Vagrant http://www.vagrantup.com/
PyCharm http://www.jetbrains.com/pycharm/
A git client of choice (I installed github’s windows client)
which we will use to install chef solo cookbooks.
Note one thing, for your windows box you don’t need any sort of python installation:)
Now install all of these, pretty much you should go with defaults or sane options (i installed those previously so i don’t remember if there was any problem but there shouldn’t be any at all).
Now create your project folder through file system, open up pycharm and choose open directory. Point to your newly created directory.
Then follow this tutorial but read the comments after the link for potential problems:
http://www.jetbrains.com/pycharm/quickstart/configuring_for_vm.html
1. There are few errors with this piece. For some reason name of the machine in the vagrantfile contains “(virtualbox)” sufix, open up VagrantFile and delete that before uping vagrant. the line should be config.vm.box = “lucid32″ after correction.
2. After “fill from vagrant config” step you will probably get “Private key not found” exception. Download a PuttGen tool from following link:
http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe
start it File->”load Key” and select the key c/users/you/.vagrant.d/insecure_private_key (have All files filter selected)
Then go file->save private key, and save it in the location you prefer. Then point to it in the pycharm vagrant dialog box instead of that unsecured_private_key.
So we did the basic setup, we have a project directory, we have an interpreter (2.6.5), we did some consoling and stuff, but we now need to have it all do django.
First thing first, we need to set up that VM with proper python version and tools, and we need to have it all in virtual environment. We will use cookbooks from chef solo to do all of that.
There’s a line in your VagrantFile Which starts with “#Enable provisioning with chef solo”. Replace that block with the following block
# Enable provisioning with chef solo, specifying a cookbooks path, roles # path, and data_bags path (all relative to this Vagrantfile), and adding # some recipes and/or roles. # config.vm.provision :chef_solo do |chef| chef.json = { postgresql: { password: { postgres: 'yourpassword' }, pg_hba: [ {type: 'local', db: 'all', user: 'all', addr: nil, method: 'trust'}, {type: 'host', db: 'all', user: 'all', addr: '127.0.0.1/32', method: 'trust'}, {type: 'host', db: 'all', user: 'all', addr: '::1/128', method: 'trust'} ] }, python:{install_method:'source', version:'2.7.3', checksum: 'c57477edd6d18bd9eeca2f21add73919'} } chef.cookbooks_path = "../chef-recipes/cookbooks" chef.add_recipe "apt" chef.add_recipe "apache2::mod_wsgi" chef.add_recipe "build-essential" chef.add_recipe "git" chef.add_recipe "vim" chef.add_recipe "openssl" chef.add_recipe "postgresql" chef.add_recipe "postgresql::server" chef.add_recipe "yum" chef.add_recipe "python" end
This code will install python, postgresql, vim, git, and all of the goodies you need for that machine to be django ready. In the first part (chef.json) there are also setup details for your postgresql (username and password), and installation details for python 2.7.3.
If you click Vagrant reload, you’ll get a bunch of errors. That’s because you don’t have any of those cookbooks installed. You will notice a cookbook path line in the former code snippet, so you need to put your cookbooks in that directory. It’s easiest by using git commands in that directory (this is done on your HOST):
git clone git://github.com/opscode-cookbooks/apache2.git git clone git://github.com/opscode-cookbooks/apt.git git clone git://github.com/opscode-cookbooks/build-essential.git git clone git://github.com/opscode-cookbooks/git.git git clone git://github.com/opscode-cookbooks/vim.git git clone git://github.com/opscode-cookbooks/openssl.git git clone git://github.com/opscode-cookbooks/postgresql.git git clone git://github.com/opscode-cookbooks/yum.git git clone git://github.com/opscode-cookbooks/python.git
After cloning all of that, just go tools-vagrant-reload and wait for everything to get installed.
And we’re almost peachy:)
Next thing, we’ll set up a virtualenv on that machine, best to do it in the home directory
open ssh console and do the following
virtualenv ~/py2.7 -p /usr/local/bin/python2.7 source ~/py2.7/bin/activate pip install Django pip install psycopg2
I’ve put it in a separate .sh file for running later, you can too but i think you can handle it by yourself (hint: put it in you project folder, it will automatically sync with your vagrant folder on your machine).
OK, so we have a virtualenv set up on the remote machine, now we have to reference it on our project
In pycharm go to project settings, python interpreters and click plus, remote, fill from vagrant config, and on the python interperter path put
/home/vagrant/py2.7/bin/python
Next thing, go to console again and execute the following command
django-admin.py startproject DjangoTest /vagrant
Where DjangoTest would be the name of your project. You will see new files in your project explorer in pycharm.
Go to django support in settings, select root, settings file (settings.py) and manage.py should be selected automatically.
Save and then click the arrow for run/debug configuration. Click plus and add django server, edit the name(put DjangoTest for instance), in Host put 0.0.0.0, and check run browser
and put in the text box 8000 instead of 8001. Save.
When you try to run it you will probably get a 404 or something like that. There’s one more thing you have to do. In your VagrantFile, find the following
# config.vm.network :forwarded_port, guest: 80, host: 8080
Remove hash and replace it with
config.vm.network :forwarded_port, guest: 8000, host: 8001
(you should probably also do some setting up database paramaters in settings.py file, use the posgresql_psycopg2 module).
Reload the vagrant and you should be all set and see the Django welcome message.
Happy coding!
P.S. One more quick note, if you get your code underlined red (unresolver reference) in Pycharm, go to settings, project interpreters, configure the selected, open tab path and click refresh. That should do the trick:)
Steve Jun 28 , 2013 at 2:17 am /
Thanks for the post, getting vagrant running on windows was easier than I thought =)
And yeah definitely a good idea compared to trying to run django on Windows, I’ve tried that before and it works but will cause many headaches..