Search code examples
pythonimporterroribm-watson

ImportError: No module named watson_developer_cloud


## Packages
import sys
import os
import glob
import json
import matplotlib.pyplot as plt
import watson_developer_cloud

## Cloud service credential connection
discovery_creds = helper.fetch_credentials('discovery')

discovery = watson_developer_cloud.DiscoveryV1(
                        version='2018-08-01',
                        url=discovery_creds['url'],
                        iam_apikey=discovery_creds['apikey'])

## Environment initialization
env, env_id = helper.fetch_object(
    discovery, "environment", "Compugin",
    create=True, create_args=dict(
        description="Compugin 1.0 -- Question/Answering"
    ))

# Lists existing configurations for the service instance and store default configuration id
configurations = discovery.list_configurations(environment_id=env_id).get_result()
cfg_id =  configurations['configurations'][0]['configuration_id']
print(json.dumps(configurations, indent=2))

# List default configuration details
config = discovery.get_configuration(environment_id=env_id, configuration_id=cfg_id).get_result()
print(json.dumps(config, indent=2))

# Test configuration on some sample text
data_dir = "data"
filename = os.path.join(data_dir, "sample.html")
with open(filename, "r") as f:
    res = discovery.test_configuration_in_environment(environment_id=env_id, configuration_id=cfg_id, file=f).get_result()
print(json.dumps(res, indent=2))

When trying to run the above python code, I receive this error:

Traceback (most recent call last): File "compugin.py", line 7, in import watson_developer_cloud ImportError: No module named watson_developer_cloud

I have installed the watson_developer_cloud package using pip, not sure what I'm doing wrong.


Solution

  • There are two worlds when we install packages using pip - Global site-packages and virtualenv packages

    Creating Virtual Environments Python “Virtual Environments” allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally.

    Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python3.6/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

    Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

    Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

    In all these cases, virtual environments can help you. They have their own installation directories and they don’t share libraries with other virtual environments.

    Currently, there are two common tools for creating Python virtual environments:

    venv is available by default in Python 3.3 and later, and installs pip and setuptools into created virtual environments in Python 3.4 and later. virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+, and pip, setuptools and wheel are always installed into created virtual environments by default (regardless of Python version).

    Read installing packages

    To under the differences between the global site-packages and virtualenv packages, refer pip installing in global site-packages instead of virtualenv