Python Development using Laravel Homestead

How to setup Python development environment on your Homestead virtual machine?

1. Update your Homestead.yaml
Besides mapping your regular “projects” folder, you need to map our python virtual environments.
Add the following to your folders inside Homestead.yaml file:

folders:
    - map: ~/homesteadvirtualenvs
      to: /home/vagrant/.virtualenvs

2. Update provisioning script by installing python virtualenv on each Homestead VM boot
Add the following to your after.sh file:

	
	#python virtual environments
	sudo pip install virtualenvwrapper

3. Update your .bashrc so that you can access virtualenv on ssh into Homestead:
Add the following to your aliases file:

source /usr/local/bin/virtualenvwrapper.sh

That’s it! Now you can develop any python projects with seperate virtualenv using Homestead

For example:

1. ssh into your Homestead and enter the following:

mkvirtualenv  nameOfEnvironment

You can create enviroment that will work with python3 by default by typing:

mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

2. You can type “workon nameOfEnvironment” to enter the enviroment
You can exit current enviroment at any time by typing: “deactivate”

3. Install Django within our environment (workon nameOfEnvironment) by typing:

pip install django


This should give you the latest version of Django which will be installed in your virtualenv area. You can confirm this by doing:

which django-admin.py

4.  Let’s create new Django project inside our “projects” folder:

django-admin.py startproject django_project

5. Let’s test it! Type this to boot up the django server:

cd django_project
python manage.py runserver 0.0.0.0:8000

6. Type your Homestead ip address in the browser with port 8000 and you should see Django welcome page.

P.S. You can find/setup your Homestead IP address inside your Homestead.yaml

comments powered by Disqus