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.
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.
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: th-cam.com/video/sfDSQSj-uvQ/w-d-xo.html - what is a singleton? th-cam.com/video/PBCsN29ZG9A/w-d-xo.html
@@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.
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
Thanks, I find import, modules, packages concepts very confusing in Python
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.
2:04 how to create files in bash using python Set
"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`
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.
my advice is to always use absolute imports and always run scripts via `python -m` (I cover this in the video!)
Any idea where and why __new__ is used in Python?
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: th-cam.com/video/sfDSQSj-uvQ/w-d-xo.html
- what is a singleton? th-cam.com/video/PBCsN29ZG9A/w-d-xo.html
I thought you would slip in the use of __init__.py files in packages. And how to make imports cleaner
eheh -- in my opinion "no code in `__init__.py`"
__init__.py always needed ?
yes and no, technically PEP 420 allows implicit namespace packages, but a lot of tools don't work correctly with them
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'".
use `python -m`!
@@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.
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
@@anthonywritescode Thank you very much for these videos. Did you make any video about this?
yep! th-cam.com/video/hgCVIa5qQhM/w-d-xo.html
Thank you very much. I plan to go through all your videos hhh.
You saved my day ... man