Jump to content
Krion

Python 3, using old Python 2 scripts

Recommended Posts

Hi,

I have just installed the Python 3 version of Houdini to test out if I can use old code with it. I had read something about an included convert-script.

My own shelf tool which references an old python script in an own PYTHONPATH of mine produces this error:

6048e5ccf3a9c_Screenshot2021-03-10at16_27_49.png.4a85781dc1da68c81b8b75436d1aea16.png

Do I need to rewrite all my old code?

Thanks,

Share this post


Link to post
Share on other sites

There is no such a big difference between Python2 and 3, so re-writing should not be hard. 

In Python 3 "print" is not a statement anymore, it is a function, which means you have to place everything in parentheses: print ('Hello, World!')

Share this post


Link to post
Share on other sites
On 3/10/2021 at 7:41 AM, kiryha said:

There is no such a big difference between Python2 and 3, so re-writing should not be hard. 

In Python 3 "print" is not a statement anymore, it is a function, which means you have to place everything in parentheses: print ('Hello, World!')

This is simply not true. There's a huge difference between python 2 and 3. There's a reason why there's still massive amounts of Python 2 code around even after 12 years since Python 3 came out.

Changes to import mechanics, strings and unicode, iterators, metaclasses just to name a few. Switching to Python 3 is not trivial.

For simple scripts, converting manually is a reasonable option, for bigger projects I use this tool: futurize

Quote

Do I need to rewrite all my old code?

Check out this guide: https://docs.python.org/3/howto/pyporting.html

Edited by Stalkerx777
typo
  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
18 hours ago, Stalkerx777 said:

There's a huge difference between python 2 and 3.

My bad, you are right. But I was assuming the script is quite simple, and in that case, I guess it can be easy to update.

Share this post


Link to post
Share on other sites

If you are a beginner then I would suggest you to just learn Python3 as Python3 will take over Python2 by 2020. But, if you are interested in just the differences than I can tell you a few common difference a beginner should know like - Python 2 vs Python 3

  • In Python2 print is like a command but in Python3 print() is a function
  • In Python2 the integer divide works in C/C++ style but Python3 will return the expected result. For example, In Python2 7/2 will return 3 but in Python3 it will return 3.5
  • In Python 2, a string is by default ASCII. But in Python3 string is by default Unicode

 

Share this post


Link to post
Share on other sites

Hi there,

I'm very new to learning Python and am running the latest version of Houdini with python 3.

I was trying to run a simple command from a tutorial ( but it's py2 ) . I figured out that you have to now do print("stuff").
In the tutorial

When I run the code with what I thought would be the correct change to syntax:

import hou

my_obj_context = hou.node('/obj')

my_geo = hou.node('/obj/geo1')

print (my_geo.children)

I get the following result:

<bound method Node.children of <hou.ObjNode of type geo at /obj/geo1>>

When I try in an older version of Houdini with py2 I get the actual list of nodes ( but with print my_geo.children() ).

I can't seem to find any tutorials or guides that can help me get into using python in houdini with version 3 and above. Any advise is welcome.

 

Edited by GDonkey
I'm bad at links :P

Share this post


Link to post
Share on other sites
23 hours ago, GDonkey said:

print (my_geo.children)

I get the following result:

<bound method Node.children of <hou.ObjNode of type geo at /obj/geo1>>

When I try in an older version of Houdini with py2 I get the actual list of nodes ( but with print my_geo.children() ).

Hi, you forget to call the method.
print (my_geo.children) prints that method object. Not calling it.

You need:

print(my_geo.children())

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×