Yezi technology python3 code is compatible with python2

Time:2020-3-31

Yezi electric competition data official website revised https://www.xxe.io/new debut
Python3 code is compatible with python2
1. Use the future feature
Each new version of Python will add new features or make changes to the original features. Some changes are not compatible with the old version, that is, the code running normally in the current version may not run normally in the next version.

Python provides the “future” module to import the features of the next new version into the current version, so we can test some features of the new version in the current version.

For example, if you use the print (“XXX”) of python3 in python2.7, you need to reference the future module at the beginning of the file. Otherwise, an error is reported:

SyntaxError: from future imports must occur at the beginning of the file
1

Using higher version print and division

from future import print_function
from future import division
1
2
3
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import future
print ‘aaa’
aaa
print(aa)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘aa’ is not defined
print(“aa”)
aa
print(‘aa’)
aa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2. Use script to convert py2 to PY3
Convert python2 code to python3 code. Most of the answers on the network are through tools / scripts / 2to3.py script in the python installation directory;

If it is under Windows system, 2to3.py is under [Python or Anaconda installation directory] scripts directory;

However, in Ubuntu 16.04 system, there is no 2to3.py file in Python 3.5.2 installed through apt get install.
In the / usr / bin / directory, there are several 2to3 commands that can be called directly.

2to3 -w /path/a/file.py

1
2
3
4
5
6
7
Parameters of 2to3 command:

Usage: 2to3 [options] file|dir …

Options:
-h, –help show this help message and exit
-d, –doctests_only Fix up doctests only
-f FIX, –fix=FIX Each FIX specifies a transformation; default: all
-j PROCESSES, –processes=PROCESSES

                    Run 2to3 concurrently

-x NOFIX, –nofix=NOFIX

                    Prevent a transformation from being run

-l, –list-fixes List available transformations
-p, –print-function Modify the grammar so that print() is a function
-v, –verbose More verbose logging
–no-diffs Don’t show diffs of the refactoring
-w, –write Write back modified files
-n, –nobackups Don’t write backups for modified files
-o OUTPUT_DIR, –output-dir=OUTPUT_DIR

                    Put output files in this directory instead of
                    overwriting the input files.  Requires -n.

-W, –write-unchanged-files

                    Also write files even if no changes were required
                    (useful with --output-dir); implies -w.

–add-suffix=ADD_SUFFIX

                    Append this string to all output filenames. Requires
                    -n if non-empty.  ex: --add-suffix='3' will generate
                    .py3 files.

Recommended Today

Taghelper under. Net core MVC

. net web developers must have stepped into the pit during the development process. They clearly modified JS files, but when they deployed them to the production environment, the customer feedback said, “it’s still wrong.”.. Then I went to the server to read the file, which has been updated. Experienced coder may have thought that […]