Using Different Versions of Python - Compatibility/Dependencies?

  • Thread starter WWGD
  • Start date
  • Tags
    Python
In summary: The divide operator in Python 3 is different from the divide operator in Python 2. You must use the // to integer divides. // is complementary to % for modular math.
  • #1
WWGD
Science Advisor
Gold Member
6,934
10,341
Hi All,
I am trying to learn versions 2,3 of Python ( long story). Just curious as to the "Dependency/Compatibility" issues between and within versions 2 and 3. Are packages/libraries in 3.x available for 3.x' where x'>x ? How about for version 2? Specifically, at this point, I am trying to do some Web Scraping. If I learn to use it with one version, how likely am I of being able to do the same or similar within an other
 
Technology news on Phys.org
  • #2
I am fairly new to Python also. I chose to go with 3 right away and a lot of examples use 2. However, I haven't had too many issues using version 2 examples with my version 3 code.
 
  • Like
Likes WWGD
  • #3
Borg said:
I am fairly new to Python also. I chose to go with 3 right away and a lot of examples use 2. However, I haven't had too many issues using version 2 examples with my version 3 code.
Have you been able to do Web Scraping with more than one version?
 
  • #4
Haven't done that. I've been focused on machine learning code.
 
  • Like
Likes WWGD
  • #5
Borg said:
Haven't done that. I've been focused on machine learning code.
And you have been able to go back-and-forth between versions?
EDIT: Do you use Graphlab or something else for ML?
 
  • #6
I haven't been doing that. I just said that I've had few problems using the version 2 examples in my version 3 environment. BTW, why do you want to use both versions anyway? I could understand if you had a large collection of version 2 code that you built but, if you're as new to Python as I am, you're better off just going with the newer version.
 
  • #7
Borg said:
I haven't been doing that. I just said that I've had few problems using the version 2 examples in my version 3 environment. BTW, why do you want to use both versions anyway? I could understand if you had a large collection of version 2 code that you built but, if you're as new to Python as I am, you're better off just going with the newer version.
I agree, it is just that I have seen jobs that look interesting that require 2 , some require 3. Just covering my bases. I will start with 2 with Anaconda, I guess, in the meantime become familiar with 3 and IDLE. What IDE do you use?
 
  • #8
If I saw a job that required version 2, that would tell me that they have a lot of legacy code that they need to maintain. Call me silly but I wouldn't want to be a janitor. :oldtongue:

As for my IDE, I've been coding everything in Jupyter.
 
  • Like
Likes phinds
  • #9
Borg said:
If I saw a job that required version 2, that would tell me that they have a lot of legacy code that they need to maintain. Call me silly but I wouldn't want to be a janitor. :oldtongue:

As for my IDE, I've been coding everything in Jupyter.
Do you have access to Graphlab? SFrames? How do you code, e.g., a regression without them?
 
  • #10
Haven't heard of that.
 
  • #11
Borg said:
Haven't heard of that.
So you just code, say a a regression without any additional packages? Sorry if I am being ignorant here, but I have to import other packages before doing any ML
 
  • #12
I'm using pip and conda installs to download everything locally. Keep in mind that I'm still learning Python and I'm not dealing with gigantic data sets at this point. My focus is on learning the details of ML algorithms and there's plenty I can do on a laptop for now.
 
  • Like
Likes scottdave and WWGD
  • #13
Borg said:
I'm using pip and conda installs to download everything locally. Keep in mind that I'm still learning Python. I'm not dealing with gigantic data sets at this point. My focus is on learning the details of ML algorithms and there's plenty I can do on a laptop for now.
Ok, I do things in a kind of disorganized way, bottom-up learning from a source, top-down by reading someone else's code and eventually comes together in my brain.
 
  • Like
Likes Borg
  • #14
Sorry, don't mean to drag this too long, but, would you consider any version of Python 3 before the latest, e.g., 3.4 instead of 3.7?
 
  • #15
If I needed to. I was using 3.6 for a while.
 
  • #16
