Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use python 3 libraries through PyRevit

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
duongphong29
9972 Views, 17 Replies

How to use python 3 libraries through PyRevit

I am working with Python in Revit using PyRevit. Although there are lots of limitation when using PyReivit, I still wanna try.

 

I was trying to import PySimpleGUI to create User interface for my button. I have install the library by using pip install.

 

When I import to my scirpt and run in Revit. It gives me "There is no modules named PySimpleGUI"

 

Is there a way for me to use python libraries in through PyRevit? 

 

Thank you in advance

 

 

 

Labels (3)
17 REPLIES 17
Message 2 of 18
smarente1
in reply to: duongphong29

Hi, 

 

The python package must be included with embedded cpython. The packages I have used are scipy, numpy, and pandas. 

 

This is how you would import pandas:

#!python3

import pandas as pd  

 

Ensure your environment variables have a variable called "PYTHONPATH" and is pointing to the location of the module you would like to import: 

  

smarente1_0-1616423141805.png

 

You may need to restart your computer after creating this new system variable for it to be recognized. 

 

Alternatively, you can directly path the module using sys: 

 

import sys

sys.path.append(r'C:\Users\smarentette\AppData\Local\Programs\Python\Python38\Lib\site-packages')

import pandas as pd

 

 

 

Message 3 of 18
duongphong29
in reply to: smarente1

I have set up everything but it still doesn't work, including pandas.

 

I think there is something to do with IronPython, Python and CPython. I am confused about those three.

 

Can you tell me exactly how to implement Python in Revit??

 

Thank you, I am appreciated.

 

Message 4 of 18
smarente1
in reply to: duongphong29

Python does not ship out with pandas, did you install it to site-packages using pip in your command line? 

 

If so, and that still doesn't work try uninstalling python 3.9 and using python 3.8 instead as the CPython engine is 385. 

 

Message 5 of 18
duongphong29
in reply to: smarente1

I have tried uninstall Python 3.9 and install Python 3.8.0 but it doesn't work. 

 

The result still the same:

IronPython Traceback:
Traceback (most recent call last):
 File "E:\PyREVIT\extensions\pyRevitTools.extension\DuongPhong.tab\Management.panel\Update Section.pushbutton\script.py", line 6, in <module>
ImportError: No module named pandas


Script Executor Traceback:
IronPython.Runtime.Exceptions.ImportException: No module named pandas
 at Microsoft.Scripting.Runtime.LightExceptions.ThrowException(LightException lightEx)
 at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
 at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
 at PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)
Message 6 of 18
smarente1
in reply to: duongphong29

Your error shows that your not using the Cpython engine. It should show this when the Cpython engine cant find or use a module: 

 

smarente1_1-1616429490526.png

 

Ensure your using the newest pyRevit. 

 

Message 7 of 18
duongphong29
in reply to: smarente1

OMG, Thank you, turns out because of it.

 

All I need is to put #! python3 at the beginning of the script to run script with CPython.

 

And this only works when I changed to Python 3.8

 

Thank you very much 

Message 8 of 18

Thanks in advance. I am having this same issue trying to run pandas. I am getting back in to pyRevit, and as far as I can tell I have everything set up as it should be, and I have followed all the advice in this thread, and yet this still does not run.

 

I am using Spyder as an IDE, but trying to resolve this issue I have installed Python 3.10 on its own as well. I have added a PYTHONPATH in Enviromental Variables pointing to all sorts of folders. I have added Search Paths to all sorts of directories. Nothing seems to work. I either get the "pandas module does not exist" message or this "invalid syntax" error message.

 

Let me know what other information you might need. Your expertise is definitely appreciated.

 

shearobisonSGEZR_0-1642983479089.png

 

Message 9 of 18

That is revit python shell not pyRevit... 

 

 

 

 

Message 10 of 18

Thanks for the response. RPS is how I am developing my scripts for pyRevit. I don't know of another way to test my scripts other than trying them out in the RPS first. I use Spyder to write the code in Python  and the RPS to check how the scripts work in Revit. If the script isn't working in the RPS will it still work in pyRevit?

Message 11 of 18
ghensi
in reply to: shea.robisonSGEZR

Maybe you already figured it out, but anyway... In short, you can't.

 

RevitPythonShell is still using IronPython, so it can't handle libraries that are not pure python or those that only support python 3.x.

 

There's an open issue on RPS's github regarding this.

 

Until someone gets their hands dirty and implement CPython support in RPS, you're out of luck.

 

I generally code directly in visual studio code set up with ironpython-stubs for autocompletion, and use revitapidocs for everything else, then run the command directly in pyrevit (you don't need to reload pyrevit after editing a script, so it's not so bad).

 

Of course you'll lose the interactivity...

 

 

Message 12 of 18
shea.robisonSGEZR
in reply to: ghensi

Thanks for your detailed response. I wasn't sure why Python 3 libraries wouldn't work but I just assumed they wouldn't and proceeded with what I could get to work. I have also figured out how to code and test from an IPE without having to reload so I am good there too. Hopefully your answer comes in handy for others facing these same kinds of issues.

Message 13 of 18
michaelmacneill
in reply to: smarente1

Both options worked for me. Just make sure your python3 site packages directory is the only directory PYTHONPATH points to. No semi-colons and no other directories.

 

Also if setting PYTHONPATH manually inwindows, rather than sys.path.append method my Pycharm environment got completely screwed up. Which would be fixed by changing my PYTHONPATH back to what it was pointing to previously, which meant Cpython no longer worked....

Message 14 of 18
Ian_H
in reply to: duongphong29

Does this method work for all libraries? I have a working python script that want to run through my PyRevit toolbar with a couple of simple inputs but I think it is having troubles importing the libraries. The 2 that I imported were 'requests' and 'json'. 
Here is the GitHub link to view my script if you want to see what it looks like for reference. 
https://github.com/kslifter/Python

 

Thanks

Message 15 of 18
Ian_H
in reply to: ghensi

So I am still fairly new to Python. I downloaded Python 3.10. I created a working script in VS Code and I imported the libraries ’requests’ and ‘json’. The script creates a new page in Notion. I am wanting to run this through Pyrevit. If I am reading this thread correctly I just need to point the script in the direction of where libraries are located? I had an similar response when working on running the script through Dynamo. Where are they saved on my computer? 
Here is the GitHub link to view my script as reference. 
Link

 

 

Message 16 of 18
ghensi
in reply to: Ian_H

Hi @Ian_H ,

PyRevit comes with its own Cpython3 interpreter, and already has requests in its own library and json is a built-in module, so you don't need to point your scripts to the external python 3.10 site-packages.

just follow the developer guides on pyrevit's notion to create your button.

 

Message 17 of 18
Ian_H
in reply to: ghensi

Thanks for getting back to me. I ended up figuring out how to run it through Dynamo. I will remember this for the future though.

Message 18 of 18

Good day,

I am reading your post and I am also struggling with what you struggled with and seemed to figure out. I would appreciate if you can let me know how to code and test from an IPE (I am using VSCode) without having to reload pyRevit every time. Currently it is very frustrating and timeous to have to reload every time I want to test code.
Thankyou in advance

Regards
Wagner

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community