Execute Shell Script / Task - Error when importing python module psycopg2 - «ModuleNotFoundError: No module named 'psycopg2._psycopg'»

I use the function “Execute Shell Script / Task” to execute a python script.
The python script imports the module psycopg2.

Based on the discussion here, I have set a environment variable as follows:
PYTHONPATH=/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/

But I still encounter the following error in BTC:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ModuleNotFoundError: No module named 'psycopg2._psycopg'

Please note that the script runs flawlessly in Terminal.
I can also import psycopg2 in Terminal (and confirm that psycopg2 does have a module called psycopg).
So the error does not come from a bad installation of psycopg2.

  • macOS version: Monterey 12.4
  • python version: 3.10
  • BetterTouchTool version: 3.821 (1964)

What does the rest of your config for the shell script / task action look like?

Hey Andreas, thanks for reaching out.

Launch Path: /bin/bash
Parameters: -c
Environnement variables: PYTHONPATH=/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/
Script: python3 /path_to_file/file.py

As mentioned earlier, the script works fine when I run directly from Terminal, ie:
python3 /path_to_file/file.py

Have you tried like in the other thread you referenced? So instead of using /bin/bash -c directly using python?

That looks promising.
What would be the syntax, for pointing to a .py file in the script box?
I tried the absolute path, namely /path_to_file/file.py

I get the following error:

File "<string>", line 1
    /path_to_file/file.py
    ^
SyntaxError: invalid syntax

With Launch Path set as "/usr/local/bin/python3", I did try the following:

# https://www.geeksforgeeks.org/python-import-module-from-different-directory/
import sys
folder = "path_to_folder"
sys.path.append(folder)
from file import function
function()

Unfortunately, the script (backup of a local db) gets stuck.

The only workaround I found is to:

  • open a Terminal window
  • paste the python3 command.

In general you should only need to enter the path in the "script" box - in that case you'd however need to remove the -c Parameter

Thanks.
No error, but nothing happens :slight_smile:

Current params are the following:
LaunchPath: /usr/local/bin/python3
Parameters: None
Env: PYTHONPATH=/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/
Script: path_to_file/file.py

Weird, and it works if you do

/usr/local/bin/python3 path_to_file/file.py

on your terminal? What does the script do?

Yes, it does work.
I keep on looking for a typo somewhere, but I can't find any.
I copy-pasted in Terminal the following command:
/usr/local/bin/python3 path_to_file/file.py

I did with 2 different files (pointing at 2 different dbs): worked both times.
Puzzling.

I think there is a bug in BTT if no parameters are provided. I'll fix that.

Could you try to pass some parameter that doesn't have any meaning in this scenario, e.g. -q

Adding the -q flag did have a positive effect: the script started!

Unfortunately, the script (backup of a local db) gets stuck again.
Same outcome as with attempt n°5/11, above (with import sys).
The function is a simple wrapper around pg_dump.
Yet the backup gets never produced.
(The script works flawlessly when I run it through Terminal).

Running... [PGPASSWORD=xxx pg_dump --dbname=xxx --host=localhost --port=5432 --username=xxx --file=xxx --format=directory --jobs=6 --no-owner --verbose]

Weird, is it maybe expecting some other environment variables? In general there shouldn't be a difference apart from the environment variables I think.