WWGD said:
Specifically, at this point, I am trying to do some Web Scraping.

I have done web scraping with BeautifulSoup in both Python 2 and Python 3. There isn't much difference between the two. The kind of code you will need to write for web scraping will be pretty much the same in both versions.

https://pypi.org/project/beautifulsoup4/
 
  • Like
Likes Paul Colby, WWGD and Borg
  • #17
One key difference is the divide operator. In 2 its returns ints if given ints but in 3 it returns floats. You must use the // to integer divides. // is complementary to % for modular math.
 
  • Like
Likes WWGD
  • #18
jedishrfu said:
One key difference is the divide operator. In 2 its returns ints if given ints but in 3 it returns floats. You must use the // to integer divides. // is complementary to % for modular math.
Thanks. That also reminds me of the difference between raw input in 2 and input() in 3.
 
  • #19
I would suggest using 3 and not 2 unless the project calls for using 2 for legacy support.
 
  • Like
Likes WWGD
  • #20
jedishrfu said:
I would suggest using 3 and not 2 unless the project calls for using 2 for legacy support.
Thanks, but, what then, must one then move on to the next version/generation -- I guess Python 4 -- and so on indefinitely?
 
  • #21
Another was ord() returned the ascii code in 2 whereas in 3 you must use int() i think.
 
  • #22
This is the common issue developers always face ie use the latest or stick with what works. For major changes you have to weight program life with support for version it uses vs upgrading.
 
  • Like
Likes WWGD
  • #23
WWGD said:
must one then move on to the next version/generation -- I guess Python 4 -- and so on indefinitely?

Eventually, but not on any short time scale. Python 2.7, the last version of Python 2, was first released in 2010; Python 2 itself was first released in 2000; and the Python 2 series is being end of lifed by the core developers in 2020. So you're looking at 10 to 20 years between having to switch versions.
 
  • Like
Likes WWGD
  • #24
This is a philosophical objection I have to Python as a scripting language, versus Perl. The Perl philosophy is "there are many ways to do it" and they try to accept any syntax where they can understand the intent. The Python philosophy seems to be "this is the perfect way to do it and you must comply". That is until the next version comes out and the new philosophy is "this is the new super-duper, perfect way to do it and the old way was TERRIBLE and unacceptable." The Python changes sometimes require rewrites. The changes I needed to make to decades-old Perl scripts were minimal.
 
  • #25
FactChecker said:
The Python philosophy seems to be "this is the perfect way to do it and you must comply".

This has never been the Python philosophy. The Zen of Python has this:

"There should be one—and preferably only one—obvious way to do it."

Although Tim Peters couldn't resist following that with this:

"Although that way may not be obvious at first unless you're Dutch."

But in any case, nothing requires you to do it the obvious way.

[1] https://en.wikipedia.org/wiki/Zen_of_Python

FactChecker said:
That is until the next version comes out and the new philosophy is "this is the new super-duper, perfect way to do it and the old way was TERRIBLE and unacceptable."

Within a given Python version (2 or 3), I'm not aware of any change that made a previous way to do it no longer doable. Plenty of changes opened up new ways to do things, but those never invalidated the old ways.

The Python 2 to Python 3 change certainly did break a lot of old ways of doing things, but that was on purpose and the Python devs were quite open about that for years in advance. Also, Python 2 has stayed around and fully supported for 10 years while Python 3 was refined.

FactChecker said:
The Python changes sometimes require rewrites.

Do you have an example within a single Python version (2 or 3)?
 
  • Like
Likes jedishrfu
  • #26
Now Scala took this approach resulting in nearly every new version requiring code changes to keep up with the syntactic and semantic changes happening in the language. To be fair, Scala is relatively early in its gestation and is still evolving to become the "perfect" programming language better than Java.

Meanwhile developers move on to more practical languages such as Kotlin or clojure (lisp anyone?) as a means to improve coding performance and expressive power of the language.

