Тёмный

python: explicit relative imports (intermediate) anthony explains  

anthonywritescode
Подписаться 42 тыс.
Просмотров 7 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 22   
@optimiserlenergie1094
@optimiserlenergie1094 3 года назад
Thanks, I find import, modules, packages concepts very confusing in Python
@bobbycv64
@bobbycv64 3 года назад
Exactly, python is not a nice language and don't understand the popularity. Kind of like PERL. The code is not maintainable. Thank you @anthonywritescode for your video.
@oscarhagman8247
@oscarhagman8247 3 года назад
If relative imports is not recommended, could you explain what practise IS recommended? I've been struggling a lot of keeping my code tidy since I can't import things from a parent director. My projects always just ends up being a bunch of files in one directory and maybe having 1 or 2 sub directories with stuff that doesn't need code from any files in the parent directory.
@anthonywritescode
@anthonywritescode 3 года назад
my advice is to always use absolute imports and always run scripts via `python -m` (I cover this in the video!)
@consideration8881
@consideration8881 2 года назад
Thank you very much. I plan to go through all your videos hhh.
@antonyvigouret9493
@antonyvigouret9493 3 года назад
I still can't run a nested python file as a script even when I am using absolute import. I get "ModuleNotFoundError: No module named 'a'".
@anthonywritescode
@anthonywritescode 3 года назад
use `python -m`!
@antonyvigouret9493
@antonyvigouret9493 3 года назад
@@anthonywritescode Yep that's what I do but since you mentioned that explicit relative imports were tricky because you can't run them as scripts I thought that we could run absolute import as scripts but in fact you always has to run nested scripts as module with `-m` no matter the way you import packages.
@anthonywritescode
@anthonywritescode 3 года назад
you _can_ with `PYTHONPATH` or editable installs make absolute imports work -- though there are other problems even with that (which I'll probably cover in another video) github.com/asottile/scratch/wiki/PythonPathSadness
@solo_driven
@solo_driven Год назад
@@anthonywritescode Thank you very much for these videos. Did you make any video about this?
@anthonywritescode
@anthonywritescode Год назад
yep! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-hgCVIa5qQhM.html
@zeelthumar
@zeelthumar 8 месяцев назад
You saved my day ... man
@ejay4032
@ejay4032 3 года назад
I thought you would slip in the use of __init__.py files in packages. And how to make imports cleaner
@anthonywritescode
@anthonywritescode 3 года назад
eheh -- in my opinion "no code in `__init__.py`"
@optimiserlenergie1094
@optimiserlenergie1094 3 года назад
__init__.py always needed ?
@anthonywritescode
@anthonywritescode 3 года назад
yes and no, technically PEP 420 allows implicit namespace packages, but a lot of tools don't work correctly with them
@mrj1997
@mrj1997 3 года назад
2:04 how to create files in bash using python Set
@anthonywritescode
@anthonywritescode 3 года назад
"I should really do a video on this" -- so what `echo a{foo,bar}` does it it's a bash expansion to `echo afoo abar`
@ejay4032
@ejay4032 3 года назад
Any idea where and why __new__ is used in Python?
@anthonywritescode
@anthonywritescode 3 года назад
it's the first part of object creation -- you rarely need to use it in user code but here's a few examples from my videos: - how namedtuple works: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-sfDSQSj-uvQ.html - what is a singleton? ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-PBCsN29ZG9A.html
Далее
why I will never use python-poetry
9:09
Просмотров 62 тыс.
Avoiding import loops in Python
10:21
Просмотров 94 тыс.
Top 18 Most Useful Python Modules
10:50
Просмотров 932 тыс.
This Is Why Python Data Classes Are Awesome
22:19
Просмотров 811 тыс.