Fix Python Relative Imports and Auto-completion in VSCode

  Рет қаралды 60,185

k0nze

k0nze

Күн бұрын

In this video, you will learn how to properly handle Python relative imports without extending the sys.path. Additionally, you will learn how to enable auto-completion for Python packages in Visual Studio Code.
⏬ Get my FREE Python Cheat Sheets: k0nze.gumroad.com
👉 Website Article for this Video: k0nze.dev/posts/python-relati...
💌 Support me on Patreon: / k0nze
☕️ Buy me Coffee: www.buymeacoffee.com/k0nze
🌍 Check out my Website: k0nze.dev
💻 Join our Discord: discord.k0nze.dev
👔 Follow me on LinkedIn: linkedin.k0nze.dev
Recording Gear
Camera - Sony A6400: geni.us/zRw1
Lens - Viltrox AF 23mm f1.4: geni.us/RVrguGd
Microphone - Rode Wireless Go: geni.us/RBAOIx
Keyboard - Keychron K2 v2: geni.us/DPK87Xn
⏰ Timestamps:
0:00 Intro
0:21 Manually Extending the Python sys.path
2:07 Add relative Python imports in VSCode (extending the PYTHONPATH)
3:05 What is the PYTHONPATH?
4:04 Add own Python Packages to VSCode Auto-completion
5:57 Outro