Python is a very good language and supersedes Perl in so many ways. But its hard to just make the switch unless you're always looking for the "perfect general purpose" language like me. I preferred C over C++ and Java over both, Groovy over Java and like Kotlin a lot. However, my all time favorite is AWK but I would quickly hit roadblocks due its limitations and programming model and I would happily spend endless hours overcoming them until finally biting the bullet and converting my scripts to Python.

In my experience, I don't see Python restricting my programming at all unlike say Prolog or Lisp or even using in a purely functional programming model which really restricts what you can and can't do but sure makes your code so much easier to debug.
 
  • #27
PeterDonis said:
Do you have an example within a single Python version (2 or 3)?
Not within versions, just from 2 to 3.
 
  • #28
I have converted some Python scripts, programmed by advocates who were very good programmers. The Perl version was much shorter, easier, and more complete because it is set up very well to invoke OS commands, capture the output into Perl variable, parse, and move to the next step. The reduction in lines, including better error checking, was significant.
 
  • #29
FactChecker said:
Not within versions, just from 2 to 3.

Ok. Yes, as I said, there were many backwards incompatible changes from Python 2 to Python 3, but that was made clear for years in advance by the dev team, and they have continued to support Python 2 through the entire process. Only now, when Python 3 is very mature, are we reaching the point where Python 2 will be end of lifed by the core dev team; and even then there will be a further period when others, such as some Linux distributions, will continue supporting it.

Plus, from minor version to minor version in Python 3, just as with Python 2, no incompatibilities were introduced, so code that was written years ago for an early Python 3 version still runs on today's latest Python 3 version. So backwards incompatible changes are very rare with Python, since they only happen with major version changes, and those are very rare (Python 2's total lifetime, as I've said, is about 20 years).
 
  • #30
FactChecker said:
I have converted some Python scripts, programmed by advocates who were very good programmers. The Perl version was much shorter, easier, and more complete because it is set up very well to invoke OS commands, capture the output into Perl variable, parse, and move to the next step. The reduction in lines, including better error checking, was significant.

