How to enable and use logging module in Python

Question: I am writing a Python program, and I would like to use Python’s built-in logging facility to debug the program. How can I enable and use the logging module in Python? Logging is an essential debugging feature for any programming and scripting language, which goes beyond simple print statements. For example, logging allows you […]
Continue reading…

 

How to install Python3 on CentOS

Question: I am trying to install a Python app that requires python3 on CentOS system. However, my CentOS only has python 2.X installed, but not python 3.X. How can I install python3 on CentOS? As of the latest CentOS 7, the default Python version still remains python 2.7, and python3 is not available in base […]
Continue reading…

 

How to change sys.path or PYTHONPATH in Python

Question: When I run a Python application, it is not able to find one of imported modules and fails. Looks like the directory of the Python module is not included in the default sys.path used by the Python interpreter. How can I change the default sys.path in Python? When the Python interpreter executes a program […]
Continue reading…

 

How to fix “ImportError: No module named setuptools” on Linux

Question: I was trying to install a Python package using setup.py (“python setup.py install”). However, it failed with the following error message: Traceback (most recent call last): File “setup.py”, line 2, in import setuptools ImportError: No module named ‘setuptools’ How can I fix this error? There are several ways to package, distribute and install Python […]
Continue reading…

 

How to fix “ImportError: No module named wxversion” on Linux

Question: I was trying to run a Python application on [insert your Linux distro], but I got an error “ImportError: No module named wxversion.” How can I solve this error in the Python program? Looking for python… 2.7.9 – Traceback (most recent call last): File “/home/dev/playonlinux/python/check_python.py”, line 1, in import os, wxversion ImportError: No module […]
Continue reading…

 

How to fix “ImportError: No module named scapy.all”

Question: When I run a Python application, I am getting “ImportError: No module named scapy.all” message. How can I fix this import error? Scapy is a flexible packet generation and sniffer program written in Python. Using Scapy, you can create arbitrary packets, send them to the wire, reading packets from the wire or a dump […]
Continue reading…

 

How to display JSON string in a human readable format on Linux

Question: I have JSON-formatted string. Is there a quick and easy way to display JSON string in a human readable format, preferably from the command line on Linux? When you are developing a web application, you may often deal with JSON-formatted input/output. When you are inspecting such JSON-formatted data, you may want to see it […]
Continue reading…

 

How to install pip on Linux

Question: I want to use pip to install Python packages. However, when I run pip, it says “pip: command not found”. How can I install pip on [insert your Linux distro]? There are various ways to install and manage Python packages. One of the easiest way is to use pip command line tool. Using pip, […]
Continue reading…

 

How to fix “unknown url type: https” error in easy_install

Question: I am trying to build and install a Python package with easy_install, but I am getting “unknown url type: https” error during package download. How can I fix this error? A symptom of this error looks like the following. Download error on http://pypi.python.org/simple/regex/: unknown url type: https — Some packages may not be found! […]
Continue reading…

 

How to check if two .pyc files are the same byte code

Question: I have two .pyc Python bytecode files that I obtained from somewhere. Is there an easy way to check if these two .pyc files contain the same byte code? Since .pyc files are in binary format, you may attempt to either compare their md5sum outputs, or use cmp command to do binary diff. However, […]
Continue reading…