How-to articles, tricks, and solutions about PYTHON-3.X

"Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3

This error occurs when trying to open a file that contains escape characters (such as \) in the file path, and the escape characters are not being properly interpreted by Python.

Best way to convert string to bytes in Python 3?

In Python 3, you can convert a string to bytes using the bytes function.

Convert bytes to a string

To convert a byte object into a string, you can use the decode() method.

Error "Import Error: No module named numpy" on Windows

This error message indicates that the Python interpreter is unable to find the numpy module, which is likely because it is not installed on your system.

error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Here is an example of how to handle a UnicodeDecodeError caused by an invalid start byte:

How can I flush the output of the print function?

To flush the output of the print function, you can use the flush parameter of the print function.

How can I represent an 'Enum' in Python?

In Python, you can represent an Enum (enumeration) by using the enum module or by creating a class that inherits from enum.Enum.

How do I type hint a method with the type of the enclosing class?

In Python, you can use the self keyword to refer to the instance of the enclosing class within a method.

How do I upgrade the Python installation in Windows 10?

To upgrade the Python installation in Windows 10, you can use the pip package manager.

How do I use raw_input in Python 3?

In Python 3, the input() function can be used in place of raw_input() to read input from the user.

How to activate virtualenv in Linux?

To activate a virtual environment in Linux, you can use the source command and the path to the activate script that is located in the virtual environment's bin directory.

How to install pip with Python 3?

To install pip with Python 3, you can use the following command:

How to return dictionary keys as a list in Python?

In Python, you can use the dict.keys() method to return a view object that contains the keys of a dictionary.

How to search and replace text in a file?

There are several ways to search and replace text in a file, depending on the programming language you are using.

Import error: No module name urllib2

It looks like you are trying to import the urllib2 module, which is not available in Python 3.

List attributes of an object

In Python, you can use the built-in function dir() to list the attributes of an object.

Print multiple arguments in Python

Here is an example of how to print multiple arguments in Python:

Python - TypeError: 'int' object is not iterable

This error message is indicating that you are trying to iterate over an object of type 'int', which is not iterable (i.e.

Python 3: UnboundLocalError: local variable referenced before assignment

This error occurs when you are trying to access a variable before it has been assigned a value.

Relative imports in Python 3

Relative imports in Python 3 allow you to import modules or functions from other packages within your package hierarchy.

Should I put #! (shebang) in Python scripts, and what form should it take?

You should include a shebang (#!) in Python scripts if you want the script to be directly executable from the command line.

SyntaxError: unexpected EOF while parsing

The SyntaxError: unexpected EOF while parsing error is raised when the Python interpreter reaches the end of the file (EOF) while it is still parsing the file, and it is unable to complete the parsing process because of an error in the syntax of the code.

TypeError: a bytes-like object is required, not 'str' when writing to a file in Python 3

This error occurs when you are trying to write a string to a file using the write() method in Python 3, but the file is opened in binary mode (using the 'b' flag when opening the file).

TypeError: Missing 1 required positional argument: 'self'

This error message is indicating that a class method is being called without providing the "self" parameter, which is the first parameter for all class methods and refers to the instance of the class.

UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

This error occurs when trying to decode a string using the 'charmap' codec, which is typically used for Windows-1252 character encoding.

1 2