Installing IronPython modules for Grasshopper
Introduction
Expected Time: 30 min
Although IronPython 2.7 does not support Python 3 modules, it is possible to install some libraries that are developed for Python 2. This tutorial will shortly explain the steps to install some of these modules. In the final chapter of this tutorial, we will mention some methods to use CPython and Python 3 modules. If you need more information about how to use Python in Grasshopper, take a look at How to use GhPython?.
You will need to install file-extraction software like 7-zip first, before you continue this tutorial. This tutorial is focused on Windows, but the process works similar on Mac.
Downloading Python Modules
Create a general folder in your explorer where you save all your modules. Do not remove this folder in the future and use it for all the modules you install.
In this tutorial, we will use the "Polyline" module as example, but you can also try another module, as long as it is supported by IronPython 2.7.
Search for the website which has a downloadable version of the module you need. A good example is the Python Package Index (PyPI) which includes most available Python libraries you will probably need.
Check which programming languages are supported. If Python 2.7 or lower is not included in the list, this module will probably not work in Grasshopper. As you can see, the Polyline Module is supported by version 2.7.
Find the downloadable files on the website, and download the .whl-file.
Place the downloaded .whl-file in the folder you created earlier for all your Python modules.
Next, we need to unblock the .whl-file. Otherwise, the modules will not work. Right-click on the downloaded .whl-file and go to the properties tab. Next, click on the unblock box.
- Open the properties tab of the .whl file
- Click on " Unblock" (if available)
- Click on Apply
Extract the data files from the .whl-file using software like 7-zip.
- Extract the .whl data
Finally remove the original .whl file.
- Delete the .whl file
Installing Python Modules
Open Rhino and open the Rhino Python Editor.
- Open the Rhino Python Editor
- Open the Options menu
- Click on the "Add to search path" icon
- Select the folder that includes all the Python modules you downloaded.
- Click on OK
- Restart Rhino
Open Grasshopper. The Python module should now be added to your Python component if you import it. Unfortunately the module does not work yet.
Sometimes, the module will not work immediately and you will need to download another module. For example, the polyline module also requires the "Six" module which is not installed by default. Fortunately, the "Six" module is also available for Python 2.7 and therefore can also be added to the Python Modules folder we created.
- Download the Six module
- Add the unblocked version of the .whl-file in the Python modules folder
If the module is still not working, you can try the following options:
- Restart Rhino and Grasshopper again;
- In the Rhino Python Editor, reference to the specific module-folder, in this case the polyline.1.4.0-py2.py3-none-any folder inside your main Python Modules folder;
- If this works, you can redirect to the main Python modules folder again.
In practice, most of the time you just have to try if a module works. In most cases, alternatives are available if a module is not supported by Python 2.7. For example, a widely used module called Requests, can be replaced by the Urllib library.
Using CPython (Python 3) modules
If you really need to use CPython modules, for example NumPy or SciPy, this is not possible by default. Currently, there are two work-around available. Both these methods are not easy to use, so make sure you are experienced in Python programming and/or the use of Grasshopper.
- Use the Hops component and create your script in Visual Studio Code. You can find more information about this method on the McNeel Github page.
- Use the GH Python Remote component from Food4Rhino. Installing this component works differently than usual external Grasshopper plugins. An extensive manual is added on Food4Rhino.
Keep in mind that other users of Rhino and Grasshopper will also need to install these methods, before they will be able to use your script. Therefore it is recommended to use the first option, because then the final user does not need to understand any Python script, but should only install the Hops component.
For open-source Grasshopper tool development, it is not recommended to use any of the above mentioned options. Consider to switch to C# if you really need to implement the advantages of C-based Python modules.