Setup Virtualenv for a django project

This will build on the PyEnv / Bash / OSX installation on Mac blog post i wrote some time ago in order to be able to have a set of packages in a virtual environment set only for my Django project. So, lets get started!

Environment Setup

The first thing is change into a directory that will hold my project files by running the following

mkdir myapp && cd $_

Just for the sake of versioning my application, i'm going to create a README.md and initialize my repo:

echo '# My App' >> README.md
touch .gitignore
git init
git add .
git commit -m 'Initialize repo'

Now, set a local python version within that directory:

pyenv local 3.6.2

and install virtual environment which is the last step to be able to get started setting up the project:

pip install virtualenv

Project Setup

Now it's time to get my Django app started by creating a project specific virtual environment. Bare in mind we don't want to version this directory so:

virtualenv .env
echo '.env' >> .gitignore
echo 'db.sqlite3' >> .gitignore
echo '*.pyc' >> .gitignore
source .env/bin/activate
pip install django
pip freeze > requirements.txt
django-admin.py startproject myapp .
chmod +x ./manage.py
git commit -m 'Install empty Django project'

And that should do the job! now simply run the initial migration and server:

./manage.py migrate
./manage.py runserver

and go to http://127.0.0.1:8000/ and mission accomplished!

AWS Code Commit Setup

This tutorial aims to get an IAM account setup to be able to use Code Commit. In summary I'm going to be:

  • Create IAM developer group with full permissions to access code commit.
  • Create IAM User and developer group.
  • Setting up SSH keys and configure SSH to use said keys.
  • Try a new repository

That said, lets get started.

Create IAM developer group with full permissions to access code commit.

  • Log into your account
  • Go to IAM > Groups > Create New Group
  • Create a group called Developers
  • Attach AWSCodeCommitFullAccess policy
  • Make sure you download the CSV file with all the given credentials or at least store it somewhere safe

Create IAM User and developer group.

  • Go to IAM > Users > Create New User
  • Create a user, I follow the pattern of first initial and last name (eg. jdoe)
  • Allow Programmatic access and AWS Management Console access
  • Let AWS to Autogenerated password
  • Make sure you require password reset
  • You should be able to see the Developer group you created previously make sure you assign it to the user.

Setting up SSH keys and configure SSH to use said keys.

  • Create your SSH keys by running ssh-keygen -t rsa -b 4096 -C "opposite.bracket@gmail.com"
  • name your ssh keys (eg. aws-id_rsa)
  • Go to AWS > IAM > Users
  • Select the user you created (eg jdoe)
  • Copy your public ssh key by running the following in your terminal pbcopy < ~/.ssh/aws-id_rsa in the terminal
  • Update user in AWS
  • Update your ~/.ssh/config with the following content:

Host git-codecommit.*.amazonaws.com
  AddKeysToAgent yes
  UseKeychain yes
  User {SSH key ID}
  IdentityFile ~/.ssh/aws-id_rsa

Try a new repository

  • Create a repository with your AWS CLI: aws codecommit create-repository --repository-name my-first-repo
  • Clone the repo git clone ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/my-first-repo
  • Change into repo cd my-first-repo
  • Create a README.md echo '#Provisioning' > README.md
  • commit git commit -am "make first commit"
  • push git push origin master

PROFIT!!!

TextMate + RVM

I'm testing out ruby using TextMate and seems like it's not picking up my RVM environment SO, here's what i found out.

After you have a functioning rvm setup, all you need to do is as follows:

  • Open your terminal (I use iTerm2) and run which rvm-auto-ruby which currently gives me /Users/psyco-mantis/.rvm/bin/rvm-auto-ruby.
  • Go to TextMate > Preferences > Variables and add a variable called TM_RUBY and assign the path given in the previous step (Mine was /Users/psyco-mantis/.rvm/bin/rvm-auto-ruby).

Profit!, now if you create a file with puts RUBY_VERSION as the content, you'll see your RVM's ruby version.

GitHub – opposite-bracket

Jesus Rodriguez

Vancouver, BC