Python - Pass parameter instead of modules in FROM declaration

Asked

Viewed 183 times

-2

Good afternoon, everyone.

I am in a dilemma when making the FROM statement. import to import the modules, as follows below:

  • For this utility, I do not want and cannot pass the FROM.. import declaration at the beginning of the script, I need to call directly in the function. Here is a problem, because it is a function somewhat generic, it was to be at least. It is a function that depending on the option the user chooses will need a different module, so my idea is to pass the modules by variable and use these variables in the FROM declaration.. import as follows below:

both the module directory name and the class name is test, for example.

var_concorrente = teste
from + var_concorrente import var_concorrente

the problem is that this way does not work.

Could someone help me?

I don’t want to make a very big script and it will be if you’re not good, if you can’t find a solution to this.

Let me try to improve my question.

The normal from would be like this:

from source.netspeed.netspeed import Netspeed

source is the name of the directory containing all the modules.

The first netspeed is the client directory

The second netspeed is the name of the module

Netspeed is the name of the class inside the netspeed module

I have to go through all this for parameter, like this:

client = "netspeed" from_estructure = "source."+ client+"."+ client

declare from using these variables:

from from_estructure import client.capitalize()

Grateful to all.

  • Can you give a concrete example of what you want to do? For example, showing at least two different modules that would be imported by the function, show what is the idea of this function and what would be the calls to each module?

  • Let me try to improve my question. The normal from would look like this: from source.netspeed.netspeed import Netspeed The source is the name of the directory that contains all the modules. The first netspeed is the client directory The second netspeed is the module name Netspeed is the class name inside the netspeed module I have to pass all this by parameter, like this: client = "netspeed" from_estructure = "source."+ client+"."+ client declare from using these variables: from from_estructure import client.capitalize()

  • The name of the module at all times is the same as the client directory name? And the class name, will always be Netspeed, client independent?

  • The module name will always be the client name with low box, and the Class will always be the module name with first letter high box.

  • Do all classes that can be imported have the same contract? That is, it will be guaranteed that the methods and attributes you use in the function exist in all possible classes?

  • No. Several functions not equal, but each client has a specific function, so I’ve separated them into modules. Otherwise I’d use them all in the same module, I guess.

  • So after importing the class how will you know which methods you can use from it? This is looking like a XY problem, in which you try to implement something you think is the solution, but it is not.

  • Why this will be called via the menu created for this. I have a menu to, for example, print a sum, so when the client chooses this menu, it will be this information passed by the parameter.

Show 3 more comments

2 answers

1

It is possible to import a module with the name into a string, rather than directly typed into the code, with the built-in function __import__. The import command actually requires the name to be imported to be set.

The function __import__ does not have an option to from, instead, you can make a simple assignment. If the name of what you want to import inside the module is also variable, you can use the getattr

def minha_funcao(nome_modulo, nome_objeto):
    modulo = __import__(nome_modulo)
    objeto = getattr(modulo, nome_objeto)

    ...
  • I am not yet at that level of understanding. Let me continue my questioning based on your suggestion. E if the class is in the following structure: directory "source" Inside "Source" I have the directory "Netspeed"

  • __import__("source.Netspeed")

  • (Please, uppercase and minuscule have to be deterministic "source" and "Source" are different things. )

  • So I understand the issue of being case sensitive. I appreciate the touch. I’m trying like this: Concurrent = self.config.converter var_modulo = str("source."+Concurrent+"."+Concurrent) , but it’s not working, it only finds the "source". how to do?

0


I managed to resolve the issue above the mode below:

import_module = str("source." + concurrent + "." + concurrent)
import_class = concurrent.capitalize()
from_estructure = __import__(import_module, fromlist=import_class)
  • But that’s basically what jsbueno answered, no?

  • lacked the use of "fromlist=". without it not certain.

  • And the getattr does not solve this problem?

  • So, I appreciate the placements here. import and then getattr can do with it too. Thanks again

Browser other questions tagged

You are not signed in. Login or sign up in order to post.