I don't doubt that there are plenty of cases where this will happen. I also don't doubt that there are plenty of cases where the opposite is true, that the Python version of some particular code will be much easier to deal with than the Perl version. It will depend on what particular task you are trying to do (if you're invoking OS commands and parsing the output, then Perl, which is much closer to shell in this respect, will probably be more concise, yes). It will also depend on your own personal preferences, as well as who else will have to deal with your code.
 
  • Like
Likes jedishrfu and FactChecker
  • #31
PeterDonis said:
I have done web scraping with BeautifulSoup in both Python 2 and Python 3. There isn't much difference between the two. The kind of code you will need to write for web scraping will be pretty much the same in both versions.

https://pypi.org/project/beautifulsoup4/
Do you use some special package for ML, like Matplotlib , Graphlab, etc?
 
  • #32
WWGD said:
Do you use some special package for ML, like Matplotlib , Graphlab, etc?

I haven't had a need for ML in the web scraping I've done, so I don't have any real experience with these libraries.
 
  • Like
Likes WWGD
  • #33
PeterDonis said:
It will depend on what particular task you are trying to do (if you're invoking OS commands and parsing the output, then Perl, which is much closer to shell in this respect, will probably be more concise, yes). It will also depend on your own personal preferences, as well as who else will have to deal with your code.
Exactly. But when the work is heavily loaded toward a type of work, personal preference is not so important as how well the language has been designed for those tasks. A programmer must be adaptable. I didn't reprogram the Python code due to personal preference. I hate reprogramming working code. I only did it after it became obvious that the results would be so much better and easier to maintain. I handed the code off to others who had no experience in either language (but they were smart programmers) and they had no trouble modifying it as they desired.
 
  • #34
My first exposure to it was version 3.5, I think. I have come across code examples which were written for 2.x One thing I found is that the print statements work different in 2. and 3.
In 2.x, it is a statement. You would type
Code:
print "Hello, World"
for example.
In 3.x it is a function. The argument of the function must be enclosed in parenthesis:
Code:
print("Hello, World")
 
  • Like
Likes jedishrfu and WWGD
  • #35
Here's a summary of the major differences between Python 2 and Python 3:

https://www.geeksforgeeks.org/important-differences-between-python-2-x-and-python-3-x-with-examples/
I ran into several while converting some C code to python2 and then discovering I had to do it for 3 instead. The / vs // operator split was the most troubling conversion as the code was indexing into a custom database via seeks and record sizes. They used / everywhere with the understanding that it returned an integer when the operand datatypes were integers but 3 changed that notion and "broke" the code. The fix was to change to // wherever the / was used with integer operands. Duh
 
  • Like
Likes PeterDonis and WWGD
<h2>1. What is the difference between Python 2 and Python 3?</h2><p>Python 2 and Python 3 are two different versions of the Python programming language. Python 2 was released in 2000 and Python 3 was released in 2008. Python 3 was created to fix some of the flaws and inconsistencies in Python 2 and to make the language more efficient and modern. Some of the major differences between the two versions include changes to the print function, string handling, and the use of Unicode.</p><h2>2. Can I run code written in Python 2 on a Python 3 interpreter?</h2><p>No, code written in Python 2 is not compatible with a Python 3 interpreter. The syntax and some of the built-in functions are different between the two versions, so code written in Python 2 will not run on a Python 3 interpreter without making some changes.</p><h2>3. Are there any compatibility issues when using different versions of Python?</h2><p>Yes, there can be compatibility issues when using different versions of Python. Code written in one version may not run on a different version without making some changes. Additionally, some libraries and packages may only be compatible with certain versions of Python, so it is important to check compatibility before using them.</p><h2>4. How can I manage dependencies when using different versions of Python?</h2><p>One way to manage dependencies when using different versions of Python is to use virtual environments. Virtual environments allow you to create isolated environments for different projects, each with its own dependencies and versions of Python. This way, you can avoid conflicts and ensure that your code runs smoothly.</p><h2>5. Is it necessary to upgrade to Python 3 if I am currently using Python 2?</h2><p>It is not necessary to upgrade to Python 3 if you are currently using Python 2. However, Python 2 will no longer be supported after 2020, so it is recommended to switch to Python 3 in order to receive updates and support. Additionally, many new libraries and packages are being developed for Python 3, so you may miss out on new features and advancements if you continue to use Python 2.</p>

1. What is the difference between Python 2 and Python 3?

Python 2 and Python 3 are two different versions of the Python programming language. Python 2 was released in 2000 and Python 3 was released in 2008. Python 3 was created to fix some of the flaws and inconsistencies in Python 2 and to make the language more efficient and modern. Some of the major differences between the two versions include changes to the print function, string handling, and the use of Unicode.

2. Can I run code written in Python 2 on a Python 3 interpreter?

No, code written in Python 2 is not compatible with a Python 3 interpreter. The syntax and some of the built-in functions are different between the two versions, so code written in Python 2 will not run on a Python 3 interpreter without making some changes.

3. Are there any compatibility issues when using different versions of Python?

Yes, there can be compatibility issues when using different versions of Python. Code written in one version may not run on a different version without making some changes. Additionally, some libraries and packages may only be compatible with certain versions of Python, so it is important to check compatibility before using them.

4. How can I manage dependencies when using different versions of Python?

One way to manage dependencies when using different versions of Python is to use virtual environments. Virtual environments allow you to create isolated environments for different projects, each with its own dependencies and versions of Python. This way, you can avoid conflicts and ensure that your code runs smoothly.

5. Is it necessary to upgrade to Python 3 if I am currently using Python 2?

It is not necessary to upgrade to Python 3 if you are currently using Python 2. However, Python 2 will no longer be supported after 2020, so it is recommended to switch to Python 3 in order to receive updates and support. Additionally, many new libraries and packages are being developed for Python 3, so you may miss out on new features and advancements if you continue to use Python 2.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
Replies
6
Views
542
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top