Wednesday, 27 September 2017

Django Python Web App Development - Python XHTML2PDF Library error FIXED - "ImportError: cannot import name inputstream django" Solved

cannot import name inputstream, Django, Django alirazabhayani, Django Full Stack Development ali raza bhayani, html5lib, inputstream, Python, Python alirazabhayani, xhtml2pdf

While working on a Django Web Application using Python, I recently encountered an error while using a library named xhtml2pdf. This library is a Python library for exporting xhtml to PDF.

While using it in Django Web framework for developing a hassle free web service to export customized HTML templates into PDF, I encountered some error which I had to dig into the library to find the actual problem and its solution. I noticed that this problem of "cannot import name inputstream django" was faced by many people and therefore writing this post to document it. Please note that this proposed solution is workable for Python 2.7 and hasn't been checked on Python v.3.x .

When I installed xhtml2pdf library and used it in Django Web framework, I encountered following error:
 importerror: cannot import name inputstream 
Following trace shows that the error is actually triggered from html5lib which is one of the dependencies of xhtml2pdf:
File "/usr/home/username/virtualenvs/projectname/lib/python2.7/site-packages/xhtml2pdf/parser.py", line 17, in 
    from html5lib import treebuilders, inputstream 
ImportError: cannot import name inputstream
Due to broken dependencies of xhtml2pdf and lack of documentation, I had to dig in the module which revealed that this error was coming due to the fact that xhtml2pdf requires a specific version of html5lib to work properly. To solve this issue, I installed html5lib's version 1.0b8 in the following way:
$ pip install html5lib==1.0b8
 
If you had previously installed html5lib's different version, you will have to uninstall it first by running following command on your terminal or virtual environment:
$ pip uninstall html5lib

Thursday, 20 October 2016

Adding IP aliases in FreeBSD - Freebsd ifconfig add Remove Alias IPv4 / IPv6 / inet6

FreeBSD Assign IPv4 address
: # ifconfig em0 inet 192.168.10.5 netmask 255.255.255.0
FreeBSD ifconfig add alias for IPv4
: # ifconfig em0 inet 192.168.10.10/24 add
FreeBSD ifconfig remove alias for IPv4
: # ifconfig em0 inet 192.168.10.10/24 -alias
For IPv6
FreeBSD Enable IPv6 functionality of the interface:
: # ifconfig em0 inet6 -ifdisabled
FreeBSD Add the IPv6 address 2001:DB8:DBDB::123/48 to the interface em0:
: # ifconfig em0 inet6 2001:db8:bdbd::123 prefixlen 48 alias
FreeBSD ifconfig remove/delete Alias for IPv6 inet6
: # ifconfig em0 inet6 2001:db8:bdbd::123/48 delete

Monday, 22 August 2016

PostgreSQL setval Sequence - PostgreSQL manually alter sequence - Reset sequence of setval in PostgreSQL - Ali Raza Bhayani


In one of the my PostgreSQL tables, I used a very handy builtin Sequence Manipulation function setval() of PostgreSQL to generate Primary Keys in a sequential and controlled manner. But during a migration, I wanted to identify new records by Primary Key sequence greater than thousand. A very handy and tested recipe to restart the setval() function sequence for such cases is by using RESTART WITH in the following way:
ALTER SEQUENCE sequence_name RESTART WITH 1000;
is equivalent to:
    SELECT setval('sequence_name', 1000, FALSE);
Either of the statements may be used to restart the sequence and you can get the next value by:
    nextval('sequence_name')
The above recipe can also be used to reset PostgreSQL primary key sequence when it falls out of sync.

PostgreSQL setval Sequence - PostgreSQL manually alter sequence - Reset sequence of setval in PostgreSQL - Ali Raza Bhayani


In one of the my PostgreSQL tables, I used a very handy builtin Sequence Manipulation function setval() of PostgreSQL to generate Primary Keys in a sequential and controlled manner. But during a migration, I wanted to identify new records by Primary Key sequence greater than thousand. A very handy and tested recipe to restart the setval() function sequence for such cases is by using RESTART WITH in the following way:
ALTER SEQUENCE seq RESTART WITH 1000;
is equivalent to:
    SELECT setval('sequence_name', 1, FALSE);
Either of the above statements may be used to restart the sequence and for getting the next value following statement can be used:
    nextval('sequence_name')
The above recipe can also be used to reset PostgreSQL primary key sequence when it falls out of sync.

Wednesday, 29 June 2016

Install and run Redis on Mac OS - Redis Server Installation and First Run

Following is the recipe to install Redis server on Mac OS using homebrew in a very simple manner:

 $ brew install redis


After installation, you may edit Redis configuration file by going to the path via Mac OS terminal:

$ /usr/local/etc/redis.conf

To start Redis Server using configuration file on Mac OS, use following command:
$ redis-server /usr/local/etc/redis.conf

After running the Redis Server on Mac OS, you can check whether Redis Server is running by opening a new terminal window and enter following command:
$ redis-cli ping

Install and run Redis on Mac OS - Redis Server Installation and First Run

Following is the recipe to install Redis server on Mac OS using homebrew in a very simple manner:

 $ brew install redis


After installation, you may edit Redis configuration file by going to the path via Mac OS terminal:

$ /usr/local/etc/redis.conf

To start Redis Server using configuration file on Mac OS, use following command:
$ redis-server /usr/local/etc/redis.conf

After running the Redis Server on Mac OS, you can check whether Redis Server is running by opening a new terminal window and enter following command:
$ redis-cli ping

The server should reply PONG which means that the server is pinging.

Tuesday, 24 February 2015

MongoDB Correct way to start MongoDB - Error Cannot connect to mongodb errno:61 Connection refused [Solved]

Recently, while involved in full stack Django development and developing an application using Mongodb as database backend, I encountered following error after installing Mongodb on my mac. Ok first, I installed Mongodb by using brew install command:

brew update
brew install mongodb

After installing Mongodb, I tried to access the mongo shell by using mongo command, but I got "Cannot connect to mongodb errno:61 Connection refused error" . Here is the full trace:

alis-air:~ aliraza$ mongo
MongoDB shell version: 2.6.6
connecting to: test
2015-02-24T17:51:36.067+0500 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-02-24T17:51:36.069+0500 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

I searched for correct ways to start Mongodb on Mac OS. I tried several solutions but the one which worked for me is as follows: In the shell write sudo mongod to start it.

$ mkdir -p /data/db
$ sudo mongod

This started my Mongodb. Next I opened another shell instance (new shell window) and wrote mongo command and it worked without Error Cannot connect to mongodb errno:61 Connection refused! MongoDB starting Problem Solved.

$ mongo
MongoDB shell version: 2.6.6
connecting to: test
Server has startup warnings: 
2015-02-24T18:20:51.384+0500 [initandlisten] 
2015-02-24T18:20:51.384+0500 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>