HOW TO: Do Relative & Absolute Imports (Python Error Explained)

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

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

  • @sonpham791
    @sonpham791 2 ปีที่แล้ว +1

    Thank you so much, that saved my working day!

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

    Thanks so much. This is very helpful! But what if we r still developing the codes? I mean the package is not complete, and thus we don't want to install it into the library. What should we do in such a case if we are using the Spider or VSCode? I know PyCharm has the functionality to define where is the root of project file.

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

      You can install the package in an editable mode.
      ```
      pip install -e
      ```
      This means you can keep coding new things into the package.

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

      thanks so much for ur valuable reply. while do we need to `pip install -e ` multiple times as we constantly develop the codes? Or it's just one-time installation but works as the coding progresses? Besides, as i'm using customed env in miniconda, can we keep it clean after installing my own package? And when debugging the codes, we'll use the codes in the lib directory or still the raw codes in the folder? sorry to bother u with so many questions. really appreciate ur patience

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

      @@jimmy_bytheway_1112 No you don't have to it works even when you create new folders!

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

    import sys
    sys.path.append("..")
    try: from ..user.apps.Calculator import *
    except: from ..user.apps.Calculator import *
    calc.main()
    Doesn't work. Why?

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

      what's the error?

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

      @@chinamatt ImportError: attempted relative import with no known parent package

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

    Very helpful!

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

      Happy to hear it!

  • @ayushman_sr
    @ayushman_sr 8 หลายเดือนก่อน

    try except thing is unique, I have never seen anybody else using this

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

    Typically you don't run your script from root... but from somewhere you might have your data to work on so that doesn't work.
    The other method of python -m .... is also a headache to write.
    Installing package... just doing the monkey-work over and over.
    I'm really disappointed with python. This is just terrible.

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

    u saved my life hehehe tsk