Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

import queue in Python 2 and Python 3

249 views
Skip to first unread message

Benjamin Chaney

unread,
Jun 30, 2017, 12:33:19 AM6/30/17
to
What is the best way to import the synchronized queue class that is
compatible with both Python2 and Python3. Right now I am using the
following:

>if sys.version_info < (3, 0):
> import Queue as queue
>else:
> import queue

This seems somewhat unpythonic. Is there a better way without
installing extra packages?

Thanks,
Ben

Chris Angelico

unread,
Jun 30, 2017, 12:38:19 AM6/30/17
to
try:
import queue
except ImportError:
# Python 2
import Queue as queue

ChrisA

Thomas Jollans

unread,
Jun 30, 2017, 4:35:12 AM6/30/17
to
What Chris said. If you find yourself doing a lot of this kind of thing,
you might want to use six, though, and do

from six.moves import queue

0 new messages