Import from .lib extension in startup.py

I’m trying to split the tabs in our extension into separate extensions.

I get an error that our Snippets module cannot be found by the startup.py script.

ImportError: No module named AVSnippets

Do I need to import it in a certain way, or is startup.py only able to import from a lib folder inside the extension it runs in?

The .lib extension is named the same as our main extension:
Company.extension
Company.lib

I assume the .lib extension might be loaded after the .extension?

Added info:
I don’t get the error on startup, only when reloading our extension or Enable/Disable our extensions.

I found this in the startup script example:

have you tried to

import sys
print(sys.path)

(before importing AVSnippets) to see if the extension lib is added to the search paths?

Thank you for the suggestion!

It does not print the path for the .lib extension.
The paths listed are:

1. Path to the .extension where the "startup.py" is located
2. %localappdata%\\MyOrg\\pyRevitClone\\pyrevitlib 
3. %localappdata%\\MyOrg\\pyRevitClone\\site-packages
4. %localappdata%\\MyOrg\\pyRevitClone\\bin
5. %localappdata%\\MyOrg\\pyRevitClone\\pyrevitlib\\pyrevit\\loader\\addin
6. %localappdata%\\MyOrg\\pyRevitClone\\bin\\engines\\2711
7. %localappdata%\\MyOrg\\pyRevitClone\\bin\\engines\\IPY2711PR
8. %appdata%\\pyRevit
9. %appdata%\\pyRevit\\2023 
10. %appdata%\\pyRevit\\Extensions
11. %localappdata%\\MyOrg\\pyRevitClone\\bin
12. %localappdata%\\MyOrg\\pyRevitClone\\bin
13. %localappdata%\\MyOrg\\pyRevitClone\\bin 

I’m not sure why it list the bin-folder 4 times?
The goal is to set up the following extensions.

  • AVA.lib (renamed to not have the same name as main extension)
  • AVAssistent.extension
  • AVAdmin.extension
  • AVBeta.extension
  • AVElectrical.extension
  • AVMEP.extension

Our extension is currently deployed as one .extension with multiple tabs.
The new extenson setup contain one .tab-folder in each extension. The reason is that we want to install them independently and not load all tabs on startup, as we need to “save” on the number of tabs Revit API can load.

I tried to append the AVA.lib folder to sys.path but now it lists less paths, missing all paths after the .lib path (like it throws an exeption?).

The extension.json for the .lib defines the type as a lib-extension.

Do you confirm that the problem is only with the startup.py script? I believe this is by design, as stated in the message I reported, and as you can see by the sys path: the other extension are not included to avoid hurting the load process (whatever it means).

If the other scripts are working fine after a reload, the only workaround I can think of is to turn the startup.py into a stand alone module, or rely only on modules inside its .extension folder.
I know this is not perfect because it introduces duplicate code, but sometimes we need to compromise…

It might be a bug in the (re)load process, but it doesn’t hurt :wink:

Every time you install pyRevit it adds an entry to the system path even if it already exists. Super annoying when testing installation scripts and end up with 20+ duplicates… maybe this is what you are seeing?
image

1 Like

This is a separate issue, the PATH environment variable is not the same as python’s sys.path

1 Like

Yes, the problem is only with the startup.py, all other imports works as expected, across all extensions.

Can also confirm it works if I duplicate the needed modules and add them to the .lib-extension.

I tried to duplicate the startup.py to the .lib-extension, but it seems to not load in the .lib I think? The test print statement is not shown for “Hello from AVA.lib”, only the “Hello from AVAssistent.extension” is output.

So separate/duplicate libraries/modules for the startup.py seems to be the workaround for now.