Questions tagged [python-3.x]

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

Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. It was created by Guido van Rossum, and first released on February 20, 1991. 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

341062 questions
458
votes
11 answers

What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?

When I try to use a print statement in Python, it gives me this error: >>> print "Hello, World!" File "", line 1 print "Hello, World!" ^ SyntaxError: Missing parentheses in call to 'print' What does that mean?
ncoghlan
  • 40,168
  • 10
  • 71
  • 80
457
votes
10 answers

How to correct TypeError: Unicode-objects must be encoded before hashing?

I have this error: Traceback (most recent call last): File "python_md5_cracker.py", line 27, in m.update(line) TypeError: Unicode-objects must be encoded before hashing when I try to execute this code in Python 3.2.2: import hashlib,…
JohnnyFromBF
  • 9,873
  • 10
  • 45
  • 59
454
votes
14 answers

Extract a subset of key-value pairs from dictionary?

I have a big dictionary object that has several key value pairs (about 16), but I am only interested in 3 of them. What is the best way (shortest/efficient/most elegant) to subset such dictionary? The best I know is: bigdict =…
Jayesh
  • 51,787
  • 22
  • 76
  • 99
448
votes
12 answers

What is an alternative to execfile in Python 3?

It seems they canceled in Python 3 all the easy ways to quickly load a script by removing execfile(). Is there an obvious alternative I'm missing?
R S
  • 11,359
  • 10
  • 43
  • 50
447
votes
9 answers

Download file from web in Python 3

I am creating a program that will download a .jar (java) file from a web server, by reading the URL that is specified in the .jad file of the same game/application. I'm using Python 3.2.1 I've managed to extract the URL of the JAR file from the JAD…
Bo Milanovich
  • 7,995
  • 9
  • 44
  • 61
440
votes
18 answers

How to install python3 version of package via pip on Ubuntu?

I have both python2.7 and python3.2 installed in Ubuntu 12.04. The symbolic link python links to python2.7. When I type: sudo pip install package-name It will default install python2 version of package-name. Some package supports both python2 and…
kev
  • 155,172
  • 47
  • 273
  • 272
430
votes
12 answers

Which is the preferred way to concatenate a string in Python?

Since Python's string can't be changed, I was wondering how to concatenate a string more efficiently? I can write like it: s += stringfromelsewhere or like this: s = [] s.append(somestring) # later s = ''.join(s) While writing this…
Max
  • 7,957
  • 10
  • 33
  • 39
424
votes
3 answers

How to convert 'binary string' to normal string in Python3?

For example, I have a string like this(return value of subprocess.check_output): >>> b'a string' b'a string' Whatever I did to it, it is always printed with the annoying b' before the string: >>> print(b'a string') b'a string' >>> print(str(b'a…
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
421
votes
10 answers

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

I am using Python 3.1 on a Windows 7 machine. Russian is the default system language, and utf-8 is the default encoding. Looking at the answer to a previous question, I have attempting using the "codecs" module to give me a little luck. Here's a few…
Eric
  • 4,283
  • 3
  • 18
  • 7
412
votes
19 answers

Relative imports - ModuleNotFoundError: No module named x

This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files: test.py config.py config.py has a few functions defined in it as well as a few variables. I've stripped it down to…
blitzmann
  • 7,319
  • 5
  • 23
  • 29
410
votes
3 answers

Python "raise from" usage

What's the difference between raise and raise from in Python? try: raise ValueError except Exception as e: raise IndexError which yields Traceback (most recent call last): File "tmp.py", line 2, in raise…
darkfeline
  • 9,404
  • 5
  • 31
  • 32
398
votes
6 answers

What's the difference between `raw_input()` and `input()` in Python 3?

What is the difference between raw_input() and input() in Python 3?
pkumar
  • 4,743
  • 2
  • 19
  • 21
394
votes
5 answers

Is __init__.py not required for packages in Python 3.3+

I am using Python 3.5.1. I read the document and the package section here: https://docs.python.org/3/tutorial/modules.html#packages Now, I have the following structure: /home/wujek/Playground/a/b/module.py module.py: class Foo: def…
wujek
  • 10,112
  • 12
  • 52
  • 88
394
votes
21 answers

Python 3 ImportError: No module named 'ConfigParser'

I am trying to pip install the MySQL-python package, but I get an ImportError. Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python Downloading/unpacking MySQL-python Running setup.py egg_info…
if __name__ is None
  • 11,083
  • 17
  • 55
  • 71
391
votes
2 answers

How to specify "nullable" return type with type hints

Suppose I have a function: def get_some_date(some_argument: int=None) -> %datetime_or_None%: if some_argument is not None and some_argument == 1: return datetime.utcnow() else: return None How do I specify the return type…
exfizik
  • 5,351
  • 4
  • 23
  • 26