[Python] ImportError: Start directory is not importable

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    every time that i import this command

    python3 -m unittest discover -s emotion_detection/test_emotion_detection -p "*.py"

    this pops up

    Traceback (most recent call last):
    File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
    File "/usr/lib/python3.10/unittest/__main__.py", line 18, in <module>
    main(module=None)
    File "/usr/lib/python3.10/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
    File "/usr/lib/python3.10/unittest/main.py", line 124, in parseArgs
    self._do_discovery(argv[2:])
    File "/usr/lib/python3.10/unittest/main.py", line 244, in _do_discovery
    self.createTests(from_discovery=True, Loader=Loader)
    File "/usr/lib/python3.10/unittest/main.py", line 154, in createTests
    self.test = loader.discover(self.start, self.pattern, self.top)
    File "/usr/lib/python3.10/unittest/loader.py", line 346, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
    ImportError: Start directory is not importable: 'emotion_detection/test_emotion_detection'


    all i want is to unit test the code i have never done it before so i cant really provide an example of what's supposed to happen here is what the class wants to happen.

    With a functional application being available, you now need to run unit tests to validate the application output.

    To run unit tests, create a new file, test_emotion_detection.py that calls the required application function from the package and tests it for the following statements and dominant emotions.

    Statement Dominant Emotion
    I am glad this happened joy
    I am really mad about this anger
    I feel disgusted just hearing about this disgust
    I am so sad about this sadness
    I am really afraid that this will happen fear

    heres the code


    import requests
    import json

    def emotion_detector(text_to_analyze):
    # Define the URL and headers for the Watson NLP service
    url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict'
    headers = {
    "grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock",
    "Content-Type": "application/json"
    }

    # Prepare the input JSON
    input_json = { "raw_document": { "text": text_to_analyze } }

    # Send a POST request to the Watson NLP service
    response = requests.post(url, headers=headers, json=input_json)

    # Debugging: Print the request and response details
    print("Request JSON:", json.dumps(input_json, indent=2))
    print("Response Status Code:", response.status_code)
    print("Response Text:", response.text)

    # Check if the request was successful
    if response.status_code == 200:
    # Convert the response text into a dictionary
    result = response.json()

    # Debugging: Print the full response dictionary
    print("Response Dictionary:", json.dumps(result, indent=2))

    # Extract the emotions and their scores
    emotions_data = result.get('emotionPredictions', [{}])[0]
    emotion_scores = emotions_data.get('emotion', {})

    anger_score = emotion_scores.get('anger', 0)
    disgust_score = emotion_scores.get('disgust', 0)
    fear_score = emotion_scores.get('fear', 0)
    joy_score = emotion_scores.get('joy', 0)
    sadness_score = emotion_scores.get('sadness', 0)

    # Find the dominant emotion
    emotions = {
    'anger': anger_score,
    'disgust': disgust_score,
    'fear': fear_score,
    'joy': joy_score,
    'sadness': sadness_score
    }
    dominant_emotion = max(emotions, key=emotions.get)

    # Return the output in the specified format
    return {
    'anger': anger_score,
    'disgust': disgust_score,
    'fear': fear_score,
    'joy': joy_score,
    'sadness': sadness_score,
    'dominant_emotion': dominant_emotion
    }
    else:
    # Handle errors
    return {
    'error': f"Request failed with status code {response.status_code}",
    'details': response.text
    }

    # Example usage
    text = "I hate working long hours."
    result = emotion_detector(text)

    # Print the simplified result
    print(json.dumps(result, indent=2))



    screen shot of my file directory

    Continue reading...

Compartilhe esta Página