Is there any way to get the import variable in the importer?

Say I have two python files

 # importer.py
 parameter = 4
 import importee

and

 # importee.py
 print parameter

Can I or how can I access importer.py in importee.py ? parameter

I use ugly work, occupy (and pollute) sys

 # importer.py
 import sys
 sys.parameter = 4
 import importee

and

 # importee.py
 print sys.parameter

Too ugly. Look for the best solution.

+4
source share
1 answer

The recommended way to achieve what I think you want to achieve is to declare a function in importee and call it, for example:

# importer.py
import importee
importee.call_me(4)

and

# importee.py
def call_me(parameter):
    print(parameter)

It is preferable to avoid performing any operations in the global area. And especially print()nothing, but I suppose your minimal example does not match your real use case :).


, , , . :

# importer.py
import config
config.param = 4
import importee

+

# importee.py
import config
print(config.param)

+

# config.py
param = 7 # some default

- , , , .

+4

All Articles