Import of variables from one file to another with FROM

2

The intention is to access from a file (called, say, " index.py ") to the variables defined in another (for example, " config.py " ).
That is, import, to one file, the variables of the other.

Ok, these are the contents of the files:

[ config.py ]

# encoding: utf-8

_root_tit = 'Título de la Aplicación'
_root_w = 1025
_root_h = 600
_root_bg = 'black'
_db_data_conn = {'db_name': 'db_miBase', 'password': 'xxxx', 'charset': 'utf8', 'use_unicode': True, 'user': 'usu_miBase'}

[ index.py ]

# encoding: utf-8

# Recuperando ENTORNO
#-----------------------------------------
from config import *

# Recogiendo datos
#-----------------------------------------
print('o> _root_tit: {}'.format(_root_tit))
print('o> _root_w: {}'.format(_root_w))
print('o> _root_h: {}'.format(_root_h))
print('o> _root_bg: {}'.format(_root_bg))
print('o> _db_data_conn: {}'.format(_db_data_conn))

In principle, this way, it should work since, of the file " config.py " ( from config ) I import all its content ( import * ). In this case, all its variables. It is not like this? And, besides, as I had learned (unless I'm wrong), with this way of importing, you can call the name of the variable directly, for example, _root_tit , without having to precede the name of the variable for the file that is imported, that is, config._root_tit .
Or is it that I'm wrong?

Well, when executing " index.py " in the terminal, I get the ERROR that the variables are not defined. Already, not finding the definition of the first variable called with the print() from the " index.py ", it stops.

" Mensaje de Error "

NameError: name '_root_tit' is not defined

On the other hand, if that works, if I substitute the way to import this:

from config import *

to this:

import config as cfg

Of course, then, when calling each imported variable, I have to put:

cfg.variable_que_sea

So, the question is: Why do you get the variables to import with import config as cfg and not with from config import * ? Would something be missing to get through from config import * ?

I await your clarifying answers. Thank you. Greetings.

EXTRA NOTE: running on Ubuntu 16.04 with Python 2.7 (but, I suppose, this can also be applied to versions 3.x).

    
asked by zacktagnan 16.05.2018 в 14:45
source

1 answer

3

In general the use of from modulo import * is discouraged, because you do not know a priori if what you are going to import will come into conflict with other things that you have imported in the same way (because two different modules could declare classes, functions or variables with same name).

What is recommended is to be more explicit with what you want to import, for example:

from config import (_root_tit, _root_w,
                    _root_h, _root_bg)

The previous version also saves you having to use the name of the module in front of each identifier, but being explicit with what you import and what is not, you avoid the possible conflicts of a from modulo import * .

But answering your question of why the from config import * did not work, the reason is that the syntax * in a import does not matter all the identifiers defined in the module, but only those that are "declared" for it. The statement is to put them all in an array called __all__ .

So, your config.py module should end with a line like this:

__all__ = [ '_root_tit', '_root_w', '_root_h', '_root_bg', '_db_data_conn' ]

The identifiers you enter in that list will be the ones that will be imported when using the expression from config import * . (Those not included in that list can still be imported explicitly, as in from config import foo )

    
answered by 16.05.2018 / 15:33
source