How are we doing? Please help us improve Stack Overflow. Take our short survey

Questions tagged [python-3.x]

DO NOT USE UNLESS YOUR QUESTION IS FOR PYTHON 3 ONLY. Always use alongside the standard [python] tag.

3463 votes
24 answers

Convert bytes to a string

I captured the standard output of an external program into a bytes object: >>> from subprocess import * >>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0] >>...
2803 votes
12 answers

Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator. This being the case, I would have expected ...
1567 votes
7 answers

What is the Python 3 equivalent of "python -m SimpleHTTPServer"

What is the Python 3 equivalent of python -m SimpleHTTPServer?
  • 16.3k
1513 votes
13 answers

How can I flush the output of the print function?

How do I force Python's print function to flush the buffered output to the screen?
1389 votes
26 answers

Relative imports in Python 3

I want to import a function from another file in the same directory. Usually, one of the following works: from .mymodule import myfunction from mymodule import myfunction ...but the other one gives ...
1340 votes
5 answers

Best way to convert string to bytes in Python 3?

TypeError: 'str' does not support the buffer interface suggests two possible methods to convert a string to bytes: b = bytes(mystring, 'utf-8') b = mystring.encode('utf-8') Which method is ...
1190 votes
11 answers

How to return dictionary keys as a list in Python?

With Python 2.7, I can get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3] With Python >= 3.3, I get: >>> newdict....
's user avatar
1141 votes
43 answers

How can I represent an 'Enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python. How can I represent the equivalent of an Enum in Python?
1128 votes
15 answers

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

Should I put the shebang in my Python scripts? In what form? #!/usr/bin/env python or #!/usr/local/bin/python Are these equally portable? Which form is used most? Note: the tornado project uses ...
  • 40.7k
1023 votes
7 answers

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

I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: ...
976 votes
15 answers

What is the naming convention in Python for variable and function?

Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase: // C# example string thisIsMyVariable = "a" public void ThisIsMyMethod() ...
  • 182k
965 votes
11 answers

What is __pycache__?

From what I understand, a cache is an encrypted file of similar files. What do we do with the __pycache__ folder? Is it what we give to people instead of our source code? Is it just my input data? ...
  • 9,697
880 votes
12 answers

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

I'm trying to get a Python 3 program to do some manipulations with a text file filled with information. However, when trying to read the file I get the following error: Traceback (most recent call ...
  • 13k
862 votes
22 answers

Using Python 3 in virtualenv

Using virtualenv, I run my projects with the default version of Python (2.7). On one project, I need to use Python 3.4. I used brew install python3 to install it on my Mac. Now, how do I create a ...
  • 30.7k
856 votes
11 answers

What does -> mean in Python function definitions?

I've recently noticed something interesting when looking at Python 3.3 grammar specification: funcdef: 'def' NAME parameters ['->' test] ':' suite The optional 'arrow' block was absent in Python 2 ...
  • 8,781

15 30 50 per page
2 3 4 5