Any gotchas using unicode_literals in Python 2.6?

The main source of problems I’ve had working with unicode strings is when you mix utf-8 encoded strings with unicode ones. For example, consider the following scripts. two.py # encoding: utf-8 name=”helló wörld from two” one.py # encoding: utf-8 from __future__ import unicode_literals import two name=”helló wörld from one” print name + two.name The output … Read more

Pipe subprocess standard output to a variable [duplicate]

To get the output of ls, use stdout=subprocess.PIPE. >>> proc = subprocess.Popen(‘ls’, stdout=subprocess.PIPE) >>> output = proc.stdout.read() >>> print output bar baz foo The command cdrecord –help outputs to stderr, so you need to pipe that indstead. You should also break up the command into a list of tokens as I’ve done below, or the … Read more

Visibility of global variables in imported modules

Globals in Python are global to a module, not across all modules. (Many people are confused by this, because in, say, C, a global is the same across all implementation files unless you explicitly make it static.) There are different ways to solve this, depending on your actual use case. Before even going down this … Read more

Python try…except comma vs ‘as’ in except

The definitive document is PEP-3110: Catching Exceptions Summary: In Python 3.x, using as is required to assign an exception to a variable. In Python 2.6+, use the as syntax, since it is far less ambiguous and forward compatible with Python 3.x. In Python 2.5 and earlier, use the comma version, since as isn’t supported.

Suppress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6

You can disable any Python warnings via the PYTHONWARNINGS environment variable. In this case, you want: export PYTHONWARNINGS=”ignore:Unverified HTTPS request” To disable using Python code (requests >= 2.16.0): import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) For requests < 2.16.0, see original answer below. Original answer The reason doing urllib3.disable_warnings() didn’t work for you is because it looks like you’re … Read more

bash: pip: command not found

Why not just do sudo easy_install pip or if this is for python 2.6 sudo easy_install-2.6 pip? This installs pip using the default python package installer system and saves you the hassle of manual set-up all at the same time. This will allow you to then run the pip command for python package installation as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)