top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python 2-3 compatibility

0 votes
173 views

I have a Python script that I wrote to support a project that I work on (that runs primarily on Unix OSes). Given its support role in this package, this script should not introduce any other dependencies. As a result, I wrote the script in Python 2, since every Linux currently ships with 2.4--2.7 as its system Python (RHEL 5, still popular in my field, ships with 2.4).
However, Ive heard news that Ubuntu is migrating to Python 3 soon (next release??), and thats a platform we actively try to support due to its popularity. Ive tried writing the code to support both 2 and 3 as much as possible, but with priority put on supporting 2.4 over 3.
Now that Python 3-compatibility is about to become more important, Im looking for a way to catch and store exceptions in a compatible way. Because Python 2.4 and 2.5 dont support the except Exception as err:
syntax, Ive used except Exception, err:
Is there any way of getting this effect in a way compatible with Py2.4 and 3.x? Of course I could duplicate every module with 2to3 and do sys.version_info-conditional imports, but Id rather avoid duplicating all of the code if possible.

posted Jun 2, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

From http://stromberg.dnsalias.org/~dstromberg/Intro-to-Python/Python%202%20and%203.pdf [1] :
Try/Except: both 2.x and 3.x
try: print(1/0)except ZeroDivisionError: extra = sys.exc_info()[1] print(oops)

answer Jun 2, 2013 by anonymous
Similar Questions
+2 votes

I have two Questions:

  1. Could Python 3.2, when compiled against OpenSSL 1.0.0j, be affected by the poodle bug? https://www.openssl.org/~bodo/ssl-poodle.pdf

  2. If yes - are the following OpenSSL versions approved for use with Python 3.2:
    OpenSSL 0.9.8zf
    OpenSSL 1.0.0r
    OpenSSL 1.0.1m
    OpenSSL 1.0.2a

+2 votes

I am working on drawing map from shape file in Python 3.2 basemap. But, the longitude values at the bottom axis are only shown partially. Also, all latitude values are missing.

Here is my python code.

import shapefile as sf
import sys
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.basemap import Basemap

 map = Basemap(projection='stere', lon_0=-106.4, lat_0= 31.9, lat_ts = 31.9, 
 llcrnrlat=31.7, urcrnrlat= 31.85, 
 llcrnrlon=-106.5 , urcrnrlon= -106.1, 
 rsphere=6371200., resolution='l', area_thresh=1000)

 plt.figure(num=None, figsize=(10, 8), dpi=80, facecolor='w', edgecolor='k')

parallels = np.arange(31.7, 31.85, 0.25)

map.drawparallels(parallels, labels=[0, 0, 0, 1] , fontsize=10, labelstyle='+/-', dashes=[2, 2])

meridians = np.arange (-106.5, -106.1, 0.25)

map.drawmeridians(meridians, labels=[0, 0, 0, 1], fontsize=10, labelstyle='+/-' , dashes=[2, 2])

No matter how I changed the labels, the latitude/longitude legend values are still missing.

+3 votes

The http://www.python.org site says that the future is Python 3, yet whenever I try something new in Python, such as Tkinter, everything seems to default to Python 2.

By this I mean that, whenever I find that I need to install another package, it shows up as Python 2 unless I explicitly specify Python 3.

What's the deal? If I want to make a distributable software package, should it be 2 or 3?

+3 votes

Here is what I have tried:

root@secure [~]# which python3
/usr/bin/python3

root@secure [~]# which pip
/usr/bin/pip

root@secure [~]# yum install pip3
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.secrel.com.br
 * epel: mirror.imt-systems.com
 * extras: centos.secrel.com.br
 * remi: mirror5.layerjet.com
 * updates: mirrors.ucr.ac.cr
Setting up Install Process
No package pip3 available.
Error: Nothing to do
...