difference between quit and exit in python

The short answer is: both exit() and quit() are instances of the same Quitter class, the difference is in naming only, that must be added to increase user-friendliness of the interpreter. For more details let’s check out the source: http://hg.python.org/cpython In Lib/site.py (python-2.7) we see the following: def setquit(): “””Define new builtins ‘quit’ and ‘exit’. … Read more

How to imitate Python 3’s raise … from in Python 2?

There’s a raise_from in python-future; simply install it pip install future and import to use from future.utils import raise_from # or: from six import reraise as raise_from class FileDatabase: def __init__(self, filename): try: self.file = open(filename) except IOError as exc: raise_from(DatabaseError(‘failed to open’), exc) UPDATE The compatibility package six also supports raise_from, from version 1.9 … Read more

Freeze header in pandas dataframe

This function may do the trick: from ipywidgets import interact, IntSlider from IPython.display import display def freeze_header(df, num_rows=30, num_columns=10, step_rows=1, step_columns=1): “”” Freeze the headers (column and index names) of a Pandas DataFrame. A widget enables to slide through the rows and columns. Parameters ———- df : Pandas DataFrame DataFrame to display num_rows : int, … Read more

How to workaround `exist_ok` missing on Python 2.7?

One way around it is using pathlib. It has a backport for Python 2 and its mkdir() function supports exist_ok. try: from pathlib import Path except ImportError: from pathlib2 import Path # python 2 backport Path(settings.STATIC_ROOT).mkdir(exist_ok=True) As the comment suggests, use parents=True for makedirs(). Path(settings.STATIC_ROOT).mkdir(exist_ok=True, parents=True)

Error importing Seaborn module in Python: “ImportError: cannot import name utils”

I had faced the same problem. Restarting the notebook solved my problem. If that doesn’t solve the problem, you can try this pip install seaborn Edit As few people have posted in the comments, you can also use python -m pip install seaborn Plus, as per https://bugs.python.org/issue22295 it is a better way because in this … Read more

cv2.imread always returns NoneType

First, make sure the path is valid, not containing any single backslashes. Check the other answers, e.g. https://stackoverflow.com/a/26954461/463796. If the path is fixed but the image is still not loading, it might indeed be an OpenCV bug that is not resolved yet, as of 2013. cv2.imread is not working properly under Win32 for me either. … Read more

converting tiff to jpeg in python

I have successfully solved the issue. I posted the code to read the tiff files in a folder and convert into jpeg automatically. import os from PIL import Image yourpath = os.getcwd() for root, dirs, files in os.walk(yourpath, topdown=False): for name in files: print(os.path.join(root, name)) if os.path.splitext(os.path.join(root, name))[1].lower() == “.tiff”: if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + “.jpg”): … Read more