Custom Libraries and Command Prompt

Hi all,
I have a code that I would like to automate with the Command Prompt. Currently, it works great when you push the button on the Revit ribbon. When I try to run it through the command prompt it throws an error saying my custom libraries cannot be found.

How do I insure that the custom libraries are loading before the script runs?

At the start of my script I have

print("Loaded Liberairs: {}".format('\n'.join(sys.path)))
with open ("filePath.txt", 'w')as f:
    f.write(repr(list(sys.path)))

When run from the Revit ribbon:

When run from CLI:

Libraries:
image

Script:

Sorry for the multiple posts, new user.

I’m sorry but I don’t really understand what you’re getting at. :confused:
As far as I can see, you’re printing out everything in your sys.path to a file to compare them.
I suspect this is because you’re getting an error.

But that’s very vague.

Can you be a bit more specific in your question?
I’d like to know more about:

  • the error you’re receiving
  • What you’re trying to achieve with from the command line (odds are it’s not going to be possible/easy)

Hi @GertjanVDB,
Sorry for not being clear. I have written a script to extract a bunch of data from Revit and save it to a csv. When I wrote it I put all of the definitions in a separate file and saved it in the extensions folder in a folder.lib per the pyRevit instructions.

When I manually open Revit, load a project and run the script from the ribbon, it executes with no errors and saves all data to the csv. At the start of the process, I print all modules in the sys.path to a text file (image in post one). In the image you can see that I have several modules loaded from a folder.lib location.

When run from the CLI Revit opens and starts to run the script. It then errors out when I try to load the modules from my folder.lib location. The error is cannot find module xxxx. The screenshot from my second post is the sys.path when run from the CLI. As you can see it has not loaded any modules as it did in the run before.

When Revit is started manually pyRevit auto runs its start-up script that loads everything in a folder.lib to the sys.path. When run from CLI it appears that this start-up script is not running as no extra modules are being added.

I have dug a little deeper and found this page. I think I need to do the Extentions Run section but I cannot get it to work. This is just a hunch and I may be completely off.

I hope that helps, if you require more information please let me know,
Steven

Thanks for the clarification. It’s way more clear now.
So to summarize your problem.

  • A script run from the ribbon runs fine, no issues.
  • A script run from the cli runs into problems, the required libraries are not found.

I’m glad to say that you were probably on the right trail in your first post then. It’s most likely a problem with the sys.path variables.

Now you’ve already proven that the requires paths for your script are not recognised through the CLI.
That’s great. We now know that pyrevit adds some folder locations to sys.path.
Something not done when using the CLI.

BUT… we can do that ourselves too.
You yourself know exactly in which folder those missing modules are located, since you wrote them yourself. So before your imports (of those modules), you should be able to just… add the folder that contains your other scripts (module/dependencies).

Do that, and your imports should be able to proceed.

However, there is unfortunately a catch to this approach.
The thing is… this will only really work if your imports do not rely on the pyrevit module and runtime. Since pyrevit does a lot of initialization and setups for us when starting revit.
I’ve not personally dabbed in the CLI scripts, but I expect a real possibility of some other kinds of breakages.
However, I think it’s definitly worth a shot. If not for letting you run your script, at least for both of us learning more about how this works :slight_smile: