Adding __init__.py not working - Module Not Found: Fixing Python Sibling Directory Import Error

  Рет қаралды 26,003

Gus Cavanaugh

Gus Cavanaugh

Күн бұрын

I've been trying to use better project structure for my ML demos, but I keep getting punched in the face by Python when importing sibling packages.
my_ml_project/
run_project.py
sweet_src_code/
models_dir/
amazing_model.py
tests_dir/
beautiful_test.py
I add __init__.py files everywhere, re-read the python docs (lnkd.in/gcgeb6qA), and remain utterly confused.
I then usually give up and put everything in the top level dir:
my_ml_project/
run_project.py
amazing_model.py
beautiful_test.py
I finally came across a stack overflow answer that I had previously missed: use a setup.py file.
This video is a quick demonstration of what doesn't work (failing to import my model_dir from a sibling test_dir) and how adding a short setup.py file to the project root fixes the problem.
This is basic stuff but I now feel such relief. No more pentagrams of salt, ritualistic animal sacrifices, and __init__.py files everywhere trying to get imports to work.
If you want to connect with me, I'm most active on LI. Feel free to connect with me: / gustafrcavanaugh

Пікірлер: 105
@JWJD99
@JWJD99 Жыл бұрын
Gus, I've banging my head against this error code all day. You're a lifesaver! Thank you!
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
Thanks Jacob!
@rootifera
@rootifera 8 ай бұрын
That's great but do we know why the __init__.py doesn't work? According to the documentation it should, but it doesn't (this is why I'm here, just like many of you). Unless I'm reading the documentation wrong over and over again, it says "add __init__.py everywhere and Bob's your uncle". But no... There are many things I like about python, package management isn't one of them. Anyway, got my code working, thanks for the video!
@guscavanaugh2427
@guscavanaugh2427 8 ай бұрын
same!
@tuanphammanh7122
@tuanphammanh7122 Жыл бұрын
This video is life saver, I have struggle for almost 2 days, because I have to build the test also the main file in different folder from the module file. Thanks man!
@gheeno.
@gheeno. 9 ай бұрын
CLEAN, CONCISE! You are a life saver! No Hardcoded path, no implicit imporation of files. I wish __init__.py works like this, where it binds all the modules and submodiles where __init__.py is present, bit it doesnt. Thanks for this! I'll try it on my next automation project
@guscavanaugh2427
@guscavanaugh2427 9 ай бұрын
Thank you! And same here. It's just frustrating for mere mortals. Glad im not alone
@quophiebarnabaue5338
@quophiebarnabaue5338 3 ай бұрын
It can get a bit frustrating when you read and interpret the docs carefully and things still don't work. You saved me some time. Thanks.
@ravi_jain204
@ravi_jain204 11 ай бұрын
Thank you soo much buddy, I was stuck with this error from 6 hours. You saved my day!!
@guscavanaugh2427
@guscavanaugh2427 11 ай бұрын
YW!
@steveando-w9w
@steveando-w9w 8 ай бұрын
So many hours on this exact thing. You are amazing.
@guscavanaugh2427
@guscavanaugh2427 8 ай бұрын
Glad it helped!
@KirubelTamene
@KirubelTamene Жыл бұрын
God bless you. period! edit; if u can please find the reason and reply to this comment
@moe8338
@moe8338 2 ай бұрын
Holy, you are a lifesaver! For some reason the absolute path of some modules didn't work, but now it does.🎉
@karthikash
@karthikash Жыл бұрын
Hey Gus, thanks for this informative video. This is what exactly I was looking for.. I was scratching my head to figure out how to setup a base with python and you really made my day. Thanks again!!
@EpicExcelsior
@EpicExcelsior Ай бұрын
Days of searching and finally found a solution in this. Thank you, Gus! 🙏
@andrewyuan6809
@andrewyuan6809 11 ай бұрын
Great video! I still can't figure out why init doesn't work like it supposed to. Thanks for the solution
8 ай бұрын
IT. WORKED. THANK. YOU. SO. F-ING. MUCH!!!
@guscavanaugh2427
@guscavanaugh2427 8 ай бұрын
YW!
@BarbiesBoyfriend93
@BarbiesBoyfriend93 Жыл бұрын
dude, you safed my life! Thank you so much.
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
Thanks so much for sharing!
@snakemonkey784
@snakemonkey784 3 ай бұрын
I just started coding and this got me crying for a whole day😅
@okumujustine
@okumujustine Жыл бұрын
Exactly what I needed, you just earned yourself a subscription, thank you for this.
@Sibixpur
@Sibixpur 2 ай бұрын
I HAVE BEEN SEARCHING FOR SOLUTION FOR TWO DAYS AND FINALLY I GOT IT TO WORK!!! THANK YOU SO MUCHHHH!!!!!!!
@CringeCrafter-uv1lt
@CringeCrafter-uv1lt 6 ай бұрын
Amazing! Thanks! I knew it had to do with my setup.py file, but I couldn't find any good resources to figure out what the problem was for hours.
@llrajsll
@llrajsll Жыл бұрын
Thank you brother for saving us the frustration. I'll also make one such video the next time I figure out such an error.
@guscavanaugh2427
@guscavanaugh2427 11 ай бұрын
Awesome!
@AdvaTced
@AdvaTced Жыл бұрын
thats exactly what i'm doing, the sibling problem, there is barely information about this problem around the net, it's super basic design problem... i would like to ask you if you found why it is necessary to use setup tools to overpass this issue? seems like PYCHARM or any advanced IDE recognize those modules, but in runtime it will fail and will not recognize the modules because it's not recognized the PYTHONPATH. another way to overcome this issue is to use for example: ``` parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) sys.path.append(parent_dir) ``` and only than make the desired import of the desired module for example. so by the docs of python all seems legit, but when you do it in runtime seems like things breaking (just like you wondered in the video). i would like to know if you have any explanation about that issue, thanks :)
@martinsvach4142
@martinsvach4142 4 ай бұрын
You are a STAR!!! I was gonna give up. Thanks heaps! Best tutorial ever!!!
@selim2590
@selim2590 Жыл бұрын
Hey man, there is a saying in Turkish, nothing to shame on not knowing but not learning. Its been a long time i was searching for this type of solution to python and you saved the day. Thanks a lot!
@guscavanaugh2427
@guscavanaugh2427 11 ай бұрын
I love that -- great saying!
@tdonov
@tdonov 4 ай бұрын
up up up - big up. Have been looking into solutions for a few days now, even created my own pypy package
@parthpatel8726
@parthpatel8726 9 ай бұрын
Gus brother You are amazing. Lots of thanks for this solution🤩
@guscavanaugh2427
@guscavanaugh2427 9 ай бұрын
your welcome!
@pixar3653
@pixar3653 11 ай бұрын
This happens when we build project from scratch in VSCode or some other Text Editor. When we use IDE like PyCharm, it still supports us to do that even __init_.py is not there. That's so weird. And this also makes .egg-info folders in the project.
@iCerCortes
@iCerCortes 8 ай бұрын
Thank you! Was struggling with this error.
@guscavanaugh2427
@guscavanaugh2427 8 ай бұрын
Your welcome!
@acp9569
@acp9569 Жыл бұрын
Short and Sweet ! Great video, worked for me. Thanks Gus
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
thank you!
@НиколайМакаров-ж1щ
@НиколайМакаров-ж1щ Жыл бұрын
dude, thank you so much for this video! it's really helpful
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
You got it, bro! The struggle is real with imports and python path
@TautologyTechSystems
@TautologyTechSystems 5 ай бұрын
I was so hopeful that this was the fix for me, but unfortunately I'm still getting the import not found error. Incredibly frustrating as I've spent hours now running through all the solutions on stack overflow. I have literally tried everything at this point and python still fails to identify my modules.
@mohammedsahil520
@mohammedsahil520 3 ай бұрын
same here bro
@useless_intern
@useless_intern 7 ай бұрын
2.48 minutes video saved my hours . thanks man it resovled my issue
@larsseverson5725
@larsseverson5725 Жыл бұрын
i was getting so frustrated with this issue. Thank you so much man
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
Thank you
@ns_the_one
@ns_the_one 5 ай бұрын
from setuptools import setup, find_packages setup(name="my", version="0.1", packages=find_packages(), package_dir={'': 'src'}, )
@SneezeAlpha
@SneezeAlpha 5 ай бұрын
Amazing! You solve the problem trouble me for weeks!
@tangellagopal7299
@tangellagopal7299 3 ай бұрын
Awesome, Finally I resolved this error!
@gveenhof4657
@gveenhof4657 Жыл бұрын
the relatively and absolutely boring joke was pretty funny ngl
@dtsleite
@dtsleite Жыл бұрын
Good tips for Python! Thanks!
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
Thanks Douglas!
@Kyle-zu6rl
@Kyle-zu6rl 2 ай бұрын
I was encounter the same error. For me, the real cause of the issue is that python did not recognize my project directory as a project directory with corresponding dependencies build, so I had to run 'pip install -e .' at the project directory (test_structure folder directory in the case of the video), assuming the setup.py exists and contain basic stuff (easy to find). Then the 'from...import' with absolute path would work.
@ryankirkland7706
@ryankirkland7706 Ай бұрын
I tried AI. I tried Google. All I needed was Gus. Thank you - I was gonna put my head through my monitor.
@ИванСлепов-д9щ
@ИванСлепов-д9щ 5 ай бұрын
Hey Gus, just wanted to thank you for the helping hand!!!)))
@AmritpalSingh-sy6dl
@AmritpalSingh-sy6dl Жыл бұрын
Earned one subscriber. Keep up good work mate
@luffy7564
@luffy7564 Жыл бұрын
3 days to fix this finally find your video thanks a lot
@abdallahelra3y118
@abdallahelra3y118 6 ай бұрын
Appreciate the help, That was quite frustrating.
@progressivemusicnepal
@progressivemusicnepal 11 ай бұрын
Thank you, you saved me a lot of my time.
@guscavanaugh2427
@guscavanaugh2427 11 ай бұрын
YW!
@ongayijohnian8787
@ongayijohnian8787 2 ай бұрын
Tremendous help Gus 💯
@sivanagarajuboyina4617
@sivanagarajuboyina4617 Жыл бұрын
saved my day bro.... literally after 5 hours of my search......
@gaddammanoj6852
@gaddammanoj6852 Жыл бұрын
Thanks a lot dude you are a massive help
@solitudechronikles3521
@solitudechronikles3521 Жыл бұрын
Been sh*tting myself with that module error for days man thanks a lot. Although when trying to run pip install -e . I get a virus warning
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
Appreciate the comment. Not sure about what your anti-virus (AV) is doing. Curious which one you are running?
@mohammadmiri6474
@mohammadmiri6474 Жыл бұрын
Saved the day! Awesome!
@fazankabir2029
@fazankabir2029 6 ай бұрын
BRO. YOU SAVED MY LIFE!!!🎉🎉
@matheusgoes640
@matheusgoes640 Жыл бұрын
Man, you've saved me! Thanks a lot!!
@yanzhimin1093
@yanzhimin1093 11 ай бұрын
Incredible. Thank you so much!!!
@guscavanaugh2427
@guscavanaugh2427 9 ай бұрын
You're very welcome!
@vasilisg.2637
@vasilisg.2637 5 ай бұрын
LIFE SAVER! THANK YOU! GOD BLESS YOU!
@afolabisamguy3762
@afolabisamguy3762 4 ай бұрын
This is a fucking lifesaver been stuck on this for almost a day.. even AI doesnt know the solution lol Thanks bro
@danijelmacakanja2671
@danijelmacakanja2671 3 ай бұрын
You just saved me a ton of nerves!
@xushuwang6527
@xushuwang6527 Жыл бұрын
Thank you so much!!!! Can't thank you more
@konstantinmorkovkin6334
@konstantinmorkovkin6334 7 ай бұрын
That's a cool solution! Very helpful for me.
@hamza-kacem
@hamza-kacem 11 ай бұрын
You're a lifesaver !
@guscavanaugh2427
@guscavanaugh2427 9 ай бұрын
YW!
@srhreza
@srhreza Жыл бұрын
WoW, where did you find it from? Python is crazy.
@MatthieuDRIDI
@MatthieuDRIDI 5 ай бұрын
You made my day 🙏
@iFunktion
@iFunktion Жыл бұрын
Was really hoping this would work, but alas no, it doesn't appear to work with my set up
@seangadson3085
@seangadson3085 7 ай бұрын
THANK YOU SO MUCH FOR THIS I THOUGHT I WAS DUMB!
@mirzabaig4480
@mirzabaig4480 3 ай бұрын
Thanks a lot for the video, helped a lot.
@wahi80
@wahi80 6 ай бұрын
Someone buy this man a beer !
@muffinmuncher
@muffinmuncher Жыл бұрын
very useful, thanks 🎉
@KundanYadav-je4xf
@KundanYadav-je4xf Жыл бұрын
thank u so much brother you are gem 💎
@ugurturkarslan3837
@ugurturkarslan3837 3 ай бұрын
Hey folks! Funnily, running "pytest" causes this problem but running "python -m pytest" does not. Don't know why exactly. Just FYI :)
@martinndegwa7155
@martinndegwa7155 Жыл бұрын
Amazing Thank you.
@eliabekun
@eliabekun 7 ай бұрын
Why that is not in documentation...? Thanks man!
@janfabik7801
@janfabik7801 Жыл бұрын
Hi Gus, thank you for the video. I found out that I can import the module as it is described in the Python document without creating the setup file. However, I have to run the module file first and then run the main file. If I just run the main file first, I receive the error AttributeError: module 'ecommerce.shipping' has no attribute 'calc_shipping'. Any idea what might help here please?
@victoriwuoha3081
@victoriwuoha3081 Жыл бұрын
Your shipping class does not have a function named calc_shipping... perharps you should check the name.
@manzihabamungublaise1146
@manzihabamungublaise1146 5 ай бұрын
Thank you for this👍👍👍👍
@JorgeEscobarMX
@JorgeEscobarMX Жыл бұрын
This seems more like a hack. Does this works if I try to run this on a different computer as is or do I need to install this on every computer?
@liabasqulizad7962
@liabasqulizad7962 4 ай бұрын
Thank you sir.
@lsnery
@lsnery Жыл бұрын
Thanks a lot
@guscavanaugh2427
@guscavanaugh2427 Жыл бұрын
your welcome!
@raulsofia6169
@raulsofia6169 7 ай бұрын
Thanks, it saved me! Is there any way to add this to a requirements.txt file?
@munnkiboy8014
@munnkiboy8014 4 ай бұрын
When I try to install setuptools like you have, I get the error "import cannot be resolved from source". I'm banging my head on a wall, someone please help 🙃
@munnkiboy8014
@munnkiboy8014 4 ай бұрын
Update: I've now fixed this problem, although the original issue of 'ModuleNotFoundError: No module named ' is still there for me.
@peterwoolley4893
@peterwoolley4893 5 ай бұрын
Why not just add .. to PYTHONPATH ?
@sunilkumar-ob7yz
@sunilkumar-ob7yz 2 ай бұрын
Can not python -m helps here?
@RiritoNinigaya
@RiritoNinigaya 6 ай бұрын
not working!!! This is clickbait, don't fall for it!!!
@කැලණිකුප්පි
@කැලණිකුප්පි 4 ай бұрын
What do meam this works prefectly
@RiritoNinigaya
@RiritoNinigaya 4 ай бұрын
@@කැලණිකුප්පි no, this is really clickbait lmao
@RiritoNinigaya
@RiritoNinigaya 4 ай бұрын
i'm founded way to fix module not found, it is because this __init__.py is not founded any python scripts or just incorrect initializated python... to fix this, you need write this in __init__.py: from . import youre_python_script_name and it will be worked!!!
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 16 МЛН
The selfish The Joker was taught a lesson by Officer Rabbit. #funny #supersiblings
00:12
Importing Your Own Python Modules Properly
9:56
NeuralNine
Рет қаралды 230 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 90 М.
python absolute import not working
3:27
CodeLearn
Рет қаралды 25
Never install locally
5:45
Coderized
Рет қаралды 1,8 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Can I Run Youtube Entirely From My Terminal? (No Browser)
15:31
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 392 М.
Makefiles in Python For Professional Automation
13:43
NeuralNine
Рет қаралды 43 М.
Intermediate Python Tutorial: How to Use the __init__.py File
6:22
Eric O Meehan
Рет қаралды 138 М.
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 16 МЛН