python: explicit relative imports (intermediate) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ม.ค. 2025

ความคิดเห็น • 22

  • @optimiserlenergie1094
    @optimiserlenergie1094 3 ปีที่แล้ว +12

    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.

  • @mrj1997
    @mrj1997 3 ปีที่แล้ว +2

    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`

  • @oscarhagman8247
    @oscarhagman8247 3 ปีที่แล้ว +8

    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 ปีที่แล้ว +10

      my advice is to always use absolute imports and always run scripts via `python -m` (I cover this in the video!)

  • @ejay4032
    @ejay4032 3 ปีที่แล้ว +1

    Any idea where and why __new__ is used in Python?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +1

      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

  • @ejay4032
    @ejay4032 3 ปีที่แล้ว +2

    I thought you would slip in the use of __init__.py files in packages. And how to make imports cleaner

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +1

      eheh -- in my opinion "no code in `__init__.py`"

    • @optimiserlenergie1094
      @optimiserlenergie1094 3 ปีที่แล้ว

      __init__.py always needed ?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +1

      yes and no, technically PEP 420 allows implicit namespace packages, but a lot of tools don't work correctly with them

  • @antonyvigouret9493
    @antonyvigouret9493 3 ปีที่แล้ว +1

    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! th-cam.com/video/hgCVIa5qQhM/w-d-xo.html

  • @Considerationhhh
    @Considerationhhh 2 ปีที่แล้ว

    Thank you very much. I plan to go through all your videos hhh.

  • @zeelthumar
    @zeelthumar 11 หลายเดือนก่อน

    You saved my day ... man