Reputation: 3860
I am trying to run the script defined here: https://iterm2.com/python-api/tutorial/example.html
import iterm2
async def main(connection):
app = await iterm2.async_get_app(connection)
window = app.current_window
if window is not None:
await window.async_create_tab()
else:
print("No current window")
iterm2.run_until_complete(main)
Running script (python3 code.py) result in the following error
File "code.py", line 20, in <module>
iterm2.run_forever(main)
File "/Users/naveen/ENV/mysite-env/lib/python3.8/site-packages/iterm2/connection.py", line 528, in run_forever
Connection().run_forever(coro, retry, debug)
File "/Users/naveen/ENV/mysite-env/lib/python3.8/site-packages/iterm2/connection.py", line 159, in run_forever
self.run(True, coro, retry, debug)
File "/Users/naveen/ENV/mysite-env/lib/python3.8/site-packages/iterm2/connection.py", line 240, in run
return loop.run_until_complete(self.async_connect(async_main, retry))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/Users/naveen/ENV/mysite-env/lib/python3.8/site-packages/iterm2/connection.py", line 416, in async_connect
async with self._get_connect_coro() as websocket:
File "/Users/naveen/ENV/mysite-env/lib/python3.8/site-packages/websockets/legacy/client.py", line 604, in __aenter__
return await self
File "/Users/naveen/ENV/mysite-env/lib/python3.8/site-packages/websockets/legacy/client.py", line 622, in __await_impl__
transport, protocol = await self._create_connection()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1033, in create_connection
raise OSError('Multiple exceptions: {}'.format(
OSError: Multiple exceptions: [Errno 61] Connect call failed ('127.0.0.1', 1912), [Errno 61] Connect call failed ('::1', 1912, 0, 0)
python version: 3.8.3
iterm2 Build version: 3.3.10
iterm2 python library: iterm2==1.25
reference: https://github.com/aaugustin/websockets/issues/593
Upvotes: 1
Views: 482
Reputation: 8618
You need to install a Python runtime in iterm2 and allow Python scripts in a separate step, plus install the iterm2 module.
Install Python runtime: Scripts -> Manage -> Runtime
Allow Python scripts: Preferences, tab General, sub tab Magic: tick the "Enable Python API" checkbox. Mac OS' Accessibility settings will ask "Enable Python API?", click ok.
Then run pip3 install iterm2
.
Upvotes: 1