Questions tagged [python-2.7]

Python 2.7 is the last major version in the 2.x series, and is no longer maintained since January 1st 2020. Use the generic [python] tag on all Python questions. Do not add this tag simply to convey the version of Python you're using, unless the question concerns an issue specific to Python 2.7.

Documentation for Python 2.7.

For an explanation as to why it's the last 2.x release, see PEP 404.

The End-of-life date for Python 2.7 was set as the 1st of Jan 2020, see PEP 373. The last release of Python 2.7 was 2.7.18, released April 20, 2020.

This guide explains how to choose a Python version for development. All new projects should now target Python 3 unless you have external requirements which still force you to stay on the old version.


Tagging recommendation:

Use the tag for all Python related questions. If you believe your question includes issues where the version incompatibilities between Python 2.x and Python 3.x are relevant, then add the or in addition to the main tag. 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.

95376 questions
259
votes
10 answers

How can I read inputs as numbers?

Why are x and y strings instead of ints in the below code? (Note: in Python 2.x use raw_input(). In Python 3.x use input(). raw_input() was renamed to input() in Python 3.x) play = True while play: x = input("Enter a number: ") y =…
user2509848
250
votes
6 answers

Why isn't my Pandas 'apply' function referencing multiple columns working?

I have some problems with the Pandas apply function, when using multiple columns with the following dataframe df = DataFrame ({'a' : np.random.randn(6), 'b' : ['foo', 'bar'] * 3, 'c' : np.random.randn(6)}) and the…
Andy
  • 9,483
  • 12
  • 38
  • 39
248
votes
6 answers

AttributeError: 'datetime' module has no attribute 'strptime'

Here is my Transaction class: class Transaction(object): def __init__(self, company, num, price, date, is_buy): self.company = company self.num = num self.price = price self.date = datetime.strptime(date,…
Michael
  • 15,386
  • 36
  • 94
  • 143
248
votes
12 answers

Writing a Python list of lists to a csv file

I have a long list of lists of the following form --- a = [[1.2,'abc',3],[1.2,'werew',4],........,[1.4,'qew',2]] i.e. the values in the list are of different types -- float,int, strings.How do I write it into a csv file so that my output csv file…
user1403483
247
votes
14 answers

Python - Extracting and Saving Video Frames

So I've followed this tutorial but it doesn't seem to do anything. Simply nothing. It waits a few seconds and closes the program. What is wrong with this code? import cv2 vidcap = cv2.VideoCapture('Compton.mp4') success,image = vidcap.read() count…
Rontron
  • 3,963
  • 7
  • 26
  • 44
239
votes
13 answers

How to convert a string to utf-8 in Python

I have a browser which sends utf-8 characters to my Python server, but when I retrieve it from the query string, the encoding that Python returns is ASCII. How can I convert the plain string to utf-8? NOTE: The string passed from the web is already…
Bin Chen
  • 61,507
  • 53
  • 142
  • 183
230
votes
35 answers

ImportError: numpy.core.multiarray failed to import

I'm trying to run this program import cv2 import time cv.NamedWindow("camera", 1) capture = cv.CaptureFromCAM(0) while True: img = cv.QueryFrame(capture) cv.ShowImage("camera", img) if cv.WaitKey(10) == 27: …
user3090952
219
votes
1 answer

Python 'If not' syntax

I'm a bit confused about how/why so many python developers use if not in their conditional statements. for example, lets say we had a function, def foo(bar = None): if not bar: bar = 2 But why go about this way? I mean, wouldn't doing…
user1971598
219
votes
10 answers

Why do I get "'str' object has no attribute 'read'" when trying to use `json.load` on a string?

In Python I'm getting an error: Exception: (, AttributeError("'str' object has no attribute 'read'",), ) Given python code: def getEntries (self, sub): url =…
RobinJ
  • 5,022
  • 7
  • 32
  • 61
217
votes
11 answers

How to use pip with Python 3.x alongside Python 2.x

I installed Python 3.x (besides Python 2.x on Ubuntu) and slowly started to pair modules I use in Python 2.x. So I wonder, what approach should I take to make my life easy by using pip for both Python 2.x and Python 3.x?
theta
  • 24,593
  • 37
  • 119
  • 159
213
votes
5 answers

Making an API call in Python with an API that requires a bearer token

Looking for some help with integrating a JSON API call into a Python program. I am looking to integrate the following API into a Python .py program to allow it to be called and the response to be printed. The API guidance states that a bearer token…
user4657
  • 2,303
  • 2
  • 15
  • 9
210
votes
18 answers

"OSError: [Errno 1] Operation not permitted" when installing Scrapy in OSX 10.11 (El Capitan) (System Integrity Protection)

I'm trying to install Scrapy Python framework in OSX 10.11 (El Capitan) via pip. The installation script downloads the required modules and at some point returns the following error: OSError: [Errno 1] Operation not permitted:…
Luis U.
  • 2,500
  • 2
  • 17
  • 15
207
votes
2 answers

What does from __future__ import absolute_import actually do?

I have answered a question regarding absolute imports in Python, which I thought I understood based on reading the Python 2.5 changelog and accompanying PEP. However, upon installing Python 2.5 and attempting to craft an example of properly using…
Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
202
votes
10 answers

How do you create nested dict in Python?

I have 2 CSV files: 'Data' and 'Mapping': 'Mapping' file has 4 columns: Device_Name, GDN, Device_Type, and Device_OS. All four columns are populated. 'Data' file has these same columns, with Device_Name column populated and the other three columns…
atams
  • 2,739
  • 3
  • 16
  • 14
200
votes
9 answers

pip or pip3 to install packages for Python 3?

I have a Macbook with OS X El Captain. I think that Python 2.7 comes preinstalled on it. However, I installed Python 3.5 too. When I started using Python 3, I read that if I want to install a package, I should type: pip3 install…
Ammar Alyousfi
  • 4,112
  • 5
  • 31
  • 42