Help Running a Python Script With Clipboard Contents

Before deciding to post this question I searched through these forums to see possible solutions for the problem I have running a simple Python script. I even saw an old post of mine that had helped me solve a similar problem a while ago. The problem I have now is the following.

I have different versions of Python installed in my Mac. I use Conda to administer the environments. Now, reading the answers to some questions about Python I did the following. First I used the option Execute script file from 'Execute Shell script' and added the following Shebang to the script:

#!/Users/josepm.fontana/opt/anaconda3/envs/py11/bin/python3

This is the path to the version of Python I want to use. When I execute the macro, nothing happens. I have a 'tail' running in the log and I see this message:

2023-04-21 18:38:12 Execute macro “CleanURL” from trigger The exact case string “url4” is typed (then deleted)
2023-04-21 18:38:15 Running application query took a while (5003 us)

Then I tried what worked for me last time. This involves using Execute text script with the following lines:

#!/usr/bin/env zsh -f

python3 /Users/josepm.fontana/Scripts/clean_url.py

This seems to work in the sense that it is clear that it executes the script but I get an error that shows that it is not using the version of Python I need to use:

2023-04-21 18:29:01 Action 56387 failed: Execute a Shell Script failed with script error: Traceback (most recent call last):
  File "/Users/josepm.fontana/Scripts/clean_url.py", line 4, in <module>
    import pyperclip
ModuleNotFoundError: No module named 'pyperclip'

The problem is that I don't even know what version of Python is being used. I have done a pip install of pyperclip on the 3 versions I installed via Conda but the shell must be pointed to another one which could probably be the one that MacOS installs by default. That version is way too old and I don't want to use it.

Any suggestion that helps me solve this problem will be very welcome.

Don't you use Conda to load an "environment" (including modules) and then run your Python script in that? In which case you'll probably need to do the same in the KM "Execute Shell Script" action. Start your fact-finding in the Terminal with condo info --env -- the active environment will be asterisked.

You may also have to add the correct entries $PATH entries -- you can run the "correct" Python as you did, but it won't know where to look for anything it needs -- but first you need to find out where those modules are installed! I'm hoping that loading the right Conda env will take care of that for you...

Thanks Nige_S. The conda environment is the one that is in the shebang at the beginning of the script. The Python version I want to use is in:

/Users/josepm.fontana/opt/anaconda3/envs/py11

The problem is, I don't know where I have to specify this in the one of the two alternative ways to execute a script that actually works for me, i.e. the option 'Execute text script' that starts with:

#!/usr/bin/env zsh -f

What should I have after this to use the version of Python that lives in:

/Users/josepm.fontana/opt/anaconda3/envs/py11/bin

Usually (in my very limited experience!) you create a conda environment, you activate that environment, you then run Python under that particular environment (see https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-python.html). How are you actually switching environments in Terminal?

It may be that, even though you are specifying a full PATH to the particular version of python3 you want to use, any "extras" can't be loaded because they aren't available via the KM shell PATH environment variable. You could try adding that to the beginning of the ENV_PATH variable in KM's Settings->Variables:

(note the : between each entry) -- but while that may be enough for the shell, it may not be for Python to find the required modules (which is where loading the correct environment comes in).

Again -- post the output from running condo info --env in Terminal. That'll give some clues as to what might be required. Remember that Terminal and KM shells are different -- even if Terminal always loads/uses the correct environment without you doing anything, KM's "Execute Shell Script" action never will unless you tell it to.

Thanks Nige_s. In the end I solved the problem. The thing is that I did by doing something that is not what I had understood one should be doing by looking at the KB documentation. I simply added the command I would use to execute the script from my terminal into the 'Execute text script' window:

~/opt/PathToCondaFolderForSpecificPythonVersion/Python3 'PathToPythonScript/script.py'

No shebang, no ENV_PATH, no nothing. Just use the 'Execute text script' window as if it were the terminal. It worked.

So it was a PATH problem...

That works because that "version" of Python "knows" where to look for any extras (modules etc) it needs in that environment -- which is a relief!

You can always reference things specifically, to avoid setting ENV_PATH, but that can get cumbersome if you have to do it a lot. That's what PATH and KM's ENV_PATH are for -- "if this thing isn't at where I just said, look for it in these places in this order" (remembering that if you just type the command python3 the shell will first look for a match in the current working directory).

But have another look at the Anaconda documentation. It sounds like your different "environments" are wholesale duplications of python3 plus all the things required in that environment, which is rather inefficient (but very portable). The more usual way is to "load" whichever environment you want to use, which creates the setup you want on the fly from the various bits and pieces required -- a more flexible way to manage things.

I only know the above because our High Performance Computing admin gets very sweary when people use the first method, unnecessarily using up disk space -- I could have misunderstood, so YMMV.

Thanks Nige_S. You are right that the conda environments are duplications of python3 but I don't have that many environments installed and they do not take a lot of space. It saves me a lot of headaches I used to have when I used a different system.

1 Like