I am currently working on a development project where i need to script some python functions that i would like to call using a GHpython Script to interact with some of my Data. What i have understood is that i need to write my code in python 2.7 because the GHpython Script uses IronPython2. Seems however that there is now a version of IronPython 3 that supports python3.X, is there any way to make it work fror rhino7 so that i can write my external functions using pyhton 3.X?
I am just starting in Coding, so forgive me if there has been some unclarity / misunderstanding in my explanations!
There are various CPython 3 tools and projects, and IronPython 3 can be run as an external process from GhPython just as well as any other executable.
Which feature exactly from Python 3 or Iron Python 3 can you absolutely not live without? Anything’s possible, but I find the easiest thing to do, is simply to write Python 2/3 compatible code.
Thanks James for your answer!
Well two main reasons why im considering python3 instead of 2:
- Not a fan of writing a development tool in an obsolete language
- I am actually struggling to import the packages (requests and office365) for python 2.7 instead of python 3.9 in Visual studio (the Pip command installs them for python 3.9 but when i switch the interpreter, they are not recognized anymore). I’ve tried many things including virtual environments but still struggling with it. So I am trying a different approach.
It depends on the packages, but you may have just the same problems for importing if you are using IronPython 3. The Rhino 8 WIP includes support for CPython 3 which is something you may need to use if you are attempting to import certain packages.
@Torso i tried using that plugin but it consistently crashed Rhino when opening the Python editor, Any idea how i could get that fixed?
When the Python editor crashes, do you get an error message? Do you get a 'DotNet" crash file in your desktop? How can we repeat the crash you are getting?
@dale No error message, its my whole Rhino that crashes completely.
Seems that I am not the only one experiencing an issue with that plugin as i saw some comments (in chinese, thanks DeepL) that mentionned the same issue in the Food4 Rhino page. I have however no idea how this issue can be replicated.
I am probably going to follow @James_Parrott advice and rewrite my code so that its compatible with python 2.7. (Few lines will need changing hopefully)
I have found a workaround for downloading my packages compatible with python 2.7 (ie outdated) from pip in python 3.9 and then copy paste them in my python 2.7 interpreter folder. Seems to be working so far.
I wasnt expecting this python version and packages download thing to be such a big issue. Another Rabbit hole in the learning process!
Great stuff Augustin. I find backporting isn’t too much work at all if you set up a linter for Python 2 in your source code editor. The only Python 3 thing I do miss, is f-strings. But they’re actually a little awkward to type when printing, compared to C-style interpolation with %, a lot of brackets and quotes need to be matched.
I wasnt expecting this python version and packages download thing to be such a big issue
Welcome to Python!
@James_Parrott maybe you could be helping me just a bit more:
I have manged to download an old package of openpyxl that is supposed to be compatible with python 2.7 (Asked ChatGPT) using pip in the cmd prompt and linked my Rhino pyhton editor to it by adding the path to where i have installed my package.
Seems that GH_pyhton editor finds that package but throws a weird error (see screenshot) where the from … import sentence is not recognised in some of the package files. Any idea what could be going on?
Edit: when im trying to run everything in Visual studio using a python 2.7.0 interpreter, all is working find with this package, so what is the difference between GH-Python and the Visual studio python 2.7.0 interpreter
No problem. I’m not sure at all why a CPython 2.7 interpreter would behave differently to GHPython, but your problem feels very familiar. Open up \openpyxl\compat\numbers.py in your Python27 packages folder and see what’s on line 17 (or an unclosed opening bracket on a previous line)
I was bamboozled for a few hours a couple of months back with LadyBugTools and ShrimpGIS, by exactly the same error.
The import statement using the keyword
from is fine. This error can still occur when Python 2 code using
from module import thing, not because of an issue with this completely valid
from as far as Python 2 is concerned, but because in
module, there are Python 3 only statements of the forms:
yield from iterator or
raise Error from Exception.
Python parses modules for Syntax errors first, before passing over it again to actually run the code. Maybe that’s why when there’s a SyntaxError in an import, GHPython doesn’t always provide a helpful stack trace.
Yeah, I reckon you have exactly the same problem as people have with shrimpGIS and LadyBug: Numpy. Somehow there’s a version of Numpy installed that uses the Python 3