Questions tagged [python-3.x]

USE ONLY IF YOUR QUESTION IS VERSION-SPECIFIC. For questions about Python programming that are specific to version 3+ of the language. Use the more generic [python] tag on all Python questions. Use the [python-2.x] tags for Python 2 questions.

Python 3 is the latest version of the Python programming language, first released on December 3rd, 2008. It features simplifications and improvements to the syntax of the language. Some of these changes are backwards incompatible, and therefore Python 3 has its own tag.

Although Python 3 is now the recommended and supported version of the language, some users still remain on version 2.7 for various reasons. If you start new projects or begin to learn Python, version 3 is now the recommended target under normal circumstances:

Python 3 is strongly recommended for any new development. As of January 2020, Python 2 has reached End Of Life status, meaning it will receive no further updates or bugfixes, including for security issues. Many frameworks and other add on projects are following a similar policy. As such, we can only recommend learning and teaching Python 3.

One of the main differences is in the print statement.

Python 2:

print "Hello World"

Python 3:

print("Hello World")

For more information on the differences, see Porting Python 2 Code to Python 3.

For information on Python in general, visit the main Python tag wiki.


Tagging recommendation:

Use the tag for all Python related questions. If you believe your question includes issues specific to the incompatible Python 2.x or Python 3.x, in addition to the main tag, use or . If you believe your question may be even more specific, you may include a version specific tag, such as .

Please do not mix (or a more specific tag such as and (ditto) unless you are specifically asking a question about an interoperability problem between versions.

Python Free Tutorials

Python Online Books

Python API Reference

Python Online IDE

Python Package Index

285551 questions
2930
votes
13 answers

How can I make a time delay in Python?

I would like to know how to put a time delay in a Python script.
user46646
  • 133,483
  • 43
  • 73
  • 82
2877
votes
20 answers

Convert bytes to a string

I'm using this code to get standard output from an external program: >>> from subprocess import * >>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0] The communicate() method returns an array of bytes: >>> command_stdout b'total…
Tomas Sedovic
  • 34,313
  • 9
  • 36
  • 30
2418
votes
11 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 the following line to take an inordinate amount of…
Rick supports Monica
  • 33,838
  • 9
  • 54
  • 100
1404
votes
6 answers

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

What is the Python 3 equivalent of python -m SimpleHTTPServer?
ryanbraganza
  • 14,583
  • 3
  • 13
  • 23
1382
votes
13 answers

How can I flush the output of the print function?

How do I force Python's print function to output to the screen?
Walter Nissen
  • 14,485
  • 4
  • 24
  • 26
1223
votes
10 answers

What is setup.py?

Can anyone please explain what setup.py is and how it can be configured or used?
Software Enthusiastic
  • 21,163
  • 15
  • 55
  • 66
1143
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?
John Rutherford
  • 10,664
  • 6
  • 26
  • 31
1117
votes
4 answers

Best way to convert string to bytes in Python 3?

There appear to be two different ways to convert a string to bytes, as seen in the answers to TypeError: 'str' does not support the buffer interface Which of these methods would be better or more Pythonic? Or is it just a matter of personal…
Mark Ransom
  • 271,357
  • 39
  • 345
  • 578
1049
votes
21 answers

How to copy a dictionary and only edit the copy

Can someone please explain this to me? This doesn't make any sense to me. I copy a dictionary into another and edit the second and both are changed. Why is this happening? >>> dict1 = {"key1": "value1", "key2": "value2"} >>> dict2 = dict1 >>>…
MadSc13ntist
  • 15,070
  • 7
  • 23
  • 18
1043
votes
18 answers

Relative imports in Python 3

I want to import a function from another file in the same directory. Sometimes it works for me with from .mymodule import myfunction but sometimes I get a: SystemError: Parent module '' not loaded, cannot perform relative import Sometimes it works…
John Smith Optional
  • 15,639
  • 10
  • 36
  • 54
975
votes
13 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 the shebang. On the other hand the Django project…
treecoder
  • 36,160
  • 18
  • 57
  • 89
965
votes
9 answers

How to return dictionary keys as a list in Python?

In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3] Now, in Python >= 3.3, I get something like this: >>> newdict.keys() dict_keys([1, 2, 3]) So, I have to do this to…
user2015601
815
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 virtualenv that uses the new version? e.g. sudo…
Prometheus
  • 27,277
  • 37
  • 139
  • 270
799
votes
10 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? This folder keeps getting created, what it is for?
user2063042
  • 8,037
  • 3
  • 12
  • 6
715
votes
9 answers

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

I've very recently migrated to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp:…
masroore
  • 7,815
  • 3
  • 15
  • 21
1
2 3
99 100