0

I have tried to include a python script in the project directory. I want to just run getoffense.py(under project1) and 3 scripts will be run indirectly upon executing getoffense.py. Those three scripts are SampleUtilities.py, RestApiClient.py and config.py.these 3 scripts are under "modules"

When i am running this program seperately from django it is working perfectly however when i am using the module path the server gives me error about it.

I have explained as better as i can please help as i am new to python and django. this is my project structure

C:.
├───.idea
└───project1
    ├───modules
    ├───Templates
    └───__pycache__

I want to run these external python scripts and show the result on an html file.

this is my getofense.py

import json
import os
import sys

import importlib
sys.path.append(os.path.realpath('/modules'))
client_module = importlib.import_module('/modules/RestApiClient')
SampleUtilities = importlib.import_module('/modules/SampleUtilities')


def main():

    # First we have to create our client
    client = client_module.RestApiClient(version='9.0')

    # -------------------------------------------------------------------------
    # Basic 'GET'
    # In this example we'll be using the GET endpoint of siem/offenses without
    # any parameters. This will print absolutely everything it can find, every
    # parameter of every offense.

    # Send in the request
    SampleUtilities.pretty_print_request(client, 'siem/offenses', 'GET')
    response = client.call_api('siem/offenses', 'GET')

    # Check if the success code was returned to ensure the call to the API was
    # successful.
    if (response.code != 200):
        print('Failed to retrieve the list of offenses')
        SampleUtilities.pretty_print_response(response)
        sys.exit(1)

    # Since the previous call had no parameters and response has a lot of text,
    # we'll just print out the number of offenses
    response_body = json.loads(response.read().decode('utf-8'))
    print('Number of offenses retrieved: ' + str(len(response_body)))

    # ------------------------------------------------------------------------

    # Setting a variable for all the fields that are to be displayed
    fields = '''id%2Cstatus%2Cdescription%2Coffense_type%2Coffense_source%2Cmagnitude%2Csource_network%2Cdestination_networks%2Cassigned_to'''

    # Send in the request
    SampleUtilities.pretty_print_request(client, 'siem/offenses?fields='+fields, 'GET')
    response = client.call_api('siem/offenses?fields=' +fields, 'GET')


    # Once again, check the response code
    if (response.code != 200):
        print('Failed to retrieve list of offenses')
        SampleUtilities.pretty_print_response(response)
        sys.exit(1)

    # This time we will print out the data itself
    #SampleUtilities.pretty_print_response(response)

    response_body = json.loads(response.read().decode('utf-8'))
    print(response_body)
    print(type(response_body))
    for i in response_body:
        print(i)
        print("")

    for j in response_body:
        print(j['id'])
        print(j['status'])
        print(j['description'])


if __name__ == "__main__":
    main()

this is the error message

 File "C:\celery\project1\project1\urls.py", line 18, in <module>
    from . import views
  File "C:\celery\project1\project1\views.py", line 2, in <module>
    from . import getoffenses
  File "C:\celery\project1\project1\getoffenses.py", line 25, in <module>
    SampleUtilities = importlib.import_module('SampleUtilities')
  File "C:\Users\kiran.tanweer\Envs\celery\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'SampleUtilities'

1 Answer 1

1

Few times ago I had similar issue. I wanted to run some scripts inside django views. Problem was with imports. If you want to import some module in django that is at the same level you have to use "." prefix like:

# Django
from .module import some_func

In Python scripts there's no need to use "." before module name

# Python Script
from module import some_func

I suggest you to try write imports like this:

import importlib
sys.path.append(os.path.realpath('./modules'))
client_module = importlib.import_module('./modules/RestApiClient')
SampleUtilities = importlib.import_module('./modules/SampleUtilities')

Or just using normal imports:

import importlib
from .modules import RestApiClient
from .modules impoort SampleUtilities

Note that there's just some ideas. I'm not sure if thats solution to your problem. Good luck and have a nice day!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.