Questions tagged [python-2.6]

For issues that are specific to Python 2.6. If your question applies to Python in general, use the tag [python].

Use this tag if your question is specifically about Python 2.6. If your question applies to Python in general, use the tag . If your question applies to Python 2.x but not to Python 3, use the tag . If you aren't sure, tag your question and mention which version you're using in the body of your question.

1318 questions
611
votes
14 answers

How to make a timezone aware datetime object in Python?

What I need to do I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for…
Mark Tozzi
  • 8,455
  • 4
  • 20
  • 30
512
votes
32 answers

bash: pip: command not found

I downloaded pip and ran python setup.py install and everything worked just fine. The very next step in the tutorial is to run pip install but before it even tries to find anything online I get an error "bash: pip: command not…
Trindaz
  • 14,751
  • 20
  • 74
  • 103
464
votes
4 answers

Get all object attributes in Python?

Is there a way to get all attributes/methods/fields/etc. of an object in Python? vars() is close to what I want, but it doesn't work unless an object has a __dict__, which isn't always true (e.g. it's not true for a list, a dict, etc.).
user541686
  • 189,354
  • 112
  • 476
  • 821
421
votes
12 answers

Suppress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6

I am writing scripts in Python2.6 with use of pyVmomi and while using one of the connection methods: service_instance = connect.SmartConnect(host=args.ip, user=args.user, …
Patryk
  • 18,244
  • 37
  • 110
  • 212
420
votes
16 answers

Is there a way to perform "if" in python's lambda?

In Python 2.6, I want to do: f = lambda x: if x==2 print x else raise Exception() f(2) #should print "2" f(3) #should throw an exception This clearly isn't the syntax. Is it possible to perform an if in lambda and if so how to do it?
Guy
  • 12,478
  • 25
  • 61
  • 86
256
votes
5 answers

Python try...except comma vs 'as' in except

What is the difference between ',' and 'as' in except statements, eg: try: pass except Exception, exception: pass and: try: pass except Exception as exception: pass Is the second syntax legal in 2.6? It works in CPython 2.6 on…
Peter Graham
  • 10,135
  • 7
  • 36
  • 41
203
votes
12 answers

How to calculate the time interval between two time strings

I have two times, a start and a stop time, in the format of 10:33:26 (HH:MM:SS). I need the difference between the two times. I've been looking through documentation for Python and searching online and I would imagine it would have something to do…
Hannah
  • 2,337
  • 4
  • 16
  • 10
175
votes
10 answers

How to remove all characters after a specific character in python?

I have a string. How do I remove all text after a certain character? (In this case ...) The text after will ... change so I that's why I want to remove all characters after a certain one.
Solihull
  • 5,299
  • 5
  • 20
  • 12
149
votes
9 answers

How to convert a set to a list in python?

I am trying to convert a set to a list in Python 2.6. I'm using this syntax: first_list = [1,2,3,4] my_set=set(first_list) my_list = list(my_set) However, I get the following stack trace: Traceback (most recent call last): File "", line…
gath
  • 21,746
  • 35
  • 91
  • 120
140
votes
8 answers

Visibility of global variables in imported modules

I've run into a bit of a wall importing modules in a Python script. I'll do my best to describe the error, why I run into it, and why I'm tying this particular approach to solve my problem (which I will describe in a second): Let's suppose I have a…
Nubarke
  • 1,574
  • 2
  • 10
  • 9
104
votes
6 answers

Why does sys.exit() not exit when called inside a thread in Python?

This could be a stupid question, but I'm testing out some of my assumptions about Python and I'm confused as to why the following code snippet would not exit when called in the thread, but would exit when called in the main thread. import sys,…
Shabbyrobe
  • 11,310
  • 15
  • 55
  • 86
101
votes
6 answers

Any gotchas using unicode_literals in Python 2.6?

We've already gotten our code base running under Python 2.6. In order to prepare for Python 3.0, we've started adding: from __future__ import unicode_literals into our .py files (as we modify them). I'm wondering if anyone else has been doing…
Jacob Gabrielson
  • 29,844
  • 15
  • 44
  • 60
100
votes
3 answers

Pipe subprocess standard output to a variable

I want to run a command in pythong, using the subprocess module, and store the output in a variable. However, I do not want the command's output to be printed to the terminal. For this code: def storels(): a =…
Insomaniacal
  • 1,687
  • 3
  • 13
  • 10
94
votes
6 answers

Get class that defined method

How can I get the class that defined a method in Python? I'd want the following example to print "__main__.FooClass": class FooClass: def foo_method(self): print "foo" class BarClass(FooClass): pass bar = BarClass() print…
Jesse Aldridge
  • 7,134
  • 7
  • 41
  • 72
87
votes
9 answers

sort dict by value python

Assume that I have a dict. data = {1:'b', 2:'a'} And I want to sort data by 'b' and 'a' so I get the result 'a','b' How do I do that? Any ideas?
kingRauk
  • 1,159
  • 1
  • 10
  • 22
1
2 3
87 88