简体   繁体   中英

How to import Revit and pyRevit librairies with PyCharm?

This is my very first question here, hope won't be my last. As I already use some answers from a lot of people, I would like to start by thanking all of you.

Context: I'm developing a module for Revit, and for this, I'm using pyRevit to exchange with the Revit API. pyRevit allow me to code in Python, which I do with PyCharm IDE.

Problem: When I try to import libraries, I have a "Unresolved reference" error message. Otherwise, the module is already working on Revit so imports go well when using it.

I already try to add the folder's path to Files --> Settings --> Project Interpreter --> Interpreter path, then mark the folder as a source folder (Right click --> Mark Directory As --> Source Root), then Invalidate Cache / restart. Paths are:

C:\Users\***\AppData\Roaming\pyRevit-Master\bin
C:\Program Files\Autodesk\Revit 2019

I also try to add these folders in Files --> Settings --> Project Structure --> Add Content root. Once done, they appear on the project root. Here it is, with the failing imports on the right side (not enough reputation to post pictures so here is a link)

Project structure and red imports

So everything is flagged as "Unresolved Reference", even the System import. How can I help Pycharm to read these libraries correctly, delete all import errors and ease auto completion and development? I wonder if Revit libraries are protected but in this case, it should work with pyRevit and System, at least.

Any help would be greatly appreciated.

UPDATE: After having added Pyrevit folder to the project strucute AND mark it as Source Folder AND click on Files --> Invalidate caches/restart, the import of pyRevit is OK. Great News.

For the Revit folder (and also for Microsoft .NET libraries like mscorlib (for List)), I assume it fails because they are compiled from C# language. Is there a way to import libraries in C# with a Python IDE, or at least, recognize the internal structure and the functions names?

https://github.com/gtalarico/ironpython-stubs

Try these stubs for autocomplition with clr, revit etc. However, it does not include pyRevit libraries.

Find stubs in the folder release on GitHub link and download it. Then reference that folder in PyCharm Project Structure.

It seems there are many SyntaxError in the stubs. Have you corrected them? If no, How did you make the file work without correcting them?

example of SyntaxeError:

File "C:\ironpython-stubs\release\stubs\System\__init__.py", line 7366
None = None

Since you are using pyrevit, there is a stubs builder built in - this is more complete than the ironpython-stubs and can be done for the version of Revit that you're using.

You can run this script in your own extension and it should work. (Note - I have installed pyRevit through the installer on GitHub)

https://github.com/eirannejad/pyRevit/blob/7eb0a4e1f6d2509735b2da80168b1c8b0e5b2f20/extensions/pyRevitDevTools.extension/pyRevitDev.tab/Developer%20Tools.panel/Generate%20API%20Stubs.pushbutton/script.py

Add the generated file in your interpreter and you will have stubs for everything. I will note though, that sometimes the interpreter won't understand what kind of element you have (for instance it will get upset that you use.GetRodInfo() even though you've grabbed your fabrication elements through DB.FilteredElementCollector.OfClass(DB.FabricationPart).ToElements()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM