1

I recently upgraded to python 3.4 to use continuum tools but many of my scripts are written for 2.7. This can causes some errors; some are simple (like "print" now requires parentheses), but others are more complicated:

if struct.unpack("h", "\0\1")[0] == 1:
  defs.append(("WORDS_BIGENDIAN", None))

Yields the error:

  File "setup.py", line 302, in build_extensions
    if struct.unpack("h", "\0\1")[0] == 1:
    TypeError: 'str' does not support the buffer interface

Is there a way to run my python code as 2.x like you can with C++ (-std=c++11 etc) ? It's possible that many more errors will surface if I just solve this one. Thanks!

Emanuel Ey
  • 2,724
  • 5
  • 30
  • 38
so860
  • 408
  • 3
  • 12
  • I think not. You'll have to port it. But there are tools for that. – Bart Friederichs Dec 03 '14 at 16:37
  • 1
    There's the `2to3.py` converter that comes with python 3 that will attempt to update your code for you, for one. But there's probably work you'll have to do yourself as well. – g.d.d.c Dec 03 '14 at 16:38
  • @ACSutton Please remember to vote up useful answers and comments and to mark the answer that solves your problem as "Accepted". – Emanuel Ey Dec 03 '14 at 17:05
  • possible duplicate of [Using multiple versions of Python](http://stackoverflow.com/questions/20555517/using-multiple-versions-of-python) – simopopov Dec 03 '14 at 17:17
  • Thank you for linking to that answer! I had not seen it, but I still think this question is relevant for a few reasons: I need to maintain both versions; this is linux-specific; the answers provided here will be useful to others. – so860 Dec 03 '14 at 17:31

2 Answers2

1

If you have several versions installed, you can change the first line of your python script to explicitly use 2.x or 3.x:

For a python 2.x script:

#!/usr/bin/env python2

or, for a python 3.x script:

#!/usr/bin/env python3
Emanuel Ey
  • 2,724
  • 5
  • 30
  • 38
  • Can you confirm that this works? I get an error "bash: ./program_name.py: Permission denied" – so860 Dec 03 '14 at 17:11
  • Yes, I have just tried this in my shell. Note that this will only work if you have both python2 and python3 installed on your system. Similarly, when running python from a console you can explicitly run an interactive python session by calling `python2` or `python3`. – Emanuel Ey Dec 03 '14 at 17:13
  • Thanks, but I'm still getting the error. I'm trying to add the Image module to my Python 3.4 distribution, but in addition to not being explicitly supported in Python 3.4 it might not work at all... – so860 Dec 03 '14 at 17:18
  • Try running `which python2` in a console and use the returned path in the script's first line. – Emanuel Ey Dec 03 '14 at 17:23
  • Okay, your answer makes me re-think what I was trying to do, and I don't think my original goal is possible -- I can't use python2 to install a module for python3. I need the actual install script to be python3-callable. Does that sound right? – so860 Dec 03 '14 at 17:37
  • Hmm, that sounds like I'm missing some context... what do you mean with "install a module for python 3"? If by that you mean "use a script that automates an installation/setup task on an OS or VirtualEnv level" then I see no problem, but if you're referring to using a python2 module in a python3 script, that will probably not be possible at all, require some awkward hack, or just generally be a bad idea. – Emanuel Ey Dec 04 '14 at 11:36
0

Python 3 is really a different language than Python 2. There's no way to make the Python 3 interpreter run Python 2 code (unless that code doesn't happen to use any of the features that were changed).

You may want to read the guide to porting to Python 3 in the Python documentation. Here's a brief summary of the current recommendations:

  • If you only need to support Python 3 from now on (you don't need to maintain Python 2 compatibility), use the 2to3 tool to translate most of your code, then manually fix up whatever it has missed. There is lots of documentation that explains the changes between versions, if you haven't used Python 3 before.
  • If you're writing new code and need to be able to run it with both Python versions, write for Python 3 (or a common subset of 2 and 3) and backport to Python 2 as necessary.
  • If you have an existing Python 2 codebase and you want to run it on Python 3 without breaking Python 2 compatibility, use libraries like six and from future imports to help you port your code to a common subset of the two versions of Python. 2to3 and other tools like modernize will help you find the places you can improve things. Note that it's easier to make this work if you drop support for older version of Python 2.
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • It's unfortunate that this is the case. 2to3 seems to be a very imperfect tool. I'll play around with 'six' and 'fromfuture' to see if these help. – so860 Dec 03 '14 at 17:18