Import Shapely in python

Hey pretty simple question.
I want to use Shapely in my python script. Specifically LineString. I’m not sure how or if it is possible to do this. Any ideas?

I’m getting this error:
ImportError: No module named shapely.geometry

Code:

from shapely.geometry import LineString

line1 = LineString([(0,0), (1,0), (1,1)])
line2 = LineString([(0,1), (1,1)])

point = (line1.intersection(line2))

OUT = point

Have you successfully imported shapely in other IronPython scripts? It may be that IronPython 2.7 does not support shapely.

I have not. I’m a little over my head here to be honest.

I haven’t used any external modules in Dynamo so I’m not sure if this will solve your problem, but the first step you can take is to append your Lib folder to your sys.path variable:

import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
sys.path.append('C:\Program Files\IronPython 2.7\Lib')

I’ve only used this to import standard modules like csv and StringIO. Theoretically you would just have to install the shapely module in the same folder, but Dynamo uses IronPython 2.7 so I’m not sure if you’ll end up being able to do so.

1 Like

@dancleary Did this actually solve your problem?

I’m still not able to use shapely with IronPython due to the missing/not correctly available geos_c.dll

Could you please further explain your steps to install shapely under IronPython?

I’m not sure why my above response is marked as a solution, but, as the name of the .dll implies, GEOS is written in C. So, while it may work for CPython, it doesn’t look like shapely is compatible with IronPython.

Thanks for the feedback. That’s what I thought as well.

Got confused by the “solution”

@mbaicad Sorry about that! It did not solve my problem and I removed the solution. I have not been able to import Shapely into dynamo, I resorted to OOTB nodes and old geometry and math to get by my line intersection issue.

@cgartland mentions it may work for CPython. The only way I know how to get CPython to work with Revit is using PyRevit which allows you to write your own extensions. Might be worth looking into.

@dancleary @mbaicad You both may be interested in this thread from last year regarding launching a sub-process to execute a separate CPython script from within an IronPython script.