Pages

June 1, 2014

Python development environment setup in Ubuntu 14.04 LTS

After 100 days of using the Antergos Gnome 3 distribution as my main OS, I've decided to distro-hop again and try out the new Ubuntu 14.04 LTS (which I just recently installed).

A little bit over a month ago, I landed a job as a (probationary) web developer at a local web development shop. My tasks there consists mostly of Python/Django development. And so I can now say that I am professionally doing Python/Django development (yey!). I learned from my mentors at work on how to manage my development environment efficiently. I did know about virtualenv prior to landing that job, but I learned about virtualenvwrapper and how it augments usage of Python virtual environments on the job.

This post are just my notes on how I setup virtualenvwrapper on my Ubuntu 14.04 LTS box.

'pip' - alternative Python package installer

In Ubuntu, I installed the pip package installer globally. This can be done by the following command:

    $ sudo apt-get install python-pip

If Python 3 is what you're using, then there is the python3-pip version of the package installer, so install that instead. Basically, this is the only package I install globally. I like keeping my system relatively clean of globally installed packages (but that's just me). The following Python packages were installed locally.

The 'virtualenvwrapper' module

Based on this module's site, to install this module to your local user directory, just issue the following command:

    $ pip install --install-option="--user" virtualenvwrapper

Sure enough, this installed virtualenvwrapper to ~/.local but the other two modules, virtualenv and stevedore, which are dependencies of virtualenvwrapper are being installed to the /usr/local/lib/python2.7/dist-packages/ directory. And so I am getting the [Error 13] Permission denied error.

What I did to get these 2 modules installed was to download the .tar.gz from PyPI - the Python Package Index, unzip them and install them like so:

    $ cd ~/path/to/extracted/folder/of/downloaded/module
    $ python setup.py install --user

I did this for both packages and now they are installed to my ~/.local directory. You can check if it successfully installed by checking the ~/.local/lib/python2.7/site-packages directory.

Additional '.bashrc' configurations needed

There needs to be additional entries the the .bashrc file. Since I installed the modules to my local user-space, then I need to also add that to the PATH system variable. Here's what I added in my .bashrc file.

    # Add ~/.local/bin to PATH
    if
        [ -d "$HOME/.local/bin" ]; then
            PATH="$HOME/.local/bin:$PATH"
    fi

    # Add virtualenvwrapper variables
    export WORKON_HOME=$HOME/Envs
    export PROJECT_HOME=$HOME/Projects
    source virtualenvwrapper.sh

Save the file and do a source ~/.bashrc to activate the changes. To test if it worked, type the command workon and press Enter. If you don't get a "command not found" error, then you're good to go. Start creating virtual environments with ease and develop away!

Conclusion

This may somewhat be a rudimentary environment setup but this is how we do it at work and it works for us. Also, I don't know why the pip install --install-option="--user" virtualenvwrapper didn't carry over to the other 2 modules. Perhaps it's the distro, I'd have to try it on other distros to say for sure (I can't recall if I had this issue in Arch).

Thanks for reading, and I hope you learned something new from this post.

1 comment:

  1. Hi, Eric, thank you for this post. Regarding the issue with pip and virtualenvwrapper being install locally, I just issued the option "--user" to "pip install", and it worked perfectly.

    ReplyDelete