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
838
votes
8 answers

Creating an empty Pandas DataFrame, and then filling it

I'm starting from the pandas DataFrame documentation here: Introduction to data structures I'd like to iteratively fill the DataFrame with values in a time series kind of calculation. I'd like to initialize the DataFrame with columns A, B, and…
Matthias Kauer
  • 9,697
  • 5
  • 17
  • 19
605
votes
12 answers

Get exception description and stack trace which caused an exception, all as a string

How to convert a caught Exception (its description and stack trace) into a str for external use? try: method_that_can_raise_an_exception(params) except Exception as e: print(complete_exception_description(e))
bluish
  • 26,356
  • 27
  • 122
  • 180
540
votes
30 answers

How do I create test and train samples from one dataframe with pandas?

I have a fairly large dataset in the form of a dataframe and I was wondering how I would be able to split the dataframe into two random samples (80% and 20%) for training and testing. Thanks!
tooty44
  • 6,829
  • 9
  • 27
  • 39
527
votes
6 answers

Converting dictionary to JSON

r = {'is_claimed': 'True', 'rating': 3.5} r = json.dumps(r) file.write(str(r['rating'])) I am not able to access my data in the JSON. What am I doing wrong? TypeError: string indices must be integers, not str
sheetal_158
  • 7,391
  • 6
  • 27
  • 44
515
votes
20 answers

How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"

as3:~/ngokevin-site# nano content/blog/20140114_test-chinese.mkd as3:~/ngokevin-site# wok Traceback (most recent call last): File "/usr/local/bin/wok", line 4, in Engine() File "/usr/local/lib/python2.7/site-packages/wok/engine.py", line…
fisherman
  • 10,739
  • 5
  • 20
  • 16
476
votes
4 answers

How to sort a dataFrame in python pandas by two or more columns?

Suppose I have a dataframe with columns a, b and c, I want to sort the dataframe by column b in ascending order, and by column c in descending order, how do I do this?
Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76
462
votes
12 answers

How do I install the yaml package for Python?

I have a Python program that uses YAML. I attempted to install it on a new server using pip install yaml and it returns the following: $ sudo pip install yaml Downloading/unpacking yaml Could not find any downloads that satisfy the requirement…
harperville
  • 6,921
  • 8
  • 28
  • 36
454
votes
10 answers

How to get current time in python and break up into year, month, day, hour, minute?

I would like to get the current time in Python and assign them into variables like year, month, day, hour, minute. How can this be done in Python 2.7?
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
446
votes
17 answers

How to uninstall Python 2.7 on a Mac OS X 10.6.4?

I want to completely remove Python 2.7 from my Mac OS X 10.6.4. I managed to remove the entry from the PATH variable by reverting my .bash_profile. But I also want to remove all directories, files, symlinks, and entries that got installed by the…
Jan Deinhard
  • 19,645
  • 24
  • 81
  • 137
442
votes
6 answers

How can I selectively escape percent (%) in Python strings?

I have the following code test = "have it break." selectiveEscape = "Print percent % in sentence and not %s" % test print(selectiveEscape) I would like to get the output: Print percent % in sentence and not have it break. What actually happens: …
jondykeman
  • 6,172
  • 3
  • 23
  • 22
438
votes
47 answers

urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error

I am getting the following error: Exception in thread Thread-3: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File…
user3724476
  • 4,720
  • 3
  • 15
  • 20
434
votes
3 answers

Delete a dictionary item if the key exists

Is there any other way to delete an item in a dictionary only if the given key exists, other than: if key in mydict: del mydict[key] The scenario is that I'm given a collection of keys to be removed from a given dictionary, but I am not certain…
Simon Hughes
  • 4,739
  • 4
  • 19
  • 12
428
votes
7 answers

How to write a Python module/package?

I've been making Python scripts for simple tasks at work and never really bothered packaging them for others to use. Now I have been assigned to make a Python wrapper for a REST API. I have absolutely no idea on how to start and I need help. What I…
yowmamasita
  • 4,810
  • 3
  • 17
  • 17
424
votes
21 answers

bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

... soup = BeautifulSoup(html, "lxml") File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 152, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to…
user3773048
  • 5,839
  • 4
  • 18
  • 22
403
votes
15 answers

(13: Permission denied) while connecting to upstream:[nginx]

I am working with configuring Django project with Nginx and Gunicorn. While I am accessing my port gunicorn mysite.wsgi:application --bind=127.0.0.1:8001 in Nginx server, I am getting the following error in my error log file; 2014/05/30 11:59:42…
Mulagala
  • 8,231
  • 11
  • 29
  • 48
1
2 3
99 100