Importing Your Own Python Modules Properly

Поделиться
HTML-код
  • Опубликовано: 3 янв 2025

Комментарии •

  • @robbiefryers13
    @robbiefryers13 2 года назад +203

    You're confusing the term module with what is actually a package in python. Any file with a py extension is a 'module' in python. A collection of py files in a directory along with an __init__ file is a package, not a module

    • @PavitraGolchha
      @PavitraGolchha Год назад +10

      It's actually python which made this confusing. JS ESM is so simple

    • @raenastra
      @raenastra Год назад +5

      @@PavitraGolchha JS imports may not be the best example of simplicity, due to the competing standards, but I get what you mean

    • @BooklyCrashCourse
      @BooklyCrashCourse 9 месяцев назад +1

      It is kind of confusing. Also a package can be comprised of packages.

  • @bloodywolftr
    @bloodywolftr 2 года назад +18

    I was looking for init usage and most of the tutorials in the wild were showing importing files just from next to the main python file.Now this is useful.Thanks.

  • @kaushiksinghrawat
    @kaushiksinghrawat Год назад +6

    Was stuck on this for almost two days with most solutions on stack overflow and chatgpt suggesting to use absolute paths, instead of this type of relative pathing to import stuff.
    Finally managed to understand it 😆

    • @Ridwan-vb8nm
      @Ridwan-vb8nm 5 месяцев назад

      yeah if you want to do "real work," chatGPT just crashes.

    • @terryjophlin
      @terryjophlin 4 месяца назад

      ​@@Ridwan-vb8nmI am amazing at debugging because I used ChatGPT to learn to code. I really don't recommend it.

  • @aceldama
    @aceldama 2 года назад +23

    nice. only thing i'd add is that you can import from a parent directory using .., so it would read "from ..parent_directory import y"

    • @authentic_101
      @authentic_101 8 месяцев назад +1

      But this hasn't worked for me so far. Getting an error citing something about "unspecified parent package"

    • @aceldama
      @aceldama 8 месяцев назад

      @authentic_101 there are a few rules you need to stick to. Every directory you import from must have an __init__.py file, for example.

    • @authentic_101
      @authentic_101 8 месяцев назад +1

      @@aceldama this I tried actually

    • @felixmerz00
      @felixmerz00 4 месяца назад

      Unfortunately, this doesn't work for me either.

  • @somecallmetimelderberries432
    @somecallmetimelderberries432 Год назад +8

    Nice explanation, best one yet I've seen on modules!

  • @sirsquirrel0
    @sirsquirrel0 Год назад +5

    Thanks for the video and making the effort. I found it quite difficult to see which files you were editing with the editor. I have rewatched a few times, but its blowing my mind.

    • @DokterTonga
      @DokterTonga Год назад +2

      I had the same. The file he is editing is named below (bottom)

  • @Dinco422
    @Dinco422 Год назад +1

    As a noob, I've watched this and still have no idea how to add modules, thanks.

  • @siddhubhai2508
    @siddhubhai2508 5 месяцев назад +2

    Your explaination is soooooo cooooool like your intro! Your explaination made me your fan only in one video, ultra cool bro! I'm glad you have a youtube channel so that I can learn from you!

  • @subi_rocks
    @subi_rocks 2 года назад +6

    What if I want to import fourth.py inside second.py and then call second.py from main.py?

    • @CattleRustlerOCN
      @CattleRustlerOCN 10 месяцев назад +1

      In second.py Import Fourth, in main Import Second
      maybe 😊

  • @stevenhoang7297
    @stevenhoang7297 Год назад +1

    what's your vim setup 👉👈

  • @Cordic45
    @Cordic45 2 года назад +1

    what does . mean at 8:31?

  • @DJaycerOfficial
    @DJaycerOfficial Год назад +1

    Making a text based rpg, and really needed a way for stats of enemies to be imported properly. I try not to use videos unless I’m A. Learning something or B. Completely lost and have no idea of the full concept of something.

  • @philipp4980
    @philipp4980 2 года назад +3

    Is that also possible to import the modules without the "from..."?
    E.g. with "import functionality" and call the functions with "functionality.add()".
    In comparison when I import the "math" module I use that method and can call "math.pi" afterwards.

  • @soklagenhet
    @soklagenhet Год назад +3

    Great, but you really tend not to use the star * import, it's generally bad practice as it bloats your namespace with potentially unwanted modules

    • @royw2
      @royw2 4 месяца назад

      Small programs with a single developer may be able to get away with wildcard imports. In any serious program wildcard imports can introduce hard to find bugs when multiple packages just happen to have the same module name so import order determines which module is actually imported. This is why wildcard imports are a code smell and in truth, just evil. Modern IDEs facilitate explicitly importing the module you want. Please, please get in the habit of avoiding wildcard imports.

  • @amiralizadeh6621
    @amiralizadeh6621 2 года назад +3

    what if our module is in another directory? should we amend the system path?

    • @benjaminnelson5455
      @benjaminnelson5455 2 месяца назад

      This is what I came here hoping to learn. I just spent 2 days dealing with all manner of issues with namespacing in an ultimately vain attempt to keep the test coverage in a discrete folder from the production code.

    • @Jomster777
      @Jomster777 13 дней назад

      So just to answer this question for anyone coming across this same curiosity as I have, we apparently have to use the _sys_ module built into python where we can use the sample import function _sys.path.insert(0, '')_

  • @MrCipek1221
    @MrCipek1221 2 года назад +14

    Thank you! :) I always learn something new with your videos :D Please keep it up!! Very well done :)

  • @katlegophele8909
    @katlegophele8909 Год назад +2

    Tutorial on how to setup the environment like yours please

  • @Lefthandup
    @Lefthandup 2 года назад +1

    Is there a library I can import that will allow me to read in user input without them having to press enter? Like for a WPM tester for example to see if they had a typo

    • @mables8698
      @mables8698 2 года назад +1

      Yes, but which one depends on your project.
      For a typing tester in the console you can check out this video from Tech with Tim:
      ruclips.net/video/NQ5i1kJAA6Y/видео.html
      Also, you could create something with pygame, which checks continually for input.

    • @Lefthandup
      @Lefthandup 2 года назад

      @@mables8698 wow this is exactly what I was trying to do thx

  • @supermankelly
    @supermankelly 7 месяцев назад

    What if the module file is not under the project folder. How do you define a totally different path?

  • @404errorpagenotfound.6
    @404errorpagenotfound.6 2 года назад +2

    Please do a video on different IDE's, pros, cons etc.
    Thanks for your great comment.

    • @Lefthandup
      @Lefthandup 2 года назад +1

      I can recommend pycharm for python and blueJay for Java

    • @404errorpagenotfound.6
      @404errorpagenotfound.6 2 года назад +1

      @@Lefthandup but more people especially on YT are using VS Code.

    • @stwni
      @stwni 2 года назад +1

      @@404errorpagenotfound.6 Vscode isn't really an IDE it's a text editor

    • @404errorpagenotfound.6
      @404errorpagenotfound.6 2 года назад +1

      @@stwni what does an IDE do that VS doesn't? It's incredibly popular with YT programmers teaching coding.

    • @Lefthandup
      @Lefthandup 2 года назад +1

      @@404errorpagenotfound.6 I’m pretty sure an ide has the advantage of using other peoples libraries etc.

  • @matteodelgrosso4364
    @matteodelgrosso4364 Год назад +2

    The terms module and package are mixed up! A module is a python file. A package is a collection of python modules!

  • @ninavanleeuwen1183
    @ninavanleeuwen1183 9 месяцев назад

    THIS!! This is exactly what i have been looking for! Thank you so much

  • @bobjohnson3427
    @bobjohnson3427 7 месяцев назад

    Thanks, that clears up several of my problems that other resiurces weren't helping me resolve. I also have a couple of self-made modules that I use in multiple projects. Do you have any tips for how to access them using the environmentsl vafuable PATH or PYTHONPATH?

  • @MagnusAnand
    @MagnusAnand 2 года назад +1

    But why use * in the first place? It's supposed to be an antipattern because of namespacing issues.

  • @hovh03
    @hovh03 11 месяцев назад

    thanks you for very clear explanation. What about a source module downloaded from github and build by me? It is in a different location, not sub directory.

  • @ChristopherBruns-o7o
    @ChristopherBruns-o7o 6 месяцев назад

    this combined with if __name__ == "__main__": would have been the best youtube in history.

  • @MrJet84
    @MrJet84 Год назад

    Thank you for lesson. But how import from upper module to lower?

  • @AhmedThahir2002
    @AhmedThahir2002 2 года назад

    According to pyinstaller's official documentation
    "
    It is not a cross-compiler
    - to make a Windows app you run PyInstaller on Windows
    - to make a Linux app you run it on Linux, etc
    "
    Any workaround this?

  • @canozer1954
    @canozer1954 Год назад

    When i try to run my code which is 2 python file inside a folder with all import rules, i am facing __pycache__ and code does not running. I cannot get input some print ext. How can i fix this.

  • @saurabh7489
    @saurabh7489 Год назад +2

    Thank You Sir. You went into great length to explain this concept.

  • @H4KnSL4K
    @H4KnSL4K Год назад

    Thanks for the video. I recommend some daily careful typing practice, btw - so you don't need to constantly correct your typing

  • @austinmoody4719
    @austinmoody4719 Год назад

    any idea why my vs code isnt importing files from the same directory?

  • @pabloalonso9083
    @pabloalonso9083 Месяц назад

    One video like this but explaining shared variables between modules I think would be perfect !!

  • @jameswilliams-nw3lq
    @jameswilliams-nw3lq Год назад +1

    Thank you SO much. This was a difficult concept for me to grasp but so helpful. Your awesome

  • @kcvinu
    @kcvinu 2 года назад

    Learned a new thing. "__all__". Thanks a lot.

  • @stormbytes
    @stormbytes 2 года назад +18

    Really cool video and explained very clearly. What about making a video on packaging modules for sharing?

  • @dragnar4743
    @dragnar4743 9 месяцев назад

    Dude, the practical explanation was very good. In fact, on of the best on yt. But, I think you should have termed the directory containing all those modules as Package. Because, most of the beginners get little confused with term module, package and library. otherwise.... 👍👍

  • @akshatsharmasre
    @akshatsharmasre Год назад

    Does this work if the folders are in git repo?

  • @xiupingli2892
    @xiupingli2892 7 месяцев назад

    I tried to import my own module that uses pandas or yahoo finance. but it did not work. How can i solve it

  • @patrikpatrik8421
    @patrikpatrik8421 Год назад +2

    Quick and simple explanation thank you !

  • @bla7091
    @bla7091 Год назад +1

    Good stuff! I did notice that if I package everything into a wheel, without adding the classes to the __init__.py I was getting ModuleNotFound errors whenever I used submodules from the package.

  • @ShadyNetworker
    @ShadyNetworker Год назад

    Yooo, what is this terminal program and setup, it looks sexy. Thanks for the video!

  • @yashasvimantha6012
    @yashasvimantha6012 2 года назад

    You didn't talk about having the module not in the same directory and using it.

  • @szenghoe
    @szenghoe 2 года назад

    why we dont have to declare submodule in the init file?

  • @ranknwarjitsinghdhaliwal9856
    @ranknwarjitsinghdhaliwal9856 Год назад

    what ui is this??? sorry, new to this

  • @amit12000
    @amit12000 Год назад

    how use linting in neovim using pylint and how to avoid import error

  • @YoubiTheGreat
    @YoubiTheGreat Год назад

    I'm using VS Code. I followed pretty well until I got to __all__ = ["second", "third"]. second and third are grizzled underscored with a message ""second" is specified in __all__ but is not present in module Pylance".I'm still searching for a way to make it work. Note : from othermodule import *, second.myfunction() works and I get an Hello anyway but I still don't know why it works if I get errors in my code. Just to add. When I added the from .second import myfunction, from .third import another_function in the init file, under __all__ line, those errors are gone. Thanks for the video, it helps a lot.

  • @jakobmatt8632
    @jakobmatt8632 Год назад

    Im having trouble with this using gRPC I'm importing with from gRPC.python import * I have an __init__.py in the python directory do I somehow also need one in the gRPC directory which is the parent?.

  • @danielpartyhead2234
    @danielpartyhead2234 Год назад

    all good. but it dont see import pandas as pd in function :(

  • @Cordic45
    @Cordic45 2 года назад

    what is this test editor name?

  • @Person-hb3dv
    @Person-hb3dv Год назад

    you saved me a lot of time with this video. thank you. earned my sub

  • @CiaoKizomba
    @CiaoKizomba 3 дня назад

    Can you do how to set up PYTHONPATH so that the program can find the modules

  • @stephen285
    @stephen285 Год назад

    fyi ~ this did not work for me, yes I had my functions file in the same directory as my blend file, but still I had to add the explicit path as follows, then it worked as shown in video
    import bpy
    import system
    sys.path.append('E:\content\working\latest')
    from func import testfunc

  • @donjackson3520
    @donjackson3520 Год назад +1

    Excellent video! I’m new to python and this is a big help

  • @Tom-JS
    @Tom-JS Год назад

    When the teacher says add every number from to 100 instead of doing this 1 +2+3
    Of a text editor and type this
    total = 0
    for num in range(101):
    total = num + total
    print(total)
    It will print the correct answer 5050

  • @mathsfun4097
    @mathsfun4097 6 месяцев назад

    I was import error like they can't be imported from my module

  • @timshort9787
    @timshort9787 2 года назад

    Please make a tutorial on how you set up your neovim environment

  • @ahmadov01
    @ahmadov01 2 года назад

    What is this ide name ? Is it vim ? May you send config ?

  • @tki967
    @tki967 2 года назад

    can you please put module and code(main) at different folder and access module from code

  • @swifttech7853
    @swifttech7853 2 года назад +1

    BROTHER, YOU ARE THE BEST!!! You oooh really helped me!! THANK YOU VERY MUCH!

  • @manfrombritain6816
    @manfrombritain6816 Год назад

    most of the time i love python but there's something about the way we do this stuff that really does not compute in my head. the file structure of flask apps in general just doesn't work in my head... i find JS frameworks much more simpler to grasp for god's sake

  • @salarghaffarian4914
    @salarghaffarian4914 Год назад

    Really informative video. Thanks for recording and sharing it.

  • @vinayakpandey6299
    @vinayakpandey6299 Год назад

    very nice explanation. understood everything very clearly

  • @eton9931
    @eton9931 Год назад

    Wow, that was actually pretty easy
    Thanks you sir

  • @sahilgarg94
    @sahilgarg94 2 года назад +1

    Thank you very much sir .... This was badly needed...

  • @kevintanudjaja7597
    @kevintanudjaja7597 9 месяцев назад

    ah.. why not cover sibling module..

  • @zakaryreilly
    @zakaryreilly 10 месяцев назад

    Not clear on package/module/sub-module terminology. A bit confusing.

  • @Anonymous-ri4mk
    @Anonymous-ri4mk Год назад

    I am not gonna lie sometimes I get lose in the middle of my code and get confused

  • @tsvasekar
    @tsvasekar 2 года назад +1

    Needed exactly this. Trying to add a pre built library to a system where I can't use pip, or setupmodule. I hope this will work.

  • @reviii-s4t
    @reviii-s4t 2 года назад +1

    Thank you so much for this, very clear explanation.

  • @iloloey
    @iloloey 9 месяцев назад

    Fantastic explanation.

  • @paulohsgoes1959
    @paulohsgoes1959 2 года назад

    Excelent job!
    Congrats

  • @scottielin1076
    @scottielin1076 Год назад

    Very clear explanation Thanks 🎉

  • @earthchan568
    @earthchan568 Год назад

    thank man u saved my so much time

  • @krystiansawicki4985
    @krystiansawicki4985 9 месяцев назад

    Thank you this helped me a lot

  • @heathergray4880
    @heathergray4880 2 года назад

    I have always been told that start imports are bad

  • @GuillermoGarcia75
    @GuillermoGarcia75 8 месяцев назад

    GOING Awesome!! Still great!!

  • @petereziagor4604
    @petereziagor4604 2 года назад

    Nice video, I have to learn to use vim like you

  • @anurasenarathna1703
    @anurasenarathna1703 Год назад

    Very informative. Thank you very much.

  • @arcanelore168
    @arcanelore168 11 месяцев назад

    I use neovim on KDE Plasma!

  • @rizaldicakra826
    @rizaldicakra826 Год назад

    It works.
    Thank you, really help my project :)

  • @JamesThiering
    @JamesThiering 2 года назад

    Thanks for the helpful video!

  • @redlight8752
    @redlight8752 9 месяцев назад

    Thanks bro, much love

  • @tehreemakhtar8652
    @tehreemakhtar8652 Год назад

    Impressive. To the point content

  • @tejasnirantar
    @tejasnirantar Год назад

    thx for this, really helpful

  • @abba6104
    @abba6104 Год назад

    Honestly it was a bit hard to follow due to the strange IDE or whatever. Pretty good tutorial I suppose but presentation was a bit unclear.

  • @hecker5563
    @hecker5563 11 месяцев назад

    Nice tutor!

  • @BeeryDorin
    @BeeryDorin 2 года назад

    thanks a lot, solved many problems.

  • @caribbeanchild
    @caribbeanchild 11 месяцев назад +2

    You should have done this in Blender. This is no help at all. You keep saying "don't be confused". Well... you did this in a confusing manner.

    • @cybern9ne
      @cybern9ne 7 месяцев назад

      Naming everything with the same name and leaving code not used made this very confusing.

  • @tanto_id
    @tanto_id 2 года назад

    Thank you, you explained very clearly

  • @nishantgarg3688
    @nishantgarg3688 2 года назад +2

    thanks bhai

  • @jake115577
    @jake115577 9 месяцев назад +2

    Using wsl is like a vegan making a vegetable taste like meat 😢

  • @joaocanteiro4655
    @joaocanteiro4655 2 месяца назад

    Good info the topic is not well explained in other places even the manual i found was confusing

  • @masterbobp
    @masterbobp 7 месяцев назад

    you are confusing module with packages

  • @lennuard_6998
    @lennuard_6998 2 года назад

    could you do a video about a tcp chat room in c? would love to see that
    love ur videos

  • @sweetshoez
    @sweetshoez 2 года назад

    To the point, thanks!

  • @RazanIbrahimOmerMohamed
    @RazanIbrahimOmerMohamed Год назад

    Thanks bro!

  • @tomatte99
    @tomatte99 6 месяцев назад

    you are just amazing

  • @leonidgrishenkov
    @leonidgrishenkov 2 года назад

    Thanks a lot!

  • @psi4588
    @psi4588 Год назад

    amazing!!