You can simply name the directories separated by .
when importing. For example, if I am working in the directory dir0
and if my package mod.py
is in dir0/dir1/dir2/mod.py
, can do:
import dir1.dir2.mod
According to the book of Lutz (Chapter 24), such a relative import requires the following rules to be observed:
- dir1 and dir2 must contain a file
__init__.py
.
- dir0, the
cwd
, does not require a file __init__.py
; that file
will simply be ignored if present.
- dir0, not dir0 dir1, must be listed in the search path of the
module
sys.path
.
Therefore, for your case (assuming you are working on the folder Projeto
), just do:
import config_file.func_file.modulo1
import config_file.func_file.modulo1
All common import properties remain valid. In addition to from
, it is also possible to use as
to reduce the inconvenience of using long names. For modulo1, for example, we would have:
import config_file.func_file.modulo1 as mod1
Read chapter 24 of the cited book for more details on importing modules in Python.
EDIT: Changing the path to perform direct import
Relative import works as described above. However, there is also the option to import absolutely, which has the advantage that you don’t have to keep remembering the entire drawing of the directory and, in addition, you can import external modules to the current directory.
To make the absolute import just add the path
where are the user-defined modules to the list of paths already used by default. To see the default list of paths, just do:
import sys
print(sys.path)
And to add a path to the list, type:
sys.path.append("PATH/dir0/dir1/dir2/")
Where PATH
is the path on your computer to the folder dir0
.
And then it is enough to make the direct import:
import mod
The right way to use the
import
of a module from within several directories is to use the from, if someone uses the way described, when using what is being imported, the person will have to use the full path again for example a = config_file.func_file.modulo1.metodo_dentro_do_file– Guilherme França de Oliveira
Both ways are correct. The inconvenience of the big name does not prevent the import from working and, moreover, this inconvenience can be easily overcome using
as
as is widely known. Your answer is wrong in stating that the import with thefrom
is a necessary condition for the relative import to function. In addition, you also made a mistake in putting the file extension to be imported in your reply. Import needs to be done without declaring the file extension.– Lucas
I’m sorry, but if we’re to pass on some instruction, in a way that’s easier to learn than something that makes it harder, it’s not that it works that way, if someone’s learning something, that’s the best way and that’s better for learning, i can describe in an answer that works in Python 2.7 and 3, but soon Python lost support for Python 2.7 and this would cause problems for those who will have to fix their codes soon after the event, we must be intuitive, an example
– Guilherme França de Oliveira
Yes the import does not need the extension, it was a typo, but I did not deny it for malice or bad faith, but I do not see as a useful response according to what I described above
– Guilherme França de Oliveira
Yes, but if in your opinion my answer just needed an adjustment to make it easier for the author of the question, the most ethical was to comment on it and I would edit it then.
– Lucas
I’m sorry, but it’s not a copy, several questions I had arguments and I stopped answering because I already had a complete answer, I published my answer because I had something else to increase something that your answer did not have, just read both questions, I don’t decide who is right, but if I see content that should be added, I think the best is to expose them and yes, I would comment yes here for you describing why, I just didn’t have time to finish and would remove the negativity from my part if it has been modified
– Guilherme França de Oliveira