It’s been really long time since I wrote a blog post here.

Since past few months / years I’ve been doing lots of JavaScript programming and less of Python programming. Oh yah, I’ve purchased Raspberry Pi recently, now I got a chance to work on my own Python projects. Recently did whois-home, paadu-gajala and few more coming soon.

My New Toy. Just Arrived 🙂 A photo posted by Abhinay Omkar (@abhiomkar) on Oct 16, 2015 at 9:50am PDT

npm is very popular package manager for JavaScript there are few things I feel pip should learn from npm. For example, “npm init”, “npm –save install <package_name>” & “npm install” commands. I’m so used to this behaviour, thought pip also should have similar features. virtualenv concept in python is confusing sometimes especially for beginners and there are various ways to achieve it, it is not streamlined.

I made a quick hack on pip (called pip-save) which might be useful for developers who are coming from JavaScript world. Here you go..

Creating virtual environment

$ pip init

pip doesn’t have ‘init’ command by default. But, wouldn’t it be nice if it creates a new virtual environment for us?

mkvirtualenv --python=`which python` $(basename $PWD) && \ touch .pip-init && \ workon $(basename $PWD)

Bringing –save option to pip

There is no –save option for pip by default. This hack make the –save option work by automatically updating requirements.txt file in the current project folder.

$ pip --save install request

which does

$PIP ${PIP_ARGS[@]} && $PIP freeze > requirements.txt

Install project dependencies

All the project dependencies are stored in requirements.txt file. We can use ‘pip install -r requirements.txt’ or just the ‘pip install’ by this hack which does the same.

$ pip install

Changing the default behaviour of cd

‘pip init’ automatically creates a file ‘.pip-init’ in project folder which can be used to identify if the project has a virtual environment. By this hack, you can automatically switch between virtual environments by just changing the directory.

Extending builtin cd:

cd () { builtin cd "$@" if [ -f .pip3-init ] ; then workon $(basename $PWD) && \ export PIP3=$WORKON_HOME/$(basename $PWD)/bin/pip3 elif [ -f .pip-init ] ; then workon $(basename $PWD) && \ export PIP=$WORKON_HOME/$(basename $PWD)/bin/pip fi }

You can install this pip hack using below command

source <(curl -sSLk https://raw.githubusercontent.com/abhiomkar/pip-save/master/install)

This is dependent on virtualenvwrapper. Please install ‘virtualenvwrapper’ before installing pip-save.

sudo pip install virtualenvwrapper # or sudo pip3 install virtualenvwrapper

Here is the source code of this Hack (Fork it on GitHub):

https://github.com/abhiomkar/pip-save

Hope you find this hack useful and easy to use. If you’ve any suggestions or improvements please feel free to comment below.