I'm going to start on a long (~1-year) programming project in Python. I want to use wxPython for my GUI (supports 2.6), but I also want to use 3.1 for the rest of the project (to start using the 3.x syntax).
Is there any way for me to design a project that mixes 2.x and 3.x modules? Or should I just bite the bullet and use either 2.x (preferred, since I really want to learn wxPython) or 3.x throughout?
Thanks,
Mike
You should use python 2.7 (a release candidate is expected in the next days) that is very close to python 3.1 and to code taking care of no using deprecated features. There is a recent version of wxpython for python 2.7.
After wxpython gets 3.1-3.2 builds, conversion of the code should not be too hurting.
Still, wxpython has no deadline for the transition :-(
Another option is to use pyQt that already has builds for python 3.1
Python 2 and 3 are not that different. If you have learned Python 2 well, it will be a matter of minutes to get acquainted with Python 3. The official recommendation says that you should use Python 2.6 (the current version) and try to be forward-compatible. Python 3 is currently not an option for large projects as virtually none of the popular packages have been translated yet. But development of Python 2 and 3 will continue in parallel for a long time, so you won't lose much by not using Python 3. You can import many syntactical features of 3 (Unicode string literals, division, print function, absolute imports) using the __future__ module, and the standard library remains mostly the same. So I'd recommend using Python 2.
mixing the ability to use wxPython (2.x) + with learning new syntax (3.x)
Don't "mix".
Write Python 2. Get it to work.
Play with Python 3 separately. Don't "mix".
When the various packages and modules are available in Python 3, use the 2to3 conversion to create Python 3. You'll find some small problems. Fix your Python 2 so that your package works in Python 2 and also works after the conversion.
Then, you can drop support for Python 2 and focus on Python 3.
Don't "mix".
Related
If I'm writing a python package in python 3.6, then how do I ensure my code can be downloaded and ran in other python 3 environments, like python 3.5?
What makes some python packages (e.g. Tensorflow) compatible with all python 3 minor versions, while other python packages (e.g. OpenAI Gym) only compatible with Python 3.5?
Finally: if my goal is to write code that is compatible for Python 3.5 and 3.6, then would it be better to just use a python 3.5 environment?
The glib-but-true answer:
Test your 3.6 code with those other versions of Python.
If you want to see if something runs correctly in environment X, there's no substitute for actually running it in environment X.
Tox
is a Python testing framework designed to do exactly this on your workstation.
Something like this is often part of a larger
continuous integration
framework or service, which might be hosted on a remote server.
Writing your code in the earliest syntax you need to support is always a good idea, but it's not enough.
You still need to test later versions, because functions or classes can change, and even bug-fixes can break your code if you were unwittingly depending on their behavior.
As for why some packages don't work under a specific minor version, the most likely reason is that they use a Python language feature that was introduced later.
(Possibly it's one of their dependencies that requires the language feature.)
This can include language syntax changes like
Python 3.5's
# matrix-multiplication operator,
all the way down to seeming trivia like
Python 3.1's
printing commas as thousands-separators,
which is still enough to raise an exception.
It's actually a bit more complicated than just
"supports version x or greater",
because there are some gaps in Python's history.
The most likely headache is the u'' Unicode literal syntax from Python 2.
It was removed in Python 3.0...
and then
restored in Python 3.3,
after its absence caused more grief than expected.
That one change means any script with a u'Unicode literal' could work under Python 2.7 and 3.3+ while not working under Python 3.0, 3.1, or 3.2.
The Python documentation is very good about keeping track of when a feature was introduced.
For instance, the first thing the
typing module
tells you is:
26.1. typing - Support for type hints
New in version 3.5.
A Ctrl-F style search for "new in" or "changed in" will generally turn up all the most important changes.
Recently I was working on my Django web app, when I discovered that for some reason Django only worked when I used a python 2.x. Interpreter, but all the while I had thought it had been configured for Python 3 and thus was coding like so.
So I tested it with print(sys.version()) and was surprised when Python 2.7 came out. After a little digging I discovered that Django uses the six module. My first question is why does Django use this? Is there any reason other than just making it easier on the programmer? My second question is, since I've been treating it like Python 3 should I go back and change my code so that it is pure Python 2? Or does it not matter?
P.s. I kinda understand how six works, but it would be great to know a little more about it.
Thanks.
Django 1.5 is the first version of Django to support Python 3. The same code runs both on Python 2 (≥ 2.6.5) and Python 3 (≥ 3.2), thanks to the six compatibility layer.
Writing compatible code is much easier if you target Python ≥ 2.6. Django 1.5 introduces compatibility tools such as django.utils.six, which is a customized version of the six module. For convenience, forwards-compatible aliases were introduced in Django 1.4.2. If your application takes advantage of these tools, it will require Django ≥ 1.4.2.
For More read through Porting to Python 3
Which Python version your system uses has nothing whatsoever to do with the six module. That's something that Django uses internally to be able to work with both 2.7 and 3.x.
If you want to use Python 3 locally, you need to configure your system to do so. That might just mean creating your virtualenv with Python 3, for example.
I wrote some code in Python 2.7.2 and now I may need to switch to 3.4.3, but my code breaks (simply print statements right now, but who knows what else). Is it possible to write the syntax in such a way that it will be compliant with both 2.7.2 and 3.4.3?
I am just starting out with Python and don't want to build habits with one flavor and then have to relearn things with another version later.
Yes, but depending on your code. You have lot of options:
Use from __future__ import ... (making your code work with Python 2 & 3 - see e.g. this and this)
Use modernize
Use six
Yes, Python code can be compatible with both Python 2 and 3.
There are 3 options how to do it:
keep the source code itself compatible with both Python 2 and 3; this is the most used way
use separate code for Python 2 and 3; generally not very useful, but maybe acceptable for some low-level code. For example CherryPy wsgiserver (notice the files wsgiserver2.py and wsgiserver3.py).
write code in either Python 2 or Python 3 and use automatic tool like 2to3 or 3to2 for translation to the other version.
When Python 3 was first released the 2to3 way was the preferred one. You can find this in What’s New In Python 3.0: "It is not recommended to try to write source code that runs unchanged under both Python 2.6 and 3.0." But times have changed since then. Some things were introduced that made writing code for both Python 2 and 3 much easier - for example the u"" syntax in Python 3.3. Also the tool 3to2 did not get much updates since 2010...
When writing code that works unchanged on both Python 2 and 3 some compatibility code with few simple ifs may be needed; look for example at flask/_compat.py. If this isn't sufficient then use six.
Some resources how to write code working on both Python 2 and 3:
http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/
http://python-future.org/compatible_idioms.html#essential-syntax-differences
http://python3porting.com/noconv.html
https://docs.python.org/3/howto/pyporting.html
The Python 2/3 problem applies mostly for libraries, shared code and Python 2 projects that are being upgraded to Python 3. If you are starting a project that is not a library then just go with Python 3 if possible.
There are already some great answers above. A quick fix for your print problems would be to make use of the future module which backports some Python 3 features to Python 2.
I'd recommend as a minimum writing you're Python 2.7 code with the new print function. To do this import the new print function from future. i.e.
from __future__ import print_function
You will now get syntax errors in Python 2 using print as:
print x
and will now have to do:
print(x)
Other solutions like 2to3 and six exist, but these might be a bit complicated at the moment, especially as you are learning Python.
How would I use 2 different python mods in the same file when both mods require different python versions? I am trying to write a program that uses pygame, which only works with 3.2 and pymssql, which only works with 3.3. I get errors if I try to run them both in the same file(on either python version), but not if I run them separately.
There is no simple solution for you. In my opinion, it is the pygame communitie's responsibility to make it work on Python 3.3. Python 3.3 has a higher potential to be widely distributed than Python 3.2. Also, "supporting Python 3" nowadays should mean supporting Python 3.3/3.4, and not only Python 3.2. I guess this is on the todo list of the pygame maintainers. If in doubt, you might want to ask on the corresponding forums/mailing list if supporting Python 3.3 is already planned.
If you are curious and not frightened, you might event want to dig into why pygame fails on Python 3.3 and start fixing issue by issue. I guess this effort will be highly appreciated.
Edit:
I was assuming that you did your research homework a bit :-) Looks like pygame is available for Python 3.3:
https://bitbucket.org/pygame/pygame/downloads
Thanks #batbrat.
I used Python a few years ago when 2 was the only version.
Now there is 2 and 3.
Was there a a reason 2 hasn't continued to upgrade and now there is a split into two different pythons? Like no one uses Python 2.5. They all use 2.7 or whatever it is. Why not just use 3? Obviously there's a reason, but I was unable to find it googling.
My second question is, which one should I use? I am assuming 3, but that's ONLY based on the higher number. I wonder why 2 is still around if 3 is out??? If there aren't any major bugs with 3, I am just going to use that as I don't reckon it much matters: either one would probably work for py QT.
Why not just use 3?
A lot of existing Python code is not compatible with Python 3 yet. If you need to use Django for example you are forced to use Python 2.x.
My second question is, which one should I use?
Python 3 and PyQt 4 recommendations
Python 3 is significantly different than Python 2.x, and breaks numerous libraries and likely a lot of other dependent code. Python 2.x will likely be around for quite a while, as libraries are ported and 3.0 is field-tested.
it costs a lot to upgrade from python 2.x to python 3, that's why my project still uses python 2.7
The latest Ubuntu doesn't ship with Python 3. 12.04 will but that's not out yet.
OpenBSD doesn't have a package for Python 3, either.
Some people may just not have Python 3 on their platform and don't care to compile.