Boost productivity with virtualenv

"A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable." - The Hitchhiker's Guide to Python

In this blog post I'll explain about virtualenv, venv, and how I use both.

<!-- more -->

virtualenv

virtualenv gives the user the ability to use virtual environments for their python 2.x and 3.x projects. It completely isolates your projects and their dependencies from each other which makes IT AWESOME and really simple to use.

venv

TL;DR virtualenv re-implemented inside python 3.6

"The venv module provides support for creating lightweight virtual environments with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories." - docs.python.org

how I use it

Most of the time, I use direnv to handle my virtualenvs. It works flawlessly and completely transparent.

Otherwise, I use my own custom made functions to create venvs (I not a fan of virtualenvwrapper):

# source the virtualenv in the current directory
# the first argument is the name of the virtualenv
# if it's not supplied, it'll try to use .venv directory
# or a sub-directory with the same name as this directory.
function venvon() {

venv="$1"
if [ -z "$venv" ]; then
if [ -d "$(pwd)/.venv" ]; then
venv=".venv"
else
venv=$(basename $(pwd))
fi
fi

if ! source "$(pwd)/$venv/bin/activate" &> /dev/null; then
echo "./$venv dir is missing"
return 1
fi
}

function __mkvenv() {
version="$1"
venvtype="$2"
venv="$3"

if [ -z "$venv" ]; then
venv=".venv"
fi

if [ -d "$(pwd)/$venv" ]; then
echo "venv already exist"
return 1
fi

python$version -m $venvtype $venv
venvon $venv
}

# create a python2 virtualenv
function mkvenv2() {
__mkvenv "2" "virtualenv" "$1"
}

# create a python3 virtualenv
function mkvenv3() {
__mkvenv "3" "venv" "$1"
}

*args and **kwargs

*args and **kwargs are a big deal in the python world, and to me, a bit magical:
On one hand, they allow one to inject arbitrary arguments to functions, which is extremely powerful.

On the other hand, if abused, can make your code extremely complex and unreadable.

I suggest reading args and kwargs to understand how they work in depth.

the march towards go

Have you heard about the go programming language? I just read The March Towards Go and got extremely interested.

Here's what go's creators, Rob Pike & Ken Thomson, said about it:

Go attempts to combine the development speed of working in a dynamic language like Python with the performance and safety of a compiled language like C or C++. In our experiments with Go to date, typical builds feel instantaneous; even large binaries compile in just a few seconds. And the compiled code runs close to the speed of C. Go is designed to let you move fast.

We’re hoping Go turns out to be a great language for systems programming with support for multi-processing and a fresh and lightweight take on object-oriented design, with some cool features like true closures and reflection.

python & the global interpreter lock

I'm an internals guy. I read CLR via C# to understand the inner workings of C# (and I highly recommend it!)

After learning python's syntax, I started digging into the fun stuff. I just found out the python has a global interpreter lock (GIL) that basically makes python a single threaded language (not really).

In upcoming posts I'll share links that explain how to bypass/handle this limitation (hint: multiprocessing). But for now, take a look at these articles:

Hello, World!

Dear diary, I decided to ditch you and open up a blog!

Why?

Mainly because my memory is horrible and I need a place to put my thoughts. I hope somebody will find it useful!

what now?

Now I'll start writing things :)