Can I run a python 3 script on python 2 environment? [duplicate] - python

This question already has an answer here:
Why is Python 3 not backwards compatible? [closed]
(1 answer)
Closed 5 years ago.
Ok, so I'm creating a script on python 3 to do some silent installations.
My problem is that I do not know if the server is going to have python 3 available. Its possible that it has by default python 2 or some Unix Engineer installs python 3.
Now, my question is. Is it possible to run a python 3 script on a python 2?
Should I export the modules I need?
Thanks

Kindof. There is the six module. Its goal is
Six is a Python 2 and 3 compatibility library. It provides utility
functions for smoothing over the differences between the Python
versions with the goal of writing Python code that is compatible on
both Python versions.
It won't do you any good if you need python 3 functionality. Its easy to install python 3 next to python 2 on most systems, so there really isn't much reason to try to struggle with it, though.

I recommend the python-future package for writing code that is compatible with Python 2.7 and Python 3.3+. It is somewhat more convenient than six and allows you to simply write idiomatic Python 3 almost everywhere.

Related

Automatic Python version compatability checker? [duplicate]

This question already has answers here:
How do I find out all previous versions of python with which my code is compatible
(4 answers)
How to detect minimum version of python that a script required
(2 answers)
Closed 1 year ago.
I am developing a Python package. It occurred to me that as my codebase changes that it would be useful to be able to automatically check what versions of Python are compatible. In general I could see this being a hard problem, but it seems to me like a naive approach that only looks at core syntax (f-formatting of strings, type hints, etc) to give an estimate of the earliest compatible version would still be useful.
Is there an automatic way to do this?
write tests that use your code from the package
Decide what versions of python you to support
set up continuous integration (CI) that runs your tests on the the those versions of python you support.

How does python handle different python versions for code & imported custom libraries [duplicate]

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.

Confusion transitioning from Python 2 to Python 3: Why support both?

Context: migrating Python 2 app to Python 3
In the docs here: https://docs.python.org/3/howto/pyporting.html
They mention:
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)
If you are no longer blocked by your source code nor your dependencies to fully transition to Python 3, why would you continue to also support Python 2?
Is that only a consideration for when you might have users on both Python 2 and Python 3?
If nothing imports my Python app, then there's no risk in fully migrating to Python 3 and dropping Python 2 support, correct?
If you are no longer blocked by your source code nor your dependencies to fully transition to Python 3, why would you continue to also support Python 2?
Nowadays you would probably not bother supporting Python 2 in a stand-alone app, but when that document was written, Python 2 was the norm, so supporting both versions was essential for widely used applications and libraries.
Is that only a consideration for when you might have users on both Python 2 and Python 3?
Yes
If nothing imports my Python app, then there's no risk in fully migrating to Python 3 and dropping Python 2 support, correct?
It depends on the user base that your app targets. You can expect most personal / consumer users to have Python 3 on their machines, or to be able to install it if it is not present or not the default Python (though note installing Python 3 may non-trivial for non-technical users).
Commercial or institutional users may be required to use operating systems that don't have Python 3 installed, and may not have the rights to install Python 3 themselves. In this case you would need to support Python 2 until these organisations upgrade to a suitable OS version.
You missed that the document starts with the assumption you want to support both:
With Python 3 being the future of Python while Python 2 is still in active use, it is good to have your project available for both major releases of Python. This guide is meant to help you figure out how best to support both Python 2 & 3 simultaneously.
That's the premise of the document, and it is only natural that it then tells you how to keep your code compatible. Yes, the Python core team will end support entirely in a a short few months time, but that doesn't mean that this document was written with that in mind.
You can use the document to port to Python 3, and then drop Python 2 support entirely, yes, that's fine too.
Yeah I think they are still supporting that keeping legacy Python 2 code in mind.
python 2 will be left not updated very soon so it's better to consider shifting to 3
This topic is particularly germane if you have developed code for the Python Package Index (PyPI) in Python 2 and are porting it to support both Python 2 and Python 3. You will have a "customer base" for both versions for at least the short foreseeable future.

Running multiple pythons

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.

Why is python so outdated on Ubuntu? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm new to programming as well as Linux. Will using Python 2.7.6 as opposed to the latest version affect
my learning?
*nix distributions use Python for many of their core system utilities, and as a result, they are a bit conservative on moving to new versions, especially major non-backward-compatible versions like Python 3. You should have a Python 3 interpreter available on Ubuntu though, you just have to run python3 from the terminal instead of just python. That said, for introductory programming, differences between 2 and 3 are negligible (to say nothing of differences between minor versions), so you should be fine with whatever interpreter you have access to.
Python 3 has improvements that are not compatible with Python 2, so Python 2 is still needed for a lot of software that hasn't been upgraded.
You can install Python 3 alongside Python 2.
For someone who's just learning, it makes sense to use Python 3 unless you're in a class or company that's using Python 2. The improvements are worthwhile and easier to work with. It's not difficult to apply knowledge of one to the other, but better to focus on one of them while learning.
Both python (Python 2) and python3 (Python 3) are installed on Ubuntu 14.04 by default. For both Ubuntu and Debian, we have ongoing project goals to make Python 3 the default, preferred Python version in the distros.
There are many scripts that were written for Python 2 that expect that python refers to Python 2 interpreter. Run python3 to get Python 3 interpreter.

Categories