Cannot import certain python modules

When executing python, import pymysql and from bs4 import BeautifulSoup both give ‘ImportError: No module named xx’.

When I run this script using PyCharm, the error isn’t shown. How can I solve this?

And when I include the full path:

#! /Library/Frameworks/Python.framework/Versions/2.7

I get a permission denied error

Hey Marijn,

When Keyboard Maestro runs a shell script it is in a clean shell with no access to your .profile or .bash_profile files that load up in your normal Terminal environment.

So any modifications to those that allow Python to see its modules are gone and must be accounted for in whatever script you run.

What’s the $PATH in PyCharm (or the Terminal)?

What’s the $PATH in Keyboard Maestro’s Execute a Shell Script action?

echo $PATH

And find out.

I add this line to many of my shell scripts in Keyboard Maestro, so software I’ve installed is seen by the shell – and I don’t have to provide full paths to the executables.

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;

-Chris

2 Likes

Hi Chris,

Thanks for your reply!

I played around a lot but I can't get it working. I got this array back when I echo'd $PATH in PyCharm.

['/Users/Marijn/PycharmProjects/untitled', '/Users/Marijn/PycharmProjects/untitled',       '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

I broke them down to two main paths which needed to be reached when executing the python code in KM;

/Users/Marijn/PycharmProjects/untitled
/Library/Frameworks/Python.framework/Versions/2.7/

Result: /opt/local/bin:/opt/local/sbin:/usr/local/bin:/Users/Marijn/PycharmProjects/untitled:/Library/Frameworks/Python.framework/Versions/2.7/, awesome!

When I include this above my python script:

What can I do to solve it? If I run the export inside a separate 'Execute Shell Script' box, it doen't work either, properly because it runs in separate shells.

See the Execute a Shell Script action.

Shells are executed in non-interactive move (see the INVOCATION section of the base man page). As such none of your profile scripts (like ~/.profile) will be executed. This means that custom environment variable settings will not be applied, in particular any settings for your PATH environment variable, and any settings for tool-specific environment variables like PERL5LIB). You can set these by creating a variable with “ENV_” at the start (eg ENV_PATH or ENV_ PERL5LIB) and settings its value appropriately.

Most likely you have python-specific environment variables configured in your shell profile files that are not executed in non-interactive shell invocations.

Hi Peter, thanks for your reply!

I have created the ENV_PATH variable and filled it with the following data:

/opt/local/bin:/opt/local/sbin:/usr/local/bin:/Users/Marijn/PycharmProjects/untitled:/Library/Frameworks/Python.framework/Versions/2.7/

Is it right I don’t have to include anything in the shell script itself,
like you said in an other topic? At the moment I get the same error’s as
before.

With #! /usr/bin/python at the top:


without:

In the Terminal, type env and in Keyboard Maestro, Execute Shell Script “env” and then compare the two and look for differences, especially differences related to the PATH and related to python (eg anything like “PYTHONPATH” or such, I don’t know python, though a quick Google search finds some of the environment variables.

For each one that is different, create a matching Keyboard Maestro variable named ENV_, like ENV_PATH and ENV_PYTHONPATH and set them to the values shown in the Terminal.

1 Like

Thanks for your reply, Peter. Sorry for my late answer, had some problems to solve with using the terminal.

Unfortunately, the path is the same in my home directory as in the projectfolder.

When I print sys.path, however, the long array comes back.

['/Users/Marijn/PycharmProjects/untitled', '/Users/Marijn/PycharmProjects/untitled',       '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

I adjusted the ENV_PATH variable to the following line:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Marijn/PycharmProjects/untitled:/Library/Frameworks/Python.framework/Versions/2.7/ 

This includes the full path from the terminal plus the two directories which PyCharm uses.

When I execute the following code in KM, I get the known command not found errors.

The weird thing is, /usr/bin(/pyhton) isn't even found. When I include #! /usr/bin/python I only get errors for the external python modules (like requests).

When I echo $PATH in a separate 'Execute Shell script' I do get the value of ENV_PATH:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Marijn/PycharmProjects/untitled:/Library/Frameworks/Python.framework/Versions/2.7/

And when I add /usr/bin/pyhton to ENV_PATH, it still acts like there isn't #! /usr/bin/python above the script (lots of errors of 'command not found'), like the ENV_PATH doesn't get loaded when executing python, but does allow it to print in a separate 'Execute shell script'.

So I'm a bit lost. Can you shed some light on this?

Martin
you are trying to run python code from a shell/bash script.
type #!/usr/bin/python at the top of your script. Otherwise KM will think is a regular bash script

Thanks for helping out. I did try that as well. The error I get is as following:

Traceback (most recent call last):
  File "/var/folders/bv/58d872g57zb4slyrlfwf14gw0000gn/T/Keyboard-Maestro-Script-A2283818-C152-4C0A-8E58-78919E643584", line 6, in <module>
    import requests
ImportError: No module named requests

you can:

  1. install package requests first (/usr/bin/pip install requests. do this for any package)
    or the other option:
  2. set the right path to your python bin in the top of the file

Did try it, but doesn't really work

try:

`which pip` install requests
`which pip` install beautifulsoup4

if it has errors. try it with sudo

I’m sorry, but this doesn’t work either. It did work after I uninstalled both modules, but they won’t work in KM. I’m open to other suggestions, as I’m clueless. Have others the samen problems with importing (downloaded) modules?

See if this thread has any hints for you:

-Chris