Fix Python Relative Imports and Auto-completion in VSCode

preview_player
Показать описание


Recording Gear

⏰ Timestamps:
0:00 Intro
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
Рекомендации по теме
Комментарии
Автор

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.

MultiMediaUploads
Автор

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.

rvoros
Автор

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.

Автор

❤❤❤

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!!!!

lancebUpload
Автор

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?

JoseLuisNaranjoVillotaEc
Автор

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!

shivakrishna
Автор

Many thanks man, for this amazing video, it took me hours to get rid of this!

Apo_
Автор

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!

sokolovl
Автор

that was great!but I have a question...vs code not showing mysql.connector auto complete suggestions what should I do for that?

alirezamohammadi
Автор

thanks for all ! I make an anti pattern during several day and now I can work easly :) thanks for all

timothedavid
Автор

I don't make comments often, but you have a very clear way of explaining things. Keep on creating videos!

nex
Автор

Thank you very much, that video helped me a lot.

idelfridesjorgewm
Автор

What if there is more than one main file that I need to debug in the same folder?

emrekara
Автор

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 ?

tarakeshnc
Автор

Thank you for sharing, Very good Job!

DavidPerez-eezl
Автор

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

uncoding_with_raj
Автор

Finallye, this helped with the subfolders an Python modules in VS Code

AzulonKaiba
Автор

Thanks for the solution, it's very helpful.

ucanhlenguyen
Автор

Thanks for the video! But I still have the problem. Imagine your main.py is a main.ipynb. How do we proceed???! Help please :(

carlosnacher
Автор

Would this solution work if someones else tries to use our project using another IDE, let's say Pycharm?

noufbouf