Import into different directories in Python3

Asked

Viewed 175 times

0

Good night,

I am hard at work trying to make a simple import of modules created by myself into my main code. With everything I always get the second mistake

"attempted relative import with no known Parent package"

Follow the structure of my project.

<DIR> Projeto
------<DIR> config_file
-----------<DIR> func_file
----------------<FILE> __init__.py
----------------<FILE> modulo1.py
----------------<FILE> modulo2.py
------<FILE> config.py
<FILE> main.py

In the case of this hierarchy, what would be the correct way to perform the Imports ?

2 answers

2


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:

  1. dir1 and dir2 must contain a file __init__.py.
  2. dir0, the cwd, does not require a file __init__.py; that file will simply be ignored if present.
  3. 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

  • 1

    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 the from 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.

  • 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

  • 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

  • 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.

  • 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

Show 1 more comment

1

I believe you already know the 'import', however, for larger ways you use the from I’ll give a few examples to be clear

remembering that it also depends on the env if using but an example according to what was passed

# config.py

from config_file.func_file import modulo1

or depending if you are using venv can be this below

# config.py

from Projeto.config_file.func_file import modulo1

You can also import everything into a folder if you use the *, for example

# config.py

from config_file.func_file import *

Now something interesting about the init and all content within the same location

the init is the first location that Python looks at when you call import to the folder

the modules work in a different way when we use them as if they were modules, taking away the need to put the whole path when the import is within them

let’s write some things on __init__

# config_file/func_file/__init__.py

from . import modulo1, modulo2

realize that for the current path of files classified as modules is only one .

and that way you can import only to the folder where the __init__

# config.py

from config_file import func_file

and realize that you can use everything in that folder, like config_file.metodo2.metodo_dentro_do_arquivo

or even do so

# config_file/func_file/__init__.py

from .modulo1 import *
from .modulo2 import *

and called inside their main file so

# config.py

from config_file import func_file

config_file.metodo_dentro_do_arquivo     # sendo o que estiver dentro de ambos os metodos.py

Browser other questions tagged

You are not signed in. Login or sign up in order to post.