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!
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
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.
Related
How to use python 2 packages in python 3 project?
I have a Python 3 project, but I need some packages which are written in Python 2.
I do not want to rewrite these python-2 packages, so forking / 2to3 is not an option.
A lot of the previous questions concern using Python 2 modules in Python 3 projects. Here's the most complete explanation. It also works the other way around and suggests the following alternatives:
Using the subprocess module for requests and getting the response through the CLI. So far the most straightforward way to make cross-references.
python-fire can quickly add a CLI to a Python 3 library. You'll have a reasonable API to call from the Python 2 project.
3to2. This utility converts Python 3 code to Python 2. It remains unclear how it handles complex Python 3 projects with dependencies and Python 3-only features.
six and __future__. It seems both of these modules are intended for writing the code compatible with Python 2-3, not for using Python 3 libraries in Python 2 projects.
You can't. Any module you import in py3 codebase needs to be py3 compatible. If you can't make the upstream project do it for you, you'll have to do it yourself. As mentioned in the comments, 2to3 utility should help you with that.
I usually start all my scripts with the shebang line
#!/usr/bin/env python
However our production server has Python 2 as the default python, while all of our new scripts and programs are being built under Python 3. To help keep people from accidentally running the script with the default Python 2, I am considering switching all my shebangs from now on to this;
#!/usr/bin/env python3
On our server, python3 indeed points to Python 3, and our basic scripts will run correctly on it. However I am not clear if this is something specific to our installation, or if python3 is always available if Python 3 is installed?
I know this probably will not help a user who runs $ python myscript.py when the default Python is loaded, but its better than nothing and is clear enough in letting a user who inspects the script realize they are using the wrong Python version. Though now I also realize that, with Python being on version 3.8, a Python 4 is imminent... at the same time, I am not sure I am ready to embed code in every single script to check if Python >= 3 is loaded...
Yes, this is a safe bet.
PEP 394 recommends Python 3 be available under the binary name python3 and most Linux distributions follow this recommendation. In fact, this is the only name under which Python 3 has been available in most distributions (the only outlier being Arch Linux, but even that also provides a python3 binary), and plans to make the ‘plain’ python binary also refer to Python 3 have only been made quite recently. The article ‘Revisiting PEP 394’ on LWN.net has more details.
I believe that a python 3 version only install's python3 if there is already another version of python installed, no matter if it is a python 2 or python 3 version, because the standard python command would then not work properly for the new version of python.
But please correct me if I'm wrong!
I have python 2.7 installed. I want to use python 2.4 to run python code. Is it possible?
Either directly use the Python 2.4 interpreter to run it, or modify the programs she-bang line to point to the interpreter you wish to use.
Note that there's many things in common use in recent python (any/all, the 1 if 2 else 3 syntax, as well as major stdlib and language changes) that may cause your program to experience difficulties.
It's also worth noting that a lot of the common 3rd party modules require at least 2.5 and some of those are even dropping that and only guaranteeing 2.6+ compatibility.
You can install Python 2.4 as well. Any of the minor versions, 2.4, 2.5, 2.6, etc. can live side by side.
Code you write for 2.4 will also run on Python 2.7, albeit that you may hit some deprecation warnings. If you are using the 2.7 interpreter to write 2.4 code, you'll need to be careful that you don't use syntax and modules that have been added in newer Python versions.
To see what has been added, look at the What's new documentation; there is a document for each minor version:
What's new in Python 2.5
What's new in Python 2.6
What's new in Python 2.7
You specifically want to look for syntax changes and for new modules to avoid.
There are a few things that can bite you. Some syntactic changes have happened since 2.4, so you may get syntax errors. The standard library is bigger in 2.7, so you may have some things missing. The docs generally lists the version of python when things were added, and can be great help in making sure things will run on different python versions. Generally, syntax and libraries are forward compatible, so if you have to support 2.4, I would write using 2.4 and things should work with 2.7. The same is not true in reverse.
I am trying to set up Python on Windows 7. I haven't used this language before so it seems strange to me.
I've downloaded lastest Python release 3.2.2 from official site and upadate path variable.
However I still can't even run simplest program ever like this :
print 'Hello, world!'
It says that there is a syntax error and the last character ' is highlighted with red.
I don't know if my path variable has been set properly. Here is where I installed Python :
C:\Software\Python32
So I added such a variable : var name = PYTHONPATH , var value = C:\Software\Python32\Lib
Is there something with auto-completion and with errors/warnings details ( which line, hint what can be wrong ), for example like Eclipse or NetBeans OR should I use this installed Python IDLE GUI for delevoping or stuff like NotePad++ ?
Actually what is this Python shell for ? - I know that I can type in some arithmetic operations here and I will get results, but is it used for something more advanced ? ( Is it used when I am writting something bigger ? )
Could someone describe simple way to write and execute a program ( or script I a total beginner so I don't really know what it is going on here ) ?
In Python 3.2 you have to use print in the below manner. The parentheses are mandatory. (print became a function in Python 3)
print('Hello World')
As Venk stated, the print statement in Python 2.x has been replaced with print() in 3.x, so your statement should read
print('Hello World')
Since you're new, here some things you should know about Python versions:
Python currently comes in two flavors: Python 2.x and Python 3.x.
Python 2.x has been in development since the late '90s, so most existing codebases, frameworks, and libraries are written in this flavor of Python. Each successive version is backwards compatible, so, for example, all code written in Python 2.4 can be run with Python 2.4+. Its current revision is 2.7.2, which was released last year.
Python 3.x is considered the "future" of Python, and purposefully breaks a lot of the conventions, such as the print statement, in favor of a clearer, more explicit language. Most libraries are working to port over to Python 3.x, but since there are extensive changes in the structure of the language, most library maintainers have not yet been able to release a Python 3.x compatible version with the full features of the Python 2.x version of the library.
If you're developing now, you should learn Python 2.x; otherwise, it's recommended you learn Python 3.x.
To answer your other questions:
Python's native IDLE is an excellent IDE, but if you're looking for something more advanced, you may want to try out Eclipse's PyDev extension or PyCharm. I personally prefer PyCharm, since it doesn't keep giving me errors when I'm importing/using nonstandard Python libraries/frameworks.
In addition, Python's shell is used to interpret Python scripts (in the background) and for interactive interpreting (i.e., quick and dirty testing), and can execute code you type into it. The latter, however, is not recommended, as a single syntax error in multiple lines of code can force you to retype all the lines to fix a single bug.
Furthermore, all Python scripts end in .py, so if you can see the file extensions, you can convert a text file into a Python script, and run from shell by typing python path/to/file.py. Note, however, that you still have to write a valid Python script, or it will not run.
I suggest A Byte of Python. It'll take you through install, REPL, syntax, and the std library.
The python shell is for entering code interactively.
Try the following: http://docs.python.org/tutorial/
I've been using Python on Windows 7 for 3 years now, and I strongly recommend Notepad++ as your editor/interpreter. It is ideal for people who want to play around with the language and are learning. Notepad++ can be customized for almost any language and is free for Windows. Follow this link
and take a look at how to conveniently use Notepad++ to execute Python scripts.
Found some info on porting packages from python 2 to 3 using distribute setuptools in below link.
http://packages.python.org/distribute/python3.html
I have a C api which could be build using python 2.x, but i need to build it in python 3.x.
Can it be done using distribute.
Do anyone have idea on this?
No, it cannot be done using Distribute. Distribute just calls the 2to3 script in the build phase, but 2to3 can convert only between Python 2.x source files and Python 3.x source files. For the C API, you have to do it the hard way by manually tweaking your code to compile with both Python APIs.
A very incomplete list of C API changes between Python 2.x and Python 3.x is to be found here. The same document also outlines the major differences between Python 2.x and 3.x on the Python source code level.
distriubte uses Python's 2to3 tool to automatically (try to) convert Python 2 code to Python 3 code. However, that only works for code written in Python. C code needs to be ported by hand.
The good news is that Python's C API has not changed much between Python 2.6 and 3.1. The main difference is that Python 3 now uses Unicode for all strings and has a separate bytes type for handling raw binary data.