Пікірлер: 76
@k0nze_builds
@k0nze_builds 2 жыл бұрын
Download FREE Python Cheat Sheets: k0nze.gumroad.com
@MultiMediaUploads
@MultiMediaUploads 2 жыл бұрын
This is a suboptimal solution that works locally, and forces anyone that works on this project to use vscode as their IDE. The best solution is to package the extern directory as its own package and have it part of the requirements for the project, and then you can import it from there. Unfortunately, you are trying to fix an anti-pattern by promoting another one.
@victorialopez471
@victorialopez471 Жыл бұрын
how would you do that? do you have a video or any reference link?
@ananthmahadevan2586
@ananthmahadevan2586 9 ай бұрын
I agree with this comment. Personally I am guilty of using the anti-pattern of extending the system.path. I remember having issues with relative imports if I wanted to start an interactive VSCode sessions a if I wanted to run the module via command line. I also agree that maybe the best way to separately package the external dependencies and then import. I would also be interested in how to do this efficiently and at what stage of development I should do it.
@rvoros
@rvoros Жыл бұрын
For the intellisense part I agree. But the basic problem is not solved with imports because the solution is restricted to VS Code. The script will still not work from command prompt so the antipattern issue is not resolved by this. Path has to be extended... Bad, I agree. If there's a PEP against it they shouldn't only ban it but offer an acceptable solution too.
2 жыл бұрын
Doing multiple settings just to help VS Code and nothing else? I think it is much better to turn extern into a proper package and install it using pip (could be an "editable" install for easier development) then it will work everywhere, not just in VS Code.
@lanceb247Upload
@lanceb247Upload Жыл бұрын
❤❤❤ You, sir, are awesome. I’ve been googling how to get my VScode debugger working properly with my project for hours now. Yours was the first to give me a pretty clear understanding of what was happening and how to resolve the issue. (I have my project’s main module 2 layers deep). Thank you!!!!
@JoseLuisNaranjoVillotaEc
@JoseLuisNaranjoVillotaEc 2 жыл бұрын
Hello, thanks for the explanations, one additional question: I have classes2.py in the extern directory, if my classses.py have a line: from Dexter import classes2 , then it's not found, the solution that you show us, adding the launch.json only recognizes the classes.py but not the other, how can i solve it?
@sokolovl
@sokolovl Жыл бұрын
This helped me a lot working with micropython too (as I need relative imports with sys.path.append on the controller, but want Intellisense when coding). Thanks!
@shivakrishna1743
@shivakrishna1743 Жыл бұрын
Do we need to do this everytime we create a project? Is there a way to make vscode automatically add project folder to pythonpath? Thanks!
@alirezamohammadi9691
@alirezamohammadi9691 Жыл бұрын
that was great!but I have a question...vs code not showing mysql.connector auto complete suggestions what should I do for that?
@Apo_0
@Apo_0 2 жыл бұрын
Many thanks man, for this amazing video, it took me hours to get rid of this!
@nex88
@nex88 2 жыл бұрын
I don't make comments often, but you have a very clear way of explaining things. Keep on creating videos!
@k0nze_builds
@k0nze_builds 2 жыл бұрын
Thank you Joachim! That means a lot to me!
@emrekara369
@emrekara369 10 ай бұрын
What if there is more than one main file that I need to debug in the same folder?
@tarakeshnc7417
@tarakeshnc7417 7 ай бұрын
Thanks.. So these files are used in VScode environment.. Is it correct ? If you are running the main.py over a shell, will it work ?
@timothedavid6477
@timothedavid6477 Жыл бұрын
thanks for all ! I make an anti pattern during several day and now I can work easly :) thanks for all
@uncoding_with_raj
@uncoding_with_raj Жыл бұрын
I have this same problem with odoo library import tried settings.json and PYTHONPATH settings but it didnot work for me i still get the same import error. can you please help
@noufbouf
@noufbouf 2 жыл бұрын
Would this solution work if someones else tries to use our project using another IDE, let's say Pycharm?
@carlosnacher3593
@carlosnacher3593 2 жыл бұрын
Thanks for the video! But I still have the problem. Imagine your main.py is a main.ipynb. How do we proceed???! Help please :(
@AzulonKaiba
@AzulonKaiba 2 жыл бұрын
Finallye, this helped with the subfolders an Python modules in VS Code
@k0nze_builds
@k0nze_builds 2 жыл бұрын
I'm happy the video was helpful for you!
@oskarhovmllerdinesen317
@oskarhovmllerdinesen317 Жыл бұрын
Great tutorial! Thanks
@dharamparkashgarg5358
@dharamparkashgarg5358 Жыл бұрын
Hi I am using Visual Studio 2022 and its very complicated and unable to find solution for same problem. Please help. Even I am unable to find Run & debug icon here.
@DavidPerez-ee6zl
@DavidPerez-ee6zl 10 ай бұрын
Thank you for sharing, Very good Job!
@alexeymatveev9031
@alexeymatveev9031 Жыл бұрын
Thanks for the topic, but didn't work for me....(( I'm trying to make import from a parallel directory and it doesn't work(((
@lDMCl_DANTE
@lDMCl_DANTE 29 күн бұрын
I ve´ the same problem... and i tried everithing... the only solution was,, add my folder into .venv/lib folder... but the bad was i cant push to github...
@ucanhlenguyen7720
@ucanhlenguyen7720 2 ай бұрын
Thanks for the solution, it's very helpful.
@idelfridesjorgewm1
@idelfridesjorgewm1 7 күн бұрын
Thank you very much, that video helped me a lot.
@kumarvis01
@kumarvis01 2 жыл бұрын
helpful , simple to the point ..
@tapgaze7016
@tapgaze7016 Жыл бұрын
how for multiple paths please?
@patite3103
@patite3103 Жыл бұрын
Great video. But where I know in my case where my_package is? I'm just unable to implement it!
@MrOiZo95
@MrOiZo95 Жыл бұрын
Thanks for this !
@hongdong9434
@hongdong9434 2 жыл бұрын
Thank you so much, this helped a lot. It was really hard to set it up following the VS Code documentation. You made it so much easier to follow. Thx man.
@k0nze_builds
@k0nze_builds 2 жыл бұрын
I'm glad I could help you out Hong!
@mookjais
@mookjais 2 жыл бұрын
Thanks! This worked for me
@k0nze_builds
@k0nze_builds 2 жыл бұрын
I'm glad this video was helpful for you and you Mookjais!
@lugaresyplantas7819
@lugaresyplantas7819 10 ай бұрын
Not working for me :/
@user-wi3ug7vi2u
@user-wi3ug7vi2u 8 ай бұрын
Awesome, thanks
@nsilver42
@nsilver42 2 жыл бұрын
Thank you for this. I was trying to solve this exact problem. My project is using a multi-root workspace, and the ${workspaceFolder} substitution was not working for me with the python.analysis.extraPaths setting. Not sure if it is just an issue with PyLance, or multi-root workspaces. But putting in the relative path "..//MyLib" did work, so that is good. The ${workspaceFolder} substitution does work with the PYTHONPATH launch config setting in a multi-root workspace, so that is good.
@k0nze_builds
@k0nze_builds 2 жыл бұрын
Thats great to hear Nathan! I'm happy this video was helpful for you.
@sanketwagh7768
@sanketwagh7768 Жыл бұрын
that was very helpfull thansks a ton
@JoseHenrique-tz6cr
@JoseHenrique-tz6cr 9 ай бұрын
Very very clear
@user-kr4jn4qf5q
@user-kr4jn4qf5q 2 жыл бұрын
Clean, simple, and beautiful. Really enjoy this tutorial. Thank you :)
@k0nze_builds
@k0nze_builds 2 жыл бұрын
Thank you! I'm happy you liked the video
@enrico200165
@enrico200165 Жыл бұрын
totally agree. subscribed immediately, perfect for me (and many others I think)
@miladiouss
@miladiouss 2 жыл бұрын
I've been putting off fixing VS Code auto-completion for years. Thank you my man!
@urbanwolfgaming4985
@urbanwolfgaming4985 2 жыл бұрын
Does this allow me to run my python script outside of vs code?
@k0nze_builds
@k0nze_builds 2 жыл бұрын
This video is not about running Python outside of VSCode. However, VSCode just calls Python on the command line itself and extends the PYTHONPATH in the background. To run a project with relative imports outside of VSCode you have to manually expand the PYTHONPATH and than call your script as a module.
@sudarshankoirala2072
@sudarshankoirala2072 2 жыл бұрын
Short and clear explanation .. keep making these sort of videos .. thank you !
@user-vb5nl4gc4m
@user-vb5nl4gc4m 2 жыл бұрын
Awesome!!
@prasenjitgiri919
@prasenjitgiri919 2 жыл бұрын
Hi Konze, I've been trying to find a solution to no module found and chanced upon your video. First of all thank you for the video, but unfortunately I'm unable to resolve the problem. 1. I have created the similar folder structure and yet the debug/run wont work 2. Will you please confirm if these settings are only applicable for debug? As the settings create a debug profile? How to make something for run? 3. I've also noticed while using pycharm, one can simply select folder as "Source" and it wokrs for both debug as well as run. Will appreciate if you can spend time to help a fellow out. Thank you in advance!
@k0nze_builds
@k0nze_builds 2 жыл бұрын
The settings are also applicable for "run". The "Source" feature of PyCharm does the same thing, it adds the path to the folder to the PYTHONPATH as shown in the video.
@lch80123
@lch80123 2 жыл бұрын
Subscribed! Really great video that saves my life!! I have two questions. 1)What if I have multiple modules in the same folders that need the extended python path (or multiple extended python paths), can I just put the name of the second module after the first one? 2) It was fine for me to run the file under VS code and import the module from another directory. However, why did it still complains ModuleNotFoundError when I executed the python file via Terminal?
@Ales-pz6jq
@Ales-pz6jq Жыл бұрын
It gives you that error because terminal is not vscode, and this video is about fixing vscode. It's nice but i still need to find another and generic solution
@neilianedles9158
@neilianedles9158 10 ай бұрын
Thank you!
@mohamadfawaz4146
@mohamadfawaz4146 Жыл бұрын
You are great!
@raghuZanne
@raghuZanne 2 жыл бұрын
Awesome video. Thank you for calm and great explanation!
@k0nze_builds
@k0nze_builds 2 жыл бұрын
I'm happy you could help you out and you enjoyed the video!
@raviprasadmr
@raviprasadmr Жыл бұрын
I am new to vscode, and I searched stackoverflow and several other pages to end up getting all the confusing information. I am glad that I found out this channel finally to solve my problems. Thanks much!
@dharamparkashgarg5358
@dharamparkashgarg5358 Жыл бұрын
Fix Python Absolute Imports and Auto-completion in VSCode, could you please fix this issue as well
@salarghaffarian4038
@salarghaffarian4038 Жыл бұрын
Much appreciated! That was very helpful! Thanks!
@tayloroc1
@tayloroc1 2 ай бұрын
This should just work. I am importing a file from the same folder and VS Code can't figure it out. I can't either, that's why I'm complaining.
@victorhugocubasbalbuena5274
@victorhugocubasbalbuena5274 Жыл бұрын
unfortunately it didn't work for me :(
@mikhailkhlyzov6205
@mikhailkhlyzov6205 Жыл бұрын
As much as I appreciate the explanation and showcase of doing it inside VSCode, the solution feels a bit off imho. I'd rather use editable install of my sub module inside virtual environment (that I use anyway). Would be IDE agnostic.
@MenAmongTheRuins77
@MenAmongTheRuins77 2 жыл бұрын
fixed it sub
@ohhluka
@ohhluka Жыл бұрын
it is not working
@bar-ni1ux
@bar-ni1ux Жыл бұрын
Good explaination!
@user-yp3ir6cy4l
@user-yp3ir6cy4l 7 ай бұрын
muy bueno
@user-fj4ts9jt1p
@user-fj4ts9jt1p Ай бұрын
Honestly, this issue is very annoying, still present on 2024
@dooley-ch
@dooley-ch 2 жыл бұрын
Sorry but this is nonsense! Your entire project is now dependent on the IDE you use and if you were actually working with colleagues they would not be able to work with your project without also using the same editor. If you are using package and the loading mechanism correctly there should be no need to resort to this kind of nonsense. You do people no favors by promoting this kind of stuff.
@maelstrom254
@maelstrom254 Жыл бұрын
This is very bad advice, as it forces you to run scripts in vscode only.
@hashiruww
@hashiruww Жыл бұрын
Can't understand why microsoft leave this shit
@fapdayz
@fapdayz Жыл бұрын
absolutely thrash solution
@marquinho1p
@marquinho1p Жыл бұрын
if you are using a virual env and you've installed the package locally - its best to put below in either your workspace settings file - or maybe even your settings.json file "python.analysis.extraPaths": [ "${execPath}" ],
Powerful VSCode Tips And Tricks For Python Development And Design
15:50
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 157 МЛН
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 122 МЛН
Best father #shorts by Secret Vlog
00:18
Secret Vlog
Рет қаралды 22 МЛН
python: explicit relative imports (intermediate) anthony explains #298
8:19
How to Set up VS Code for Data Science & AI
22:53
Dave Ebbelaar
Рет қаралды 286 М.
Python Programming 53 - Sys.path and Changing Module Paths
5:21
Caleb Curry
Рет қаралды 31 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 388 М.
5 Tips To Organize Python Code
12:16
Tech With Tim
Рет қаралды 223 М.
The Python Walrus Operator is Awesome!
13:41
k0nze
Рет қаралды 670
Absolute and Relative imports - Python Tutorial 28
14:02
Automation Zone
Рет қаралды 34 М.
You Can Do Really Cool Things With Functions In Python
19:47
ArjanCodes
Рет қаралды 218 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН