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
Solved! Go to Solution.
Solved by smarente1. Go to Solution.
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:
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
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.
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.
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)
Your error shows that your not using the Cpython engine. It should show this when the Cpython engine cant find or use a module:
Ensure your using the newest pyRevit.
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
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.
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?
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...
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.
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....
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
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
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.
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.
After setting the Pythonpath to my python3.8, python3 packages started to work with my pyRevit extension, however, my pyrevit Stubs and Revit API Stubs for VSCode Intellisense suddenly stopped working. What could be the reason for this and is there a fix? I have included the stubs path in my VSCode settings.json and also include the stubs in my Path environment variable but they still don't work!
#! python3
# -*- coding: utf-8 -*-
import sys
sys.path.append(r'C:\\Users\\piete\AppData\\Local\\Programs\\Python\\Python310\\Lib\\site-packages')
import pandas as pd
Above is my code
I get this error:
CPython Traceback:
ImportError : Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
How do I fix this?
I'm an architect by eduction so I know very little about the background of coding.
Can't find what you're looking for? Ask the community or share your knowledge.