I’m new to Python, so please bare with me. I would like to use python-docx within Dynamo to be able (or try) to write to a Word document.
I went on the website for it and it explains how to install it using pip install but I don’t know how that works in IronPython through Dynamo or if it’s possible to use in Dynamo.
I’ve installed python-docx and lxml (a dependency) in a local folder but I don’t know how to access it.
So far I’ve tried this code but it can’t seem to get the library.
import clr
import sys
sys.path.append(r’[local path where python-docx is installed]’)
Is there something I’m missing when it comes to Python packages? Does it need to be put in a specific spot?
In general, don’t use anything not supported by IronPython 2.7.3 out of the box. You cant ensure that others have the same packages as you have. That version doesn’t support pip.
It looks unfortunately also to be the same outdated version of IronPython that will continue in Dynamo 2.0
I had hoped that ‘pythonnet’ (in a Python 3.6 version) could have been implemented instead, but that doesn’t seem to be the case (https://github.com/pythonnet/pythonnet).
If you place you downloaded python package in the lib folder (IronPython) and it has the --init–.py file, then should you be able to implement it using the normal path to IronPython… and obviously also only if your package support that version of Python!
Below is another way of implementing sys.path, by using insert you ensure that the path to IronPython is the primary path.
You can run external commands like scripts or executables from Dynamo. You can parse your data from Revit to Dynamo and then run a Python script from outside Revit and Dynamo. You can run Python with every library you need and both Python 2.x and 3.x
Take a look at this.
And as written, including packages not an OOTB package will give problems when it has to run on another computer. But as written, yes is is doable… My own package is coded as a OOP environment based on external files where I have created my package as a python library.
A Python script from inside Dynamo can run any arbitrary external command. You can run any shell script or executable. There’s no problem in installing stand alone Python 3.x or whatever version docx library needs and run external Python script within that environment.
To wrap up.
Install stand alone Python.
Install all required libraries.
Create a .py script on your computer.
Call your script from Dynamo
I don’t know if OP works alone or in corporate environment. His problem is solvable but not sustainable.
@Tomasz
Do you have any example where that is done? I would be very interested to see that. I would also prefer to be able to run python 3.6… so please show some example.
I am considering how I could implement pythonnet in a zerotouch node, just to be able to code in python 3.6 and be able to use pip. So if you know a workaround, please share
Yes, it seems this package is supported with IronPython 2.7.3. I made sure of that.
The thing is I have the package downloaded and unzipped, but how do I use the setup.py to install the package and get the init.py file?
I probably should’ve included that in the original post.
Also, I’m in an office and would like to put it in a shared drive location that other people could access. Does it have to go C:\Program Files (x86)\IronPython 2.7\Lib?
Thanks so much for the help!
You can place it anywhere… just give a path to it
As mentioned earlier I have implemented my package as an OOP environment. This means that you could place the python package inside your own package and by doing this you can “ensure” other users to have what is needed. Again implemented with a path, which I have done by reading a binary file in the bin folder.
Okay I see what you mean then. That’s convenient!
I guess it’s the package download that’s confusing me. After unzipping the package, it gave me a folder with the name of the package and setup.py, but I don’t know if that’s the actual package.
Do I run the setup.py somehow or place that in that folder?
How do I run that? Is that done through IronPython? Does that make sense?
by that you will have the package installed in the native Python folder… copy what you need to the shared location or whatever solution you prefer, then you can load the modules in that package as you please
Okay that’s what I needed. I didn’t realize I’d have to install Python, I figured I could go through IronPython but this sounds like a more straight-forward route. I’ll give it a try. Thank you so much!
Below is there a how-to guide for using external IDE, that involves a native Python installation… I will strongly recommend everyone to use external IDE if they want to work with python.
import sys
print(sys.version)
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))
input('Press Enter to continue...')
So you have to call it and let it run in a command shell? that is not optimal…
I agree that it is problematic that we only have IronPython (2.7.3) and we don’t have pip. But for now, it seems best to stick with Python 2.7 syntax since that be run inside dynamo.
However, I really don’t understand why the dynamo team doesnt try to implement pythonnet instead and let us use python 3.6 and pip… I don’t understand why they stick to IronPython (in an outdated version)
That’s why I have considered if it could be doable to implement pythonnet in my own zerotouch nodes…
I would also like to be able to implement other languages, example C# scripts, since there is so much help available for Revit in that language. I think also that being able to read TypeScript could be interesting.