Absolute and Relative imports - Python Tutorial 28

preview_player
Показать описание
Absolute = Complete path starting from the project dir
Relative = path using the current file location

Relative imports are as follows

from . import something = from current dir
from .. import something = from parent dir
from .Package import something = from currentdir.Package import something
Рекомендации по теме
Комментарии
Автор

Dude you are the best, i've searched far and wide and you are the only one who provided a good explanation

spoilka
Автор

For someone coming from node to python multilevel import was so confusing and this made it so simple and clear

cerkal
Автор

from package1 import file2 raise Exception ModuleNotFoundError: No module named 'package1'

songhaoli
Автор

This is possibly the best video that explains pythons tricky import situations. I don't normally comment, but thank you so much for this

MrKracx
Автор

This has been a very helpful explanation of absolute and relative imports.

If init files are not required for python > 3.3, why is it still so widely used?

EDIT: Just saw the next video and it answered my question

LeveragedAlpha
Автор

Note to self:

Just don't use relative imports.
Absolute imports are always defined from the root source folder.

novaria
Автор

Thanks for the good quality and the clear explanation

Steamxys
Автор

I really want to thank you for making this topic clear for all

VamsiKrishna-ythi
Автор

how do you get any import to work like that?

Is this special to PyCharm editor?

I'm using VSCode and neither relative imports nor absolute imports work like you have in this setup.

paul_devos
Автор

lifesaver man.. everything was so confusing before this video

engulfing_marubozu
Автор

thank you so much....i was not understanding this for a long

trishandas
Автор

Thank you so much. The best explanation I've ever listen

lepptin
Автор

It would help if you told us how the root directory is determined. Is it simply whichever directory contains the entrypoint file? If so, then this means that the import paths in the module files are determined by whichever file my module files are being imported by (meaning the root file). IOW, if there's more than one root file, the import paths won't work for both root files. This would happen if you have a file for unit tests that's not in the same directory as the root file. If you have a large project with various modules, this could realistically happen. What do we do then?

flippantfishtaco
Автор

what if our root. File is in package1 or package2 and we have to import a module from another package ? is it possible because in my case when I am checking the root path the package1 or package2 depending upon where i open the file and not the parent project directory which is in yours's case

saranzeb
Автор

I have no hierarchy in the folder. All the files are just in a single folder. I am getting import error for pyttsx3 on from .engine import Engine. I am not sure what to do

seeitall
Автор

As long as python searches the home directory first why we need to have module relative imports? Aren't they similar to importing a file located in the same location as the importer file? The difference is files said to exist in a package

freemanfreed
Автор

Hi. Thanks for the video. I got a question and I hope you can help me. I'm currently getting this error: "attempted relative import with no known parent package"
The structure is like this:
Arosics\
_init_.py
CoReg.py
DeShifter.py
My current working directory is Arosics. I want to import a function called DESHIFTER that is defined inside DeShifter.py running the following line "from .DeShifter import DESHIFTER". But I keep getting the error mentioned above. Do you have any ideas what would the problem?

andresespinoza
Автор

I change my vscode setting that the cwd is always the running file, so all absolute import becomes relative. Don’t know it’s a best practice or not.

jackqiu
Автор

why there are underscores between file and number e.g. file_2?

ruhaitian
Автор

How does python know what's the root directory? Why isnt the import e.g. from tutorial_25.package1 import file2?

xkolm