1

I have 2 modules as:

  1. main.py

  2. get.py

In main.py, I need to call a function present in get.py which is call_get(). So I'm trying it this way:

import get get.call_get() 

But it throws an error as 'Attribute Error'. On the other hand, the following code works:

import temp temp.func() 

function func in temp.py looks as:

import get get.call_get() 

I am unable to understand this behaviour. Please guide.

get.py code:

import requests import json import sys import utils import error_handler import get_helper import os import pvdata def call_get(): try: auth_tuple = utils.get_auth_details("get") headers = utils.get_header() resource_types = utils.get_resource_types() namespaces = utils.get_namespaces() if resource_types[0].lower() == "all": resource_types = utils.append_all_resources() get_helper.get_namespace_list(auth_tuple, headers) all_namespaces = utils.extract_namespaces_from_list("source") if namespaces[0].lower() != "all": error_handler.validate_source_namespaces(namespaces, all_namespaces) utils.create_file( "Namespaces", "All_namespaces_at_source.txt", str(all_namespaces)) get_helper.generate_json_for_all_namespaces( all_namespaces, auth_tuple, headers) for resource_name in resource_types: if namespaces[0].lower() == "all": for namespace in all_namespaces: get_helper.call_all_functions_for_get( namespace, resource_name, headers, auth_tuple) else: for namespace in namespaces: get_helper.call_all_functions_for_get( namespace, resource_name, headers, auth_tuple) except Exception as error: filename = os.path.basename(__file__) error_handler.print_exception_message(error, filename) return if __name__ == "__main__": call_get() 

main.py code:

import utils import remote_exec import post import get import error_handler import os import handle_space import socket import json from requests import get import sys import temp def only_dest_requires_jumpserver(): try: dictionary = { "migration_type": utils.config_data()["source_cloud"] + " to " + utils.config_data()["dest_cloud"] } utils.update_config_file(dictionary) print("\nInitialising " + utils.config_data()["source_cloud"] + " to " + utils.config_data()["dest_cloud"] + " migration...") hostname = socket.gethostname() if hostname == utils.config_data()["my_hostname"]: # get.call_get() temp.func() print("\nData successfully exported from source to this machine.\nChecking for space availability at jumpserver...") print("Done!") except Exception as error: filename = os.path.basename(__file__) error_handler.print_exception_message(error, filename) 
5
  • 2
    Show us the code in get.py. You either have a spelling error, or the function is part of a class or something. Commented Jul 12, 2021 at 5:32
  • Do you have circular imports? Commented Jul 12, 2021 at 5:34
  • I have updated the question with get.py code. No cicular imports. Commented Jul 12, 2021 at 5:35
  • What is the code of main.py? Commented Jul 12, 2021 at 5:37
  • I think I have got the answer. The issue is main.py has 2 get modules as: import get from requests import get Commented Jul 12, 2021 at 5:41

1 Answer 1

2

The issue is main.py has 2 get modules as:

import get from requests import get 

get is being overwritten.....you need to rename your function or use

import request request.get 

Another simple way is aliasing suggested by InsertCheesyLine.

from requests import get as _get 

and use _get

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

3 Comments

Wouldn't aliasing either of the get be enough? from requests import get as _get
Indeed, your approach seems to satisfy the OP and is direct. I'll leave my comment for anyone else who searches this.
@InsertCheesyLine modified by answer to include your suggestion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.