How to print to stderr in Python?

I found this to be the only one short, flexible, portable and readable: # This line only if you still care about Python2 from __future__ import print_function import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) The optional function eprint saves some repetition. It can be used in the same way as the standard print function: … Read more

What is the meaning of single and double underscore before an object name?

Single Underscore In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way. Using leading underscores for functions in a module indicates it should not be imported from somewhere else. From the … Read more

How do I install pip on macOS or OS X?

TLDR. On any modern Mac python3 -m ensurepip then pip3 –version to check. pip’s documentation lists the supported mechanisms to install it: https://pip.pypa.io/en/stable/installation/#supported-methods It is generally recommended to avoid installing pip on the OS-provided python commands, and to install Python via the official installers or using something like Homebrew or pyenv. Python 3.4+ will have … Read more

How to prettyprint a JSON file?

The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by: >>> import json >>> >>> your_json = ‘[“foo”, {“bar”:[“baz”, null, 1.0, 2]}]’ >>> parsed = json.loads(your_json) >>> print(json.dumps(parsed, indent=4, sort_keys=True)) [ “foo”, { “bar”: [ “baz”, null, 1.0, … Read more

How do I read from stdin?

Use the fileinput module: import fileinput for line in fileinput.input(): pass fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided. Note: line will contain a trailing newline; to remove it use line.rstrip().

Extracting extension from filename in Python

Use os.path.splitext: >>> import os >>> filename, file_extension = os.path.splitext(‘/path/to/somefile.ext’) >>> filename ‘/path/to/somefile’ >>> file_extension ‘.ext’ Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc: >>> os.path.splitext(‘/a/b.c/d’) (‘/a/b.c/d’, ”) >>> os.path.splitext(‘.bashrc’) … Read more

Installing specific package version with pip

TL;DR: pip install -Iv (i.e. pip install -Iv MySQL_python==1.2.2) What these options mean: -I stands for –ignore-installed which will ignore the installed packages, overwriting them. -v is for verbose. You can combine for even more verbosity (i.e. -vv) up to 3 times (e.g. -Ivvv). For more information, see pip install –help First, I see two … Read more

What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?

This is my personal recommendation for beginners: start by learning virtualenv and pip, tools which work with both Python 2 and 3 and in a variety of situations, and pick up other tools once you start needing them. Now on to the answer to the question: what is the difference between these simalarly named things: … Read more

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