Archive

Archive for the ‘python’ Category

using virtualenv (Part 1)

September 21, 2013 Leave a comment

Here I won’t explain what virtualenv is. There are lots of tutorials on this topic (see the “virtualenv” subsection here for instance, or this SO thread).

This post is about the basic usage of virtualenv.

Step 0 (20131029)
Before doing anything, create the file ~/.pip/pip.conf and edit it:

[install]
download-cache = ~/.pip/download_cache

This way each version is downloaded only once.

Update (20140807): Maybe using a cache is not that good idea. It happened to me that a new version came out of a package that I wanted to install but because of the cache, it installed an older version! I didn’t understand why I still had an out-of-date version. So I removed the ~/.pip folder entirely. I don’t recommend using a cache.

Installation
Install virtualenv:

sudo pip install virtualenv

Trying out
Create a directory where you want to store your project:

$ cd ~/python
$ mkdir mystuff
$ cd mystuff

Let’s create here a virtual environment:

$ virtualenv venv

You will get a subdirectory called “venv“, which contains a clean Python virtual environment. Activate it with the following command:

$ . venv/bin/activate

Notice that the prompt changes: now it includes the name of the virtual environment. If you call the command “python” or “pip“, the binaries in the virtual environment will be called.

Running example:

jabba@jabba-uplink:~$ cd python
jabba@jabba-uplink:~/python$ mkdir mystuff
jabba@jabba-uplink:~/python$ cd mystuff/
jabba@jabba-uplink:~/python/mystuff$ ls -al
total 16
drwxrwxr-x   2 jabba jabba  4096 Sep 21 15:58 .
drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 ..
jabba@jabba-uplink:~/python/mystuff$ virtualenv venv
...
jabba@jabba-uplink:~/python/mystuff$ ls -al
total 20
drwxrwxr-x   3 jabba jabba  4096 Sep 21 15:58 .
drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 ..
drwxrwxr-x   6 jabba jabba  4096 Sep 21 15:58 venv
jabba@jabba-uplink:~/Dropbox/python/mystuff$ . venv/bin/activate
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python
/home/jabba/Dropbox/python/mystuff/venv/bin/python                                                     
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which pip
/home/jabba/Dropbox/python/mystuff/venv/bin/pip                                                               

To stop using a virtual environment, use the command “deactivate“.

Example
Say I have the package “requests” globally installed (with “sudo pip install requests“). When I launch the python shell, this module can be imported.

However, when you activate a clean virtual environment, globally installed 3rd party packages are not available. They must be installed with the virtual environment’s “pip” command, and they will be installed inside the “venv” directory. This is the real advantage of virtualenv: virtual environments are completely isolated from each other and you can install different package versions, there won’t be any conflict.

Running example:

jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python
/usr/bin/python
jabba@jabba-uplink:~/Dropbox/python/mystuff$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Persistent session history and tab completion are enabled.
>>> import requests
>>> 
jabba@jabba-uplink:~/Dropbox/python/mystuff$ . venv/bin/activate
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python
/home/jabba/Dropbox/python/mystuff/venv/bin/python
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Persistent session history and tab completion are enabled.
>>> import requests
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named requests
>>> 
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which pip
/home/jabba/Dropbox/python/mystuff/venv/bin/pip
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ pip install requests
Downloading/unpacking requests
  Downloading requests-1.2.3.tar.gz (348kB): 348kB downloaded
  Running setup.py egg_info for package requests
    
Installing collected packages: requests
  Running setup.py install for requests
    
Successfully installed requests
Cleaning up...
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Persistent session history and tab completion are enabled.
>>> import requests
>>> 
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$

Where to put the project files
Store your project files (in this example) in the “~/python/mystuff” directory. If the virtual environment is activated, then the modules installed in the virtual environment are available for your project:

