cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

2024 Technology Preview Program:
Master powerful new features and shape the latest BIM-enabled innovations

Archicad Python API
About automating tasks in Archicad using the Python API.

Compile Python Scripts to Executables

wkrt-hs
Booster

Dear Community,

I'm trying to compile my Python scripts to executables using pyinstaller.
Unfortunately, the executables seem to fail to connect to Archicad.

from archicad import ACConnection
conn = ACConnection.connect()
assert conn
Traceback (most recent call last):
  File "xxx", line 9, in <module>
    assert conn
AssertionError
[15984] Failed to execute script 'xxx' due to unhandled exception!

 There's already a similar topic but it is locked: Compile Python Scripts to Executables - Graphisoft Community

Did anyone already succeed with this? Building executables should be a common usecase for automation purposes.

Best, Wenzel

8 REPLIES 8
GAG
Advocate

@wkrt-hs wrote:

I'm trying to compile my Python scripts to executables using pyinstaller.
Unfortunately, the executables seem to fail to connect to Archicad.


Dear Wenzel, you could try my script `archiconnect.py` which doesn't use the official Graphisoft archicad package, but could be used for connection testing.

 

How to test

Download latest archiconnect release from GitHub

Extract release and run `archiconnect.py` directly in Terminal or via Python:

./archiconnect.py
python archiconnect.py GetAllClassificationSystems

 

When running Archicad in background, you'll get output in Terminal:

 

Connecting to 127.0.0.1:19723 …

Is alive: True
Host version: Archicad 27 3001 INT

 

archiconnect_powershell_windows.png

 

 

 

Creating and running PyInstaller package

 

Build package:

pyinstaller -F archiconnect.py

 

Run packed executable:

./dist/archiconnect

 

If this executable does or does not work in your environment, please post a short report here so we can investigate further.

George
wkrt-hs
Booster

Hi George and thank you for your reply!
Building and running your script works fine.

But it would still be nice to also use the official archicad package for this usecase.


@wkrt-hs wrote:

Building and running your script works fine.

 


Well, this means that you have no specific PyInstaller issues when using generic requests.

 


@wkrt-hs wrote:


But it would still be nice to also use the official archicad package for this usecase.


If you could provide your script, I could check/rewrite it for compatibility with PyInstaller.

 

George

It's literally the 3 lines from the first post that already fail.
Are you able to build and run them successfully in your environment?


@wkrt-hs wrote:

It's literally the 3 lines from the first post that already fail.


I already have a working solution for the simple connect (see my first comment).

 


@wkrt-hs wrote:

Are you able to build and run them successfully in your environment?


No, unfortunately, the official GS Python module doesn't work well in PyInstaller environment and I don't have enough motivation to debug and fix their code. They suppress error messages, it's not a very good practice:

 

    @staticmethod
    def find_first_port() -> int:
        for port in ACConnection._port_range():
            try:
                ACConnection(port)
                return port
            except Exception:
                continue
        return None

    @staticmethod
    def find_ports(ports: slice) -> List[int]:
        result = []
        for port in ACConnection._port_range():
            try:
                ACConnection(port)
                result.append(port)
            except Exception:
                continue
        return result

 

I can't help you if you don't want to share your code, sorry.

 

George
wkrt-hs
Booster

Thanks for your response, George. 
I'll see if I investigate further, but it's good to know that it's probably nothing related to my python environment.


@wkrt-hs wrote:

but it's good to know that it's probably nothing related to my python environment.


Sure )

Maybe GS will fix their initialisation code to be compatible with the PyInstaller environment, but I haven't found an official GitHub repo for the `archicad` Python module to create an Issue.

George
martti
Newcomer

Not sure if this is relevant for @wkrt-hs anymore, but I had the same problem with pyinstaller and spent 3 days trying to find the fix.

 

What eventually worked for me was to collect the local archicad installation into the bundle, and to use these command-line flags with pyinstaller:

pyinstaller --collect-all archicad -F <other arguments> <python_app.py>


I hope this helps anyone who stumbles on the same topic!