Archive

Archive for the ‘ubuntu’ Category

compile lxml on Ubuntu 16.04

August 4, 2016 Leave a comment

Problem
lxml doesn’t want to compile on Ubuntu 16.04.

Solution

$ sudo apt install libxml2-dev libxslt1-dev python-dev zlib1g-dev

I was getting the error “/usr/bin/ld: cannot find -lz“. It turned out that the package zlib1g-dev was the cure…

Note that this is for Python 2. For Python 3 you might need to install the package python3-dev.

Categories: python, ubuntu Tags: ,

installing a Flask webapp on a Digital Ocean Ubuntu 16.04 box using Systemd

August 4, 2016 Leave a comment

I’ve updated my Digital Ocean Flask notes on GitHub. Now it includes information about installing a Flask webapp on a Digital Ocean Ubuntu 16.04 box using Systemd.

Categories: flask, python, ubuntu Tags: ,

“import tkinter” fails

April 27, 2016 Leave a comment

Problem
Under Manjaro I wanted to use the turtle module but I got this error:

File "square.py", line 1, in <module>
    import turtle
  File "/usr/lib/python2.7/lib-tk/turtle.py", line 107, in <module>
    import Tkinter as TK
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libtk8.6.so: cannot open shared object file: No such file or directory

Solution

sudo pacman -S tk

Ubuntu 14.04
I had the same problem under Ubuntu when I tried to use matplotlib on a remote server.

sudo apt install python3-tk
Categories: python, ubuntu Tags: , , ,

make bpython use Python 3

October 14, 2014 1 comment

Problem
On my Linux box the default Python is version 2.7. I installed bpython system-wide, thus when I launch bpython, it uses Python 2.7.

How could I have another bpython that uses Python 3? I would like to have both though. Sometimes I need Python 2, sometimes Python 3, and I would like to use the bpython shell with them.

Solution
Simply install bpython in a Python 3 virtual environment. That is, create a Python 3 environment, and install bpython in it with pip. This bpython installation will use Python 3. Then use the absolute path of this bpython executable. And no, you don’t have to activate the virt. env. each time you want to use this bpython.

In a previous post I showed how to patch virtualenvwrapper to make it suitable for creating not only Python 2 but Python 3 environments too. Using this patched virtualenvwrapper, the process looked like this on my machine:

$ cd
$ mkdir bpython_python3
$ cd bpython_python3
$ mkvirtualenv bpython_python3  # creates a Python 3 virt. env.
(bpython_python3)$ pip install bpython
(bpython_python3)$ which bpython
/home/jabba/.virtualenvs/bpython_python3/bin/bpython  # sample; will be different in your case
(bpython_python3)$ deactivate
$

I suggest adding the following aliases to your $HOME/.bashrc:

alias bpy='bpython'
alias bpy3='/home/jabba/.virtualenvs/bpython_python3/bin/bpython'

Of course, you need to customize the path for bpy3.

Now “bpy” launches bpython with Python 2, while “bpy3” starts bpython with Python 3.

Categories: ubuntu Tags: ,

Getting started with Django on a DigitalOcean VPS

Django is a high-level Python framework for developing web applications rapidly. DigitalOcean’s Django One-Click app quickly deploys a preconfigured development environment to your VPS employing Django, Nginx, Gunicorn, and Postgres.

More info here.

installing pgmagick

March 4, 2014 Leave a comment

pgmagick is yet another boost.python based wrapper for GraphicsMagick.

GraphicsMagick is the swiss army knife of image processing. …it provides a robust and efficient collection of tools and libraries which support reading, writing, and manipulating an image in over 88 major formats including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.” (source)

Here I found an interesting blog post on how to remove image backgrounds with a Python script (comments on it here). The script uses the pgmagick library.

Problem
How to install pgmagick and GraphicsMagick? That is, the following line shouldn’t drop any error :)

>>> import pgmagick
>>>

Solution
It may not be an optimal solution because it installed on my machine LOTS OF extra packages… However, it worked for me.

$ sudo add-apt-repository ppa:dhor/myway
$ sudo apt-get update
$ sudo apt-get install graphicsmagick

$ sudo apt-get install libmagick++-dev
$ sudo apt-get install libboost-python-dev
$ sudo pip install pgmagick -U

Usage example
scale example (copied from here):

>>> from pgmagick import Image, FilterTypes
>>> im = Image('input.jpg')
>>> im.quality(100)
>>> im.filterType(FilterTypes.SincFilter)
>>> im.scale('100x100')
>>> im.sharpen(1.0)
>>> im.write('output.jpg')

Links

Categories: python, ubuntu Tags: ,

Launch just one instance of a Qt application

January 10, 2013 Leave a comment

Problem
I have a Qt application that I only want to run in one instance. If I forget that it’s launched and I start it again, I want the system to put the focus on the already running instance.

Solution
For more details, see this post where the used Unix commands are explained.

#!/usr/bin/env python

"""
Launch just one instance of an application.

If the application is running, the focus is put on its window.

Requires the xdotool package (sudo apt-get install xdotool).
Tested under Ubuntu GNU/Linux 12.10.

Author: Laszlo Szathmary (jabba.laci@gmail.com), 2013
"""

__author__ = 'Jabba Laci'
__appname__ = "The One and Only"
__version__ = "0.1"

import os
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from subprocess import Popen, PIPE, STDOUT
import shlex


def get_simple_cmd_output(cmd, stderr=STDOUT):
    """Execute a simple external command and get its output.

    The command contains no pipes. Error messages are
    redirected to the standard output by default.
    """
    args = shlex.split(cmd)
    return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0]


class OneDialog(QDialog):
    def __init__(self, parent=None):
        super(OneDialog, self).__init__(parent)
        self.setWindowTitle("{} {}".format(__appname__, __version__))
        self.resize(300,150)

####################

if __name__ == "__main__":
    wid = get_simple_cmd_output('xdotool search --name "{n}"'.format(n=__appname__)).strip()
    if wid:
        os.system('xdotool windowactivate {}'.format(wid))
    else:
        app = QApplication(sys.argv)
        form = OneDialog()
        form.show()
        app.exec_()

If you have a better solution, let me know.

Categories: python, Qt, ubuntu