Categories
Python

Mac OSX, homebrew & Python: Creating Virtual Environments

Assumptions:
— The system is running Mac OSX
— You have installed Python using homebrew
— You have “pip” installed. (Try “easy_install pip” if not installed)

Install Virtualenvwrapper:

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

It’s cooler in the sense that the virtualenvs are generated in a seperate directory than the project directory. It’s an excellent improvement over virtualenv tool. It also provides some handy commands to generate, remove and switch environments.

Install it with this command:

After it is installed, we should create the dedicated directory for the virtualenvs:

Now add these lines to your bash profile (~/.bash_profile):

Creating an environment:

Removing an environment:

Switching to an environment:
Use the workon command:

Using hooks:
You can use predefined scripts to customize the behavior when an environment is activated. For example, to change directory to “~/codes/test” when “test” environment is activated, we shall edit add this line to $VIRTUAL_ENV/test/postactivate:

Similarly, we can use the postdeactivate script to define a behavior when the script is deactivated.

Using pip:
While using pip, you can use an extra argument to define which environment you want the package to be installed:

Cool, eh?

Categories
Python

Django management command: cleanup .pyc files

While distributing a django app, the pyc files could become very annoying. So I wrote this django management command to quickly clean up the *.pyc files from all the directories. The file should be placed under “commands” directory under the “management” directory of an application. So basically the file structure should be like: < appname >/management/commands/rmpyc.py. Note that both the management and commands directories must be python packages, that is they should both have their own __init__.py.

Here is the code:

Usage:
When you have put the application in the “INSTALLED_APPS”, you can now use commands like this to cleanup the python bytecode files:

Categories
Mac Python

Setting up Python, MySQL Module and Django on Mac OS X Lion

I setup Python and Django using homebrew the easy way. I am going to detail the process now.

Install Python

The command should first install any dependencies for Python and then install the latest version from source code.

Adding Python to PATH

We need to edit our ~/.bash_profile to add “/usr/local/share/python” to the path variable. I added the following line to my bash profile:

I reloaded the profile by issuing:

This makes the python tools and toys accessible from my terminal.

Install MySQL Module

It’s recommended that you already have “mysql” installed from brew to build the mysql module. I already had it installed so it was a painless installation for me. I just issued the command:

Install Django
I am going to keep using the PIP awesomeness! Let’s hit:

It will install django with any dependencies. You can now invoke the “django-admin.py” command from your terminal. However, I prefer the command be “django-admin”, instead of “django-admin.py”. So I created the following alias in my ~/.bash_profile:

Isn’t it simple? 😀