Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load python code at runtime

I would like to load a .py file at runtime. This .py file is basically a config file with the following format:

var1=value  
var2=value  
predicate_function=func line : <return true or false>  

Once this file is loaded, I would like to be able to access var1, var2 and predicate_function. For each line, I'll pass it to the predicate function, and if it returns false, I'll ignore it.

In any case, I'm not sure how to load a python file at runtime and access its variables.

Clarification: there may be any number of these config files that I need to pass to the main program and I won't know their names until runtime. Google tells me I should use __import__. I'm not sure how to correctly use that method and then access the variables of the imported file.

like image 279
Shahbaz Avatar asked Feb 13 '10 22:02

Shahbaz


People also ask

How do I load a Python .PY file?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do you pass a value in runtime in Python?

In Python,we can define a python function at runtime execute with the help of FunctionType(). First we import types module then perform compile() function and pass parameter exec and after that with the help FunctionType() define the function at runtime. Example 1: Function to print GEEKSFORGEEKS.

What is import * in Python?

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.


2 Answers

As written in the python official documentation, if you just want to import a module by name, you can look it up in the sys.modules dictionary after using __import__.

Supposing your configuration is in myproject.mymodule, you would do like that :

module_name = 'myproject.mymodule'

import sys
__import__(module_name)
mymodule = sys.modules[module_name]

# Then you can just access your variables and functions
print mymodule.var1
print mymodule.var2
# etc...

You can also use the return value of __import__ statement but you will have to understand fully how python works with namespaces and scopes.

like image 130
Clément Avatar answered Oct 04 '22 07:10

Clément


You just need to be able to dynamically specify the imports and then dynamically get at the variables.

Let's say your config file is bar.py and looks like this:

x = 3
y = 4
def f(x): return (x<4)

Then your code should look like this:

import sys

# somehow modnames should be a list of strings that are the names of config files
#
# you can do this more dynamically depending on what you're doing                                                                                                     
modnames = ['bar']

for modname in modnames:
  exec('import %s' % modname)

for modname in modnames:
  mod = sys.modules[modname]
  for k in mod.__dict__:
    if k[:2] != '__':
      print modname, k, mod.__dict__[k]

I get this output:

bar f <function f at 0x7f2354eb4cf8>
bar x 3
bar y 4

Then you at least have all the variables and functions. I didn't quite get what you wanted from the predicate functions, but maybe you can get that on your own now.

like image 45
forefinger Avatar answered Oct 04 '22 06:10

forefinger