How to get a family type preview image from a Family Document with Dynamo?

Hi Ruben,
Here is what you can do to get it to work. Download the latest windows msi from GitHub if you don’t already have it - IronPython 2.7.12
From a windows shell do a custom user install - for example

msiexec /a C:\Users\%username%\Downloads\IronPython-2.7.12.msi /qb TARGETDIR=C:\temp\IPY

This will take a few minutes, once the install is finished run ensurepip with ipy.exe from the installed folder like this

"C:\temp\IPY\IronPython 2.7\ipy.exe" -m ensurepip

You should get an output like this
image

Ok now we are close
Install olefile from source as above
image

For your attempt I would have a few questions - have you run the ensurepip command? Do you have permissions to install in the C:\Program Files\IronPython 2.7 folder?

It’s much easier to run shell commands with Python using the subprocess module. Point to the user preferred install with pypath and list the packages as a string or list. Your biggest issue is that 2.7 is depreciated and a lot of the libraries will be a painful, if not impossible, to install

# IronPython 2.7
import os
import subprocess


def install(package, uninstall=False):
    comm = [pyexe, "-X:Frames", "-m", "pip"]
    if uninstall:
        comm += ["uninstall", "-y"]
    else:
        comm += ["install"]

    if isinstance(package, str):
        comm += package.split()
    elif isinstance(package, list):
        comm += package
    else:
        raise TypeError("Input is not a list or string of packages")
        
    p = subprocess.Popen(comm, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    p.wait()
    return [l.strip() for l in p.stdout.readlines()] 


pypath = r"C:\temp\IPY\IronPython 2.7"
pyexe = os.path.join(pypath, "ipy.exe")

packages = IN[0]
uninstall = IN[1]

OUT = install(packages, uninstall)
2 Likes

1 Like

I installed the package downloading the zip file and extracting in a folder, but the packages of library is installed in the folder IronPython2.7 in site-extensions though, but even installing the package I do not see how to use it in Dynamo yet.


looking in the installed folder of libraries I see as well pip which was already there I believe. I did not run that code line of ensurepip or I do not remember.
image

It doesn’t look installed to me - there would be an ‘olefile’ folder there which is what you are accessing when you use import olefile

Rather than unpacking the zip file, what happens if you try this
C:\<python path>\ipy.exe -X:Frames -m pip install <path to olefile.zip>

there is an olefolder within the folder olefile-0.46-py2.7.egg created by installing as I indicated before with setup.py from the unzipeed folder dowloaded . there are 2 files called init.py and setup.py if I do not remember wrong.
image

and I tried to load the library in dynamo with this code I posted before but result says no module named future so I downloaded the library package future and installed as well but same result, I think I must be doing something wrong as I am begginer in this but I am trying everything I can. I will try later what you suggested by installing ironpython in a user folder.

import sys
import clr
clr.AddReference('System.IO')
from System.IO import Directory, Path

# Ensure that IronPython can see the olefile module by adding it to sys.path
egg_path = r'C:\Program Files\IronPython 2.7\Lib\site-packages\olefile-0.46-py2.7.egg'
olefile_module_path = Path.Combine(egg_path, 'olefile')

# Append both the egg directory and the olefile module directory to sys.path
if egg_path not in sys.path:
    sys.path.append(egg_path)
if olefile_module_path not in sys.path:
    sys.path.append(olefile_module_path)

# Now try to import the olefile module
try:
    import olefile
    import_message = "Successfully imported olefile!"
except Exception as e:
    import_message = "Failed to import olefile: {0}".format(e.message)

# Output the result to Dynamo
OUT = import_message, sys.path  # Output sys.path for debugging purposes

I installed ensurepip, the package also using easy-install olefile method and after that I ran the code you suggested and it says detected already deleting and reinstalling.

PS C:\Users\RuBeN> & “C:\Program Files\IronPython 2.7\ipy.exe” -X:Frames -m pip install B:\Descargas\olefile-0.46.zip
DEPRECATION: A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at Release process - pip documentation v24.0.dev0
Processing b:\descargas\olefile-0.46.zip
Installing collected packages: olefile
Found existing installation: olefile 0.47.dev4
Uninstalling olefile-0.47.dev4:
Successfully uninstalled olefile-0.47.dev4
Running setup.py install for olefile … done
Successfully installed olefile-0.46

Then if this is installed all right I just want to use the libraries, so I used this script to try to import the library olefile and also future because it is asking me about this as well, but the script used reported this:
[0] Successfully imported future.
[1] Failed to import olefile: No module named future

I used this script to import libraries:

import clr

# Path to the future module
future_path = r'C:\Program Files\IronPython 2.7\Lib\site-packages\future-0.18.3-py2.7.egg'
# Path to the olefile module
olefile_path = r'C:\Program Files\IronPython 2.7\Lib\site-packages'

# Append these paths to sys.path
if future_path not in sys.path:
    sys.path.append(future_path)
if olefile_path not in sys.path:
    sys.path.append(olefile_path)

# Try importing future and olefile
try:
    import future
    future_message = "Successfully imported future."
except ImportError as e:
    future_message = "Failed to import future: " + str(e)

try:
    import olefile
    olefile_message = "Successfully imported olefile."
except ImportError as e:
    olefile_message = "Failed to import olefile: " + str(e)

# Output the results
OUT = future_message, olefile_message

Is there any reason why you are installing the downloaded zip on the ‘Program Files’ version of IronPython?

The code is not using the future module correctly. See here
__future__.py should already be in C:\Program Files\IronPython 2.7\Lib folder

If you follow the steps in this post and it should work

Some notes on posting code

well I ran this script in dynamo import sys OUT = sys.version_info to know the ironpython version and it says 2.7.3 final but I uninstalled it from my computer and installed ironpython 2.7.12 instead but Dynamo does not consider the new version?

I installed ironpython in that folder because installed from a installer file IronPython-2.7.12.msi.

Future library is installed and dynamo script loads it successfully although when trying import olefile is asking for future.
image
I reinstalled dynamo 2.0.4 which reinstalls ironpython 2.7.3 in my computer and some of the previous scripts I used are not working.

I tried to load the olefile library using Dynamo of Revit 2022 which is using ironpython 2.7.9 by default and I believe it worked but I would like to change the ironpython version 2.7.3 that Dynamo is using for ironpython 2.7.12

The future module has not been installed - you are looking at the install package (.egg file)

Here is the line from the olefile GitHub repository from __future__ import print_function

You can get the python exe Dynamo uses with the following

import sysconfig
import os

OUT = sysconfig.get_path('data'), os.path.abspath(os.__file__)

It is best to not touch this install

have you tried this from CMD / PowerShell
"C:\Program Files\IronPython 2.7\ipy.exe" "C:\Program Files\IronPython 2.7\Lib\site-packages\future-0.18.3-py2.7.egg\setup.py" install

1 Like

I tried the last 2 script you shared but none worked in dynamo 2.0.4of Revit 2019, although works in Revit 2022 with the default dynamo version . you are right, the library future was not installed, I installed it and still getting same error no module named future, I do not know yet how to deal with that if it is possible

# The comment on the line below should be printed on Python 2.5 or older:
from __future__ import print_function   # This version of olefile requires Python 2.7 or 3.5+.

What to do with this line? I am using IronPython2.7 not python 2.7? Did you make this olefile library work with Dynamo 2.0.4 in Dynamo 2019?

I managed to have a go with Revit 2019 - You’re going to have to upgrade the file/ families to at least 2020 to get olefile working

Dynamo 1.3.4.6666 uses IronPython 2.7.3 from Microsoft and it just isn’t compatible with 2.7.11/12

1 Like