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
391
votes
21 answers

How to install PIL with pip on Mac OS?

I am trying to install PIL (the Python Imaging Library) using the command: sudo pip install pil but I get the following message: Downloading/unpacking PIL You are installing a potentially insecure and unverifiable file. Future versions of pip…
user3006710
  • 3,947
  • 2
  • 12
  • 3
389
votes
11 answers

Getting rid of \n when using .readlines()

I have a .txt file with values in it. The values are listed like so: Value1 Value2 Value3 Value4 My goal is to put the values in a list. When I do so, the list looks like this: ['Value1\n', 'Value2\n', ...] The \n is not needed. Here is my code: t…
TDNS
  • 4,125
  • 2
  • 17
  • 17
370
votes
25 answers

Could not find a version that satisfies the requirement tensorflow

I installed the latest version of Python (3.6.4 64-bit) and the latest version of PyCharm (2017.3.3 64-bit). Then I installed some modules in PyCharm (Numpy, Pandas, etc), but when I tried installing Tensorflow it didn't install, and I got the error…
Martin W
  • 4,548
  • 3
  • 16
  • 27
365
votes
5 answers

Python Requests - No connection adapters

I'm using the Requests: HTTP for Humans library and I got this weird error and I don't know what is mean. No connection adapters were found for '192.168.1.61:8080/api/call' Anybody has an idea?
Azd325
  • 5,752
  • 5
  • 34
  • 57
356
votes
16 answers

How to remove \xa0 from string in Python?

I am currently using Beautiful Soup to parse an HTML file and calling get_text(), but it seems like I'm being left with a lot of \xa0 Unicode representing spaces. Is there an efficient way to remove all of them in Python 2.7, and change them into…
zhuyxn
  • 6,671
  • 9
  • 38
  • 44
301
votes
13 answers

In Python, how do you convert a `datetime` object to seconds?

I have a bunch of datetime objects and I want to calculate the number of seconds since a fixed time in the past for each one (for example since January 1, 1970). import datetime t = datetime.datetime(2009, 10, 21, 0, 0) This seems to be only…
Nathan Lippi
  • 4,967
  • 5
  • 25
  • 29
300
votes
6 answers

What is the difference between json.load() and json.loads() functions

In Python, what is the difference between json.load() and json.loads()? I guess that the load() function must be used with a file object (I need thus to use a context manager) while the loads() function take the path to the file as a string. It is a…
MMF
  • 5,750
  • 3
  • 16
  • 20
277
votes
10 answers

How to display pandas DataFrame of floats using a format string for columns?

I would like to display a pandas dataframe with a given format using print() and the IPython display(). For example: df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890], index=['foo','bar','baz','quux'], …
Jason S
  • 184,598
  • 164
  • 608
  • 970
272
votes
3 answers

Split a python list into other "sublists" i.e smaller lists

I have a python list which runs into 1000's. Something like: data=["I","am","a","python","programmer".....] where, len(data)= say 1003 I would now like to create a subset of this list (data) by splitting the orginal list into chunks of 100. So, at…
JohnJ
  • 6,736
  • 13
  • 49
  • 82
269
votes
15 answers

input() error - NameError: name '...' is not defined

I am getting an error when I try to run this simple script: input_variable = input("Enter your name: ") print("your name is" + input_variable) Let's say I type in "dude", the error I am getting is: line 1, in input_variable =…
chillpenguin
  • 2,989
  • 2
  • 15
  • 18
267
votes
11 answers

Using both Python 2.x and Python 3.x in IPython Notebook

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython. I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and…
deltap
  • 4,176
  • 7
  • 26
  • 35
267
votes
13 answers

Python UTC datetime object's ISO format doesn't include Z (Zulu or Zero offset)

Why python 2.7 doesn't include Z character (Zulu or zero offset) at the end of UTC datetime object's isoformat string unlike JavaScript? >>> datetime.datetime.utcnow().isoformat() '2013-10-29T09:14:03.895210' Whereas in javascript >>>…
Murali Mopuru
  • 6,086
  • 5
  • 33
  • 51
266
votes
6 answers

What is a good practice to check if an environmental variable exists or not?

I want to check my environment for the existence of a variable, say "FOO", in Python. For this purpose, I am using the os standard library. After reading the library's documentation, I have figured out 2 ways to achieve my goal: Method 1: if "FOO"…
265
votes
20 answers

What is the result of % in Python?

What does the % in a calculation? I can't seem to work out what it does. Does it work out a percent of the calculation for example: 4 % 2 is apparently equal to 0. How?
orange
  • 5,297
  • 12
  • 50
  • 71
260
votes
27 answers

sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

I am using sklearn and having a problem with the affinity propagation. I have built an input matrix and I keep getting the following error. ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). I have…
Ethan Waldie
  • 2,799
  • 2
  • 12
  • 14