How-to articles, tricks, and solutions about PYTHON

What does __all__ mean in Python?

In Python, __all__ is a list of strings that defines the names that should be imported when from <module> import * is used.

How do I get the filename without the extension from a path in Python?

To get the filename without the extension from a file path in Python, you can use the os.path.splitext() function.

Use different Python version with virtualenv

To use a different Python version with virtualenv, follow these steps:

Convert integer to string in Python

To convert an integer to a string in Python, use the str() function.

How do I get time of a Python program's execution?

To get the time it takes for a Python program to execute, you can use the time module.

How do I write JSON data to a file?

To write JSON data to a file in Python, you can use the json module.

How do I reverse a string in Python?

To reverse a string in Python, you can use the following method:

Save plot to image file instead of displaying it using Matplotlib

To save a plot to an image file using Matplotlib, you can use the savefig function.

How can I install packages using pip according to the requirements.txt file from a local directory?

To install packages using pip from a local directory, you can use the -r option to specify the path to the requirements.txt file.

Why do Python classes inherit object?

In Python 3, all classes automatically inherit from the object class.

Selecting multiple columns in a Pandas dataframe

To select multiple columns in a pandas DataFrame, you can pass a list of column names to the indexing operator [].

How can I make a dictionary (dict) from separate lists of keys and values?

You can use the zip function to create a dictionary from separate lists of keys and values like this:

How can I import a module dynamically given the full path?

You can use the importlib.import_module function to import a module dynamically given the full path to the module.

How do I install pip on macOS or OS X?

To install pip on macOS or OS X, you need to use the Terminal.

Random string generation with upper case letters and digits

Here is a simple function that generates a random string of a given length using upper case letters and digits:

How do I print to stderr in Python?

To print to stderr in Python, you can use the stderr attribute of the sys module:

fatal error: Python.h: No such file or directory

It looks like you are trying to include the Python.h header file in a C or C++ program, but it is not found on your system.

Proper way to declare custom exceptions in modern Python?

To declare a custom exception in Python, you can create a new class that inherits from the built-in Exception class.

How do I get the row count of a Pandas DataFrame?

You can use the shape property of the DataFrame to get the number of rows and columns.

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

In Python, a single underscore "_" before an object name indicates that the object is meant to be private, meaning that it should not be directly accessed or modified outside of the class that it is defined in.

Replacements for switch statement in Python?

Here are a few alternatives to using a switch statement in Python:

How do I read from stdin?

You can read from stdin in Python using the input function.

Extracting extension from filename in Python

You can extract the file extension from a file name in Python by using the os.path module.