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/ Наука
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
It's actually python which made this confusing. JS ESM is so simple
@@PavitraGolchha JS imports may not be the best example of simplicity, due to the competing standards, but I get what you mean
It is kind of confusing. Also a package can be comprised of packages.
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.
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!
Nice explanation, best one yet I've seen on modules!
Thank you! :) I always learn something new with your videos :D Please keep it up!! Very well done :)
Thank you SO much. This was a difficult concept for me to grasp but so helpful. Your awesome
Thank You Sir. You went into great length to explain this concept.
Quick and simple explanation thank you !
THIS!! This is exactly what i have been looking for! Thank you so much
Excellent video! I’m new to python and this is a big help
you saved me a lot of time with this video. thank you. earned my sub
Thank you so much for this, very clear explanation.
BROTHER, YOU ARE THE BEST!!! You oooh really helped me!! THANK YOU VERY MUCH!
Really informative video. Thanks for recording and sharing it.
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.
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.
I had the same. The file he is editing is named below (bottom)
very nice explanation. understood everything very clearly
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 😆
yeah if you want to do "real work," chatGPT just crashes.
Wow, that was actually pretty easy
Thanks you sir
Thank you very much sir .... This was badly needed...
It works.
Thank you, really help my project :)
this combined with if __name__ == "__main__": would have been the best youtube in history.
Thank you, you explained very clearly
Thanks for the helpful video!
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.
Very informative. Thank you very much.
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"
But this hasn't worked for me so far. Getting an error citing something about "unspecified parent package"
@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.
@@aceldama this I tried actually
Very clear explanation Thanks 🎉
Thank you this helped me a lot
Excelent job!
Congrats
Impressive. To the point content
thx for this, really helpful
Thanks bro, much love
thank man u saved my so much time
Fantastic explanation.
Learned a new thing. "__all__". Thanks a lot.
Really cool video and explained very clearly. What about making a video on packaging modules for sharing?
thanks a lot, solved many problems.
To the point, thanks!
what if our module is in another directory? should we amend the system path?
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.
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?.
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.... 👍👍
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?
Nice video, I have to learn to use vim like you
Nice tutor!
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.
Thanks for the video. I recommend some daily careful typing practice, btw - so you don't need to constantly correct your typing
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.
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
Tutorial on how to setup the environment like yours please
Thanks a lot!
any idea why my vs code isnt importing files from the same directory?
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.
why we dont have to declare submodule in the init file?
Thank you for lesson. But how import from upper module to lower?
Thanks bro!
you are just amazing
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.
What if I want to import fourth.py inside second.py and then call second.py from main.py?
In second.py Import Fourth, in main Import Second
maybe 😊
What if the module file is not under the project folder. How do you define a totally different path?
how use linting in neovim using pylint and how to avoid import error
Please do a video on different IDE's, pros, cons etc.
Thanks for your great comment.
I can recommend pycharm for python and blueJay for Java
@@Lefthandup but more people especially on YT are using VS Code.
@@404errorpagenotfound.6 Vscode isn't really an IDE it's a text editor
@@stwni what does an IDE do that VS doesn't? It's incredibly popular with YT programmers teaching coding.
@@404errorpagenotfound.6 I’m pretty sure an ide has the advantage of using other peoples libraries etc.
GOING Awesome!! Still great!!
amazing!!
Thx so much
can you please put module and code(main) at different folder and access module from code
very useful
Does this work if the folders are in git repo?
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?
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
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.
@@mables8698 wow this is exactly what I was trying to do thx
As a noob, I've watched this and still have no idea how to add modules, thanks.
Love you bro
I tried to import my own module that uses pandas or yahoo finance. but it did not work. How can i solve it
what does . mean at 8:31?
thanks bhai
what is this test editor name?
what's your vim setup 👉👈
I love your video
What is this ide name ? Is it vim ? May you send config ?
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
Thanks
Yooo, what is this terminal program and setup, it looks sexy. Thanks for the video!
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
all good. but it dont see import pandas as pd in function :(
what ui is this??? sorry, new to this
I was import error like they can't be imported from my module
Please make a tutorial on how you set up your neovim environment
I have two already
You didn't talk about having the module not in the same directory and using it.
I LOVE YOU
I use neovim on KDE Plasma!
Well explained!
But why use * in the first place? It's supposed to be an antipattern because of namespacing issues.
Absolutely, yes. Avoid wildcard imports.
could you do a video about a tcp chat room in c? would love to see that
love ur videos
The terms module and package are mixed up! A module is a python file. A package is a collection of python modules!
Not clear on package/module/sub-module terminology. A bit confusing.
TOP
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
ah.. why not cover sibling module..
Thx_.