Category Archives: python

Run python virtualenv with ubuntu

By default, my ubuntu runs python2.7 (i.e. simply run python –version). This comes from /usr/bin/python –> /usr/bin/python2.7 I changed the link to point to python3 instead. BIG MISTAKE Python is used all over the place by the system => apt-get update kept failing so I had to revert back to python2.7 link To use virtualenv with python3, things are pretty easy, you simply need to prefix your commands: sudo apt-get install python3-pip sudo pip3 install virtualenv To create a virtualenv: Go to folder where you want to create the virtual env: cd /path/to/my/project To create virtual env, run: virtualenv-3-4 myvirtualenv […]

install lxml inside python virtualenv [solved]

When trying to install lxml inside virtualenv, I kept getting some compilation error messages. I already had lxml installed outside virtualenv, i.e. in my global python library. I got it installed via running: sudo apt-get install python-lxml This is of no use when using virtualenv since by definition each virtual environment will have it’s own libraries. As mentioned, pip failed when running: pip install lxml Error message: In file included from src/lxml/lxml.etree.c:239:0: /tmp/pip-build-gc7tlt4f/lxml/src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: No such file or directory #include “libxml/xmlversion.h” ^ compilation terminated. Reason it is occurring is because when installing lxml python library, a compilation occurs. […]

how to setup cx_Oracle on python – windows [solved]

When trying to use cx_Oracle in python on windows, I faced following error: >>> import cx_Oracle Traceback (most recent call last): File “<stdin>”, line 1, in <module> ImportError: DLL load failed: The specified module could not be found. In my case, oracle instant client was not installed. Here is how to solve it. Steps Download and unzip oracle instant client appropriate for your configuration: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html note: on my side i downloaded basic client + odbc + jdbc + sqlplus Ensure environment variables ORACLE_HOME and PATH points to instant client folder note: in windows, go to control panel > System > […]

python logging – KeyError: ‘fileHandler’ [solved]

I tried to use python native logging mechanism, with logging config loaded from external .conf file via logging.config module. I got hard time finding out what was causing below error: File “/usr/lib/python3.4/logging/config.py”, line 253, in _install_loggers logger.addHandler(handlers[hand]) KeyError: ‘fileHandler’ Top of my configuration file was as per below, declaring 2 loggers, 1 handler and 1 formatter: [loggers] keys=root,defaultLogger [handlers] keys=consoleHandler [formatters] keys=simpleFormatter Body of my configuration file contained the following: [handler_fileHandler] class=FileHandler level=DEBUG formatter=simpleFormatter args=(‘log/NPID[NPID].spo.log’, ‘a’, ‘UTF-8’, False) [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=simpleFormatter args=(sys.stdout,) In other words, I am defining 2 handlers, but only declared 1 in the top part. That […]

install .whl files for python

Simply run pip install <path_to_whl_file>.whl Easy! On windows, pip.exe is under “<python_installation_folder>/Scripts”  

querying url / webpages using python

Although querying webpages using python can be performed using urllib, it seems a simpler way is to use library called “requests“. Installation go to http://www.python-requests.org and download zip file (other installation instructions are available, check requests documentation) save zip file into your python installation folder or any where else in your computer unzip content open cmd shell and go to new folder from there, run “python setup.py install” Done! You should know be able to run “import requests” from python cmd Query for an url import requests link = “http://www.somesite.com/” f = requests.get(link) print f.text sources http://www.python-requests.org http://stackoverflow.com/questions/15138614/read-url-in-python

python: insert newline with regular expression (sub | substitute)

Python allows to use regular expressions on multiline strings. You can use re.MULTILINE flag to make ‘^’ and ‘$’ match resp. the beginning and end of each line. The problem is how to insert newline character doing substitutions ? Usually, substitution pattern is a raw string ex: print re.sub(‘mytext’, ‘t(.)x’, r’T\1X’) # displays ‘myTeXt’ Here we use a raw string for substitution pattern (string prefixed with ‘r’ char), otherwise we would need to double the escape character (r’T\1X’ => ‘T\\1X’) Problem with raw strings is that content is not interpreted, particularly ‘\n’ is not interpreted and is considered being two […]

python decorators: thank god! (advanced class features in python)

[sources] http://wiki.python.org/moin/PythonDecorators exhaustive python decorators coverage http://wiki.python.org/moin/PythonDecoratorLibrary python decorator library http://www.python.org/dev/peps/pep-0318/ http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators wikipedia quick explanation of python decorators

python: easter eggs — developpers’ jokes

using culy braces instead of forced indentation as been requested for long to python developing team, run ‘from __future__ import braces’ to see what they think about it. Similarly, if you want to know python’s phylosphy, run “import this”, which prints: The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly […]

python: ‘with’ statement

With python2.5 comes ‘with’ statement. To use it, first you need to import it: ‘from __future__ import with_statement’ It’s usage is pretty simple: with “expr” as “myname”: … do something where: – “myname” is a variable name in which the result of “expr”.__enter__() will be stored – “expr” must be a context expression, i.e. an object which implements both __enter__() and __exit__() method ‘With’ statement has primarily being developped to reduce overhead of length try-except-else-finally statements. If an exception is raised within the ‘with’ loop, then __exit__() method is called with corresponding (type, msg, traceback) objects. => instead of repeatedly […]