Python Forum

Full Version: beginner doesn't understand import
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, I don't know if python is different, but

I have a arquivoDados.py file
and a programa.py file in same directory

In arquivoDados.py I have
emp_ati = '000027'
emp_idt = '000054'
emp_pss = '000029'
servidor = 'wagner jose duarte'
and in programa.py file I have
import arquivoDados.py
print (servidor)
but in python It seems that is not that simple. I get the error
Traceback (most recent call last):
File "C:\Users\vitor\Documents\programa.py", line 1, in <module>
import arquivoDados.py
ModuleNotFoundError: No module named 'arquivoDados.py'; 'arquivoDados' is not a package

No .py in import.
>>> import arquivoDados
>>>
>>> arquivoDados.servidor
'wagner jose duarte'

# Or like this
>>> from arquivoDados import servidor, emp_ati
>>>
>>> servidor
'wagner jose duarte'

>>> emp_ati
'000027'
The module name does not include the .py file extension.

This code imports the arquivoDados module and prints information about the module.
import arquivodados

print(arquivodados)
print(dir(arquivodados))
print(arquivodados.__dict__)
Output:
<module 'arquivodados' from 'c:\\Users\\hystadd\\Documents\\python\\sandbox\\arquivodados.py'> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'emp_ati', 'emp_idt', 'emp_pss', 'servidor'] {...'emp_ati': '000027', 'emp_idt': '000054', 'emp_pss': '000029', 'servidor': 'wagner jose duarte'}
arquivoDados is a module object. It is kind of like a dictionary that contains information about the imported module (file). dir(arquivodados) returns the names from the module. arquivodados.__dict__ returns a dictionary mapping the names to attributes in the module. I only include part of __dict__ because the entire thing is long. But notice that 'emp_ati' is a key in the __dict__, ant the attribute is '000027'.

You could use arquivodados.__dict__['emp_ati'] to get the value of emp_ati in the arquivodados.py file, but python has a much better way to access the module attributes:
import arquivodados

print("emp_ati", arquivodados.emp_ati)
print("emp_idt", arquivodados.emp_idt)
print("emp_pss", arquivodados.emp_pss)
print("servidor", arquivodados.servidor)
Output:
emp_ati 000027 emp_idt 000054 emp_pss 000029 servidor wagner jose duarte
You can also use the attribute names in the import.
from arquivodados import emp_ati, emp_idt, emp_pss, servidor

print("emp_ati", emp_ati)
print("emp_idt", emp_idt)
print("emp_pss", emp_pss)
print("servidor", servidor)
Output:
emp_ati 000027 emp_idt 000054 emp_pss 000029 servidor wagner jose duarte
Maybe you wonder why I named the module arquivodados instead of arquivoDados. Python naming conventions is to not use any uppercase letters for module names. From the Python style convention document PEP8.

https://peps.python.org/pep-0008/#packag...dule-names
Quote:Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).
oh, I got it now.

thanks for your help