Jump to content

Python 3, using old Python 2 scripts


Krion

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,

Link to comment
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!')

Link to comment
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
Link to comment
Share on other sites

  • 4 months later...

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

 

Link to comment
Share on other sites

  • 1 month later...

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
Link to comment
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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...