(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ ls -al
total 24
drwxrwxr-x   3 jabba jabba  4096 Sep 21 16:16 .
drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 ..
-rwxrw-r--   1 jabba jabba    53 Sep 21 16:16 hello.py
drwxrwxr-x   6 jabba jabba  4096 Sep 21 16:13 venv
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ cat hello.py 
#!/usr/bin/env python

import requests
print "hello"
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ ./hello.py 
hello

We installed “requests” in the previous step, and it’s nicely available for the project. If you uninstall requests from the virtual environment with “pip uninstall requests” and try to run hello.py, you will get an import error message.

In Part 2 we will see how to colorize the prompt and how to activate a virtual environment easily.

Categories: python Tags:

Python from command line

September 18, 2013 Leave a comment

Problem
You want to calculate something with Python quickly, from the command line. You might even want to use Python in a bash script to produce some result.

Solution

$ python -c "print(2*3)"
6

Storing the result in a variable:

$ X=`python -c "print(2*3)"`
$ echo $X
6

Thanks to Tajti A. for the tip.

Update (20170803)
You can also pass a bash variable to the embedded Python:

VAL="cat dog cat"
NEW=`python3 -c "print('$VAL'.replace('dog', 'wolf'))"`
echo $NEW

Output: “cat wolf cat”.

Categories: python Tags: , , ,

Anaconda Python distribution

September 15, 2013 Leave a comment

See https://store.continuum.io/cshop/anaconda/.

Completely free enterprise-ready Python distribution for large-scale data processing, predictive analytics, and scientific computing.

It has the advantage that it installs everything in a specified directory. Say you need to use a machine where you don’t have root permission and/or this machine has an old Python version. Just install Anaconda and use the latest Python version it is shipped with.

Update (20160208)
I had Anaconda 2.3 and 2.5 came out a few days ago. How to upgrade to the new distribution?

$ conda update --all
$ conda install anaconda=2.5
$ conda update --all
Categories: python Tags:

Python + Excel

September 13, 2013 Leave a comment

Links

  • DataNitro (cheapest licence is $500, however they have a 30-day free trial)
  • pyxll

Both projects have a short video that demonstrates the basic usage.

Categories: python Tags:

Python APIs

September 9, 2013 Leave a comment
Categories: python Tags: ,

How to ignore an exception — the elegant way

September 7, 2013 1 comment

This idea was presented by Raymond Hettinger at PyCon US 2013. He is talking about it at 43:30: http://www.youtube.com/watch?v=OSGv2VnC0go.

Problem
You want to ignore an exception.

Solution 1: the classical way
Say you want to delete a file but it’s not sure it exists.

try:
    os.unlink('somefile.txt')
except OSError:
    pass

That is, if the exception occurs, we do nothing.

Solution 2: the elegant way

with ignored(OSError):
    os.unlink('somefile.txt')

Its source code in Python 2.7:

from contextlib import contextmanager

@contextmanager
def ignored(*exceptions):
    try:
        yield
    except exceptions:
        pass

This is part of Python 3.4, thus in Python 3.4 all you need is this line:

from contextlib import ignored

See the docs here.

Standard modules as standalone programs

September 5, 2013 1 comment

The following tips are from this presentation.

Encode a file in base64:

python -m base64 some_file

Open a URL in the browser:

python -m webbrowser http://google.com

List of functions in a file:

python -m pyclbr some_file.py
python -m pyclbr module_name

See the rest of the stdlib modules in /usr/lib/python/.

Categories: python Tags: ,

check indentation

September 5, 2013 Leave a comment
python -m tabnanny file.py
Categories: python Tags:

PyPy is awesome

August 15, 2013 Leave a comment

PyPy is a fast, compliant alternative implementation of the Python language (2.7.3 and 3.2.3).

Installation
Visit the download page and get the 32-bit or 64-bit archive, depending on your architecture. I extracted it in the /opt directory and put a symbolic link on it:

jabba@jabba-uplink:/opt$ ls -al | grep pypy
lrwxrwxrwx  1 jabba jabba     8 Aug 15 12:32 pypy -> pypy-2.1
drwxr-xr-x  7 jabba jabba  4096 Jul 31 12:59 pypy-2.1

If you install a newer version, just update the symbolic link.

Put another symbolic link to /usr/bin called pypy that points to /opt/pypy/bin/pypy:

root@jabba-uplink:~# ls -al /usr/bin/pypy
lrwxrwxrwx 1 root root 18 Aug 15 16:39 /usr/bin/pypy -> /opt/pypy/bin/pypy

Try it:

jabba@jabba-uplink:~$ pypy
Python 2.7.3 (480845e6b1dd, Jul 31 2013, 09:57:07)
[PyPy 2.1.0 with GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>

Install pip for pypy
We also want to use 3rd party libraries with pypy thus we need pip.

$ wget http://python-distribute.org/distribute_setup.py
$ wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ pypy distribute_setup.py
$ pypy get-pip.py

Add the following alias to your ~/.bashrc:

alias pip_pypy='/opt/pypy/bin/pip'

Open a new terminal and you are ready to install 3rd party libraries. Example:

$ pip_pypy install futures    # import concurrent.futures

Tip from here. Getting started here.

Categories: python Tags: ,
Design a site like this with WordPress.com
Get started