Importing Your Own Python Modules Properly

Поделиться
HTML-код
  • Опубликовано: 5 июл 2022
  • Today we learn how to properly import your own Python modules and also what the __init__.py file is about.
    ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
    📚 Programming Books & Merch 📚
    🐍 The Python Bible Book: www.neuralnine.com/books/
    💻 The Algorithm Bible Book: www.neuralnine.com/books/
    👕 Programming Merch: www.neuralnine.com/shop
    🌐 Social Media & Contact 🌐
    📱 Website: www.neuralnine.com/
    📷 Instagram: / neuralnine
    🐦 Twitter: / neuralnine
    🤵 LinkedIn: / neuralnine
    📁 GitHub: github.com/NeuralNine
    🎙 Discord: / discord
    🎵 Outro Music From: www.bensound.com/
  • НаукаНаука

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

  • @robbiefryers13
    @robbiefryers13 Год назад +163

    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 месяцев назад +9

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

    • @raenastra
      @raenastra 9 месяцев назад +4

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

    • @BooklyCrashCourse
      @BooklyCrashCourse 3 месяца назад

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

  • @bloodywolftr
    @bloodywolftr Год назад +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.

  • @siddhubhai2508
    @siddhubhai2508 8 дней назад +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!

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

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

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

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

  • @jameswilliams-nw3lq
    @jameswilliams-nw3lq 9 месяцев назад +1

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

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

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

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

    Quick and simple explanation thank you !

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

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

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

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

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

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

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

    Thank you so much for this, very clear explanation.

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

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

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

    Really informative video. Thanks for recording and sharing it.

  • @bla7091
    @bla7091 11 месяцев назад +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.

  • @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)

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

    very nice explanation. understood everything very clearly

  • @kaushiksinghrawat
    @kaushiksinghrawat 10 месяцев назад +3

    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 18 дней назад

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

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

    Wow, that was actually pretty easy
    Thanks you sir

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

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

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

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

  • @user-td4pf6rr2t
    @user-td4pf6rr2t Месяц назад

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

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

    Thank you, you explained very clearly

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

    Thanks for the helpful video!

  • @philipp4980
    @philipp4980 Год назад +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.

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

    Very informative. Thank you very much.

  • @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 3 месяца назад

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

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

      @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 3 месяца назад

      @@aceldama this I tried actually

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

    Very clear explanation Thanks 🎉

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

    Thank you this helped me a lot

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

    Excelent job!
    Congrats

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

    Impressive. To the point content

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

    thx for this, really helpful

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

    Thanks bro, much love

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

    thank man u saved my so much time

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

    Fantastic explanation.

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

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

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

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

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

    thanks a lot, solved many problems.

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

    To the point, thanks!

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

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

  • @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.

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

    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?.

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

    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.... 👍👍

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

    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?

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

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

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

    Nice tutor!

  • @tsvasekar
    @tsvasekar Год назад +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.

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

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

  • @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.

  • @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

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

    Tutorial on how to setup the environment like yours please

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

    Thanks a lot!

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

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

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

    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.

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

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

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

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

  • @user-xc7ow8rf9w
    @user-xc7ow8rf9w Год назад

    Thanks bro!

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

    you are just amazing

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

    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.

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

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

    • @CattleRustlerOCN
      @CattleRustlerOCN 5 месяцев назад

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

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

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

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

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

  • @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 года назад

      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 года назад

      @@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.

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

    GOING Awesome!! Still great!!

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

    amazing!!

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

    Thx so much

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

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

  • @user-cn5uh9he4f
    @user-cn5uh9he4f Год назад

    very useful

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

    Does this work if the folders are in git repo?

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

    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?

  • @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

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

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

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

    Love you bro

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

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

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

    what does . mean at 8:31?

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

    thanks bhai

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

    what is this test editor name?

  • @stevenhoang7297
    @stevenhoang7297 7 месяцев назад +1

    what's your vim setup 👉👈

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

    I love your video

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

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

  • @soklagenhet
    @soklagenhet 10 месяцев назад +2

    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

  • @user-pi2rw9bq6g
    @user-pi2rw9bq6g 11 месяцев назад

    Thanks

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

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

  • @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

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

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

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

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

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

    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

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

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

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

    I LOVE YOU

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

    I use neovim on KDE Plasma!

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

    Well explained!

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

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

  • @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

  • @matteodelgrosso4364
    @matteodelgrosso4364 8 месяцев назад +2

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

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

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

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

    TOP

  • @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

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

    ah.. why not cover sibling module..

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

    Thx_.