Python Forum
beginner doesn't understand import
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner doesn't understand import
#1
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

Reply
#2
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'
cimerio likes this post
Reply
#3
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).
cimerio likes this post
Reply
#4
oh, I got it now.

thanks for your help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'import Path' do not understand how to use it tester_V 2 2,539 Jun-19-2021, 02:23 AM
Last Post: tester_V
  import scalalib package doesn't work manu_brighter 2 3,724 Apr-17-2020, 06:36 PM
Last Post: snippsat
  I don't understand why this doesn't work sandeen 4 3,572 Dec-19-2019, 07:52 PM
Last Post: sandeen
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 7,721 Nov-03-2018, 06:53 PM
Last Post: j.crater
  Trying to understand how import works in python patrick99e99 3 4,855 Jun-12-2018, 04:48 AM
Last Post: patrick99e99
  import keyboard module doesn't get found - working on laptop but no on raspberry pi.. HANSJORG2 1 9,557 Mar-16-2018, 02:48 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020