Questions tagged [python-2.x]

For questions about Python programming that are specific to version 2.x of the language. Use the more generic [python] tag for all Python questions, and only add this tag if your question is version-specific.

Python 2 is the version of the Python programming language which was for a long time the most widely deployed in production environments; but it is now in the process of being displaced by Python 3. The two versions of the language are not compatible, though many aspects of the syntax are identical. The latest, and last, released version of Python 2 is Python 2.7.18.

Official support for Python 2 has ended on January 1, 2020.

See also Python2 or Python3.

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 can 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.

Community

Free Python Programming Books

2871 questions
275
votes
18 answers

How can I scrape a page with dynamic content (created by JavaScript) in Python?

I'm trying to develop a simple web scraper. I want to extract plain text without HTML markup. My code works on plain (static) HTML, but not when content is generated by JavaScript embedded in the page. In particular, when I use…
mocopera
  • 2,803
  • 3
  • 18
  • 10
257
votes
11 answers

How to use XPath in Python?

What are the libraries that support XPath? Is there a full implementation? How is the library used? Where is its website?
yeruham
250
votes
8 answers

Writing Unicode text to a text file?

I'm pulling data out of a Google doc, processing it, and writing it to a file (that eventually I will paste into a Wordpress page). It has some non-ASCII symbols. How can I convert these safely to symbols that can be used in HTML source? Currently…
simon
  • 5,987
  • 13
  • 31
  • 28
246
votes
15 answers

How do I keep Python print from adding newlines or spaces?

In python, if I say print 'h' I get the letter h and a newline. If I say print 'h', I get the letter h and no newline. If I say print 'h', print 'm', I get the letter h, a space, and the letter m. How can I prevent Python from printing the…
Bart
  • 10,873
  • 7
  • 29
  • 23
242
votes
31 answers

Django TemplateDoesNotExist?

My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk. For every URL I request, it throws: TemplateDoesNotExist at /appname/path appname/template_name.html Django tried loading these…
jack
  • 17,261
  • 37
  • 100
  • 125
205
votes
13 answers

Combine several images horizontally with Python

I am trying to horizontally combine some JPEG images in Python. Problem I have 3 images - each is 148 x 95 - see attached. I just made 3 copies of the same image - that is why they are the same. My attempt I am trying to horizontally join them…
edesz
  • 11,756
  • 22
  • 75
  • 123
204
votes
4 answers

Determine if 2 lists have the same elements, regardless of order?

Sorry for the simple question, but I'm having a hard time finding the answer. When I compare 2 lists, I want to know if they are "equal" in that they have the same contents, but in different order. Ex: x = ['a', 'b'] y = ['b', 'a'] I want x == y to…
toofly
  • 2,167
  • 3
  • 15
  • 9
191
votes
4 answers

What is `1..__truediv__` ? Does Python have a .. ("dot dot") notation syntax?

I recently came across a syntax I never seen before when I learned python nor in most tutorials, the .. notation, it looks something like this: f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8)) # prints 0.125 I figured it was exactly…
Taku
  • 31,927
  • 11
  • 74
  • 85
187
votes
13 answers

How to reinstall python@2 from Homebrew?

I have been having issues with openssl and python@2 with brew, which have explained here (unresolved). The documented workaround to reinstall Python and openssl was not working, so I decided I would uninstall and reinstall Python. The problem is,…
Pauline
  • 3,566
  • 8
  • 23
  • 39
187
votes
4 answers

Why should we NOT use sys.setdefaultencoding("utf-8") in a py script?

I have seen few py scripts which use this at the top of the script. In what cases one should use it? import sys reload(sys) sys.setdefaultencoding("utf-8")
mlzboy
  • 14,343
  • 23
  • 76
  • 97
186
votes
10 answers

How to check if variable is string with python 2 and 3 compatibility

I'm aware that I can use: isinstance(x, str) in python-3.x but I need to check if something is a string in python-2.x as well. Will isinstance(x, str) work as expected in python-2.x? Or will I need to check the version and use isinstance(x,…
Randall Hunt
  • 12,132
  • 6
  • 32
  • 42
185
votes
7 answers

What is the difference between encode/decode?

I've never been sure that I understand the difference between str/unicode decode and encode. I know that str().decode() is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a…
ʞɔıu
  • 47,148
  • 35
  • 106
  • 149
182
votes
2 answers

How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?

The following snippet is annotated with the output (as seen on ideone.com): print "100" < "2" # True print "5" > "9" # False print "100" < 2 # False print 100 < "2" # True print 5 > "9" # False print "5" > 9 …
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
175
votes
10 answers

Why doesn't print work in a lambda?

Why doesn't this work? lambda: print "x" Is this not a single statement, or is it something else? The documentation seems a little sparse on what is allowed in a lambda...
Anycorn
  • 50,217
  • 42
  • 167
  • 261
166
votes
4 answers

What is the difference between isinstance('aaa', basestring) and isinstance('aaa', str)?

a='aaaa' print isinstance(a, basestring)#true print isinstance(a, str)#true
zjm1126
  • 63,397
  • 81
  • 173
  • 221