Python Code Portability
Python is a portable language. You can write your script locally and distribute it to other machines with the Python interpreter.
You do not need to worry about Python code irrespective of the operating system you are using whether it is Linux or Windows Computer system.
The only thing you need to care about the version of Python you are using. Python 2 version and Python version 3 is different. Python 2 is incompatible with Python 3 version.
On December 2008, Python released version 3.0. This version was mainly released to fix problems that exist in Python 2. The nature of these changes is such that Python 3 was incompatible with Python 2. It is backward incompatible Some features of Python 3 have been backported to Python 2.x versions to make the migration process easy in Python 3.
As a result, for any organization that was using Python 2.x version, migrating their project to 3.x needed lots of changes. These changes not only relate to projects and applications but also all the libraries that form part of the Python ecosystem.
Step to keep in mind for porting Python code from one computer to another
So, technically, it is simple if the original code written in version Python 3 is ported to any other computer with Python version 3 will not cause any problem.
If the original code is written in Python 2 and it is being ported to the computer that uses Python 3 version, you need to modify the code.
The other important thing to remember is, the libraries you have used in the original code which are installed on the source computer should also be installed on the destination computer to which the code is being ported.
The Short Explanation
To make your project be single-source Python 2/3 compatible, the basic steps are:
- Only worry about supporting Python 2.7
- Make sure you have good test coverage (coverage.py can help;
pip install coverage) - Learn the differences between Python 2 & 3
- Use Futurize (or Modernize) to update your code (e.g.
pip install future) - Use Pylint to help make sure you don’t regress on your Python 3 support (
pip install pylint) - Use caniusepython3 to find out which of your dependencies are blocking your use of Python 3 (
pip install caniusepython3) - Once your dependencies are no longer blocking you, use continuous integration to make sure you stay compatible with Python 2 & 3 (tox can help test against multiple versions of Python;
pip install tox) - Consider using optional static type checking to make sure your type usage works in both Python 2 & 3 (e.g. use mypy to check your typing under both Python 2 & Python 3).