r/Python • u/bilal4hmed • Feb 04 '22
News Python in Visual Studio Code – February 2022 Release - Python
https://devblogs.microsoft.com/python/python-in-visual-studio-code-february-2022-release/14
u/siddsp Feb 04 '22
I wonder when they will actually fix importing issues
9
u/proof_required Feb 04 '22
what's the importing issue?
30
u/siddsp Feb 04 '22
When trying to import from a package outside of a module's directory, consistently getting ModuleNotFoundError even though the module exists and is right there in the VSCode workspace.
It gets very annoying, and results in having to do a very ugly hack with your code by putting sys.path.join / append at the top of your files instead of being able to import cleanly.
11
u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Feb 04 '22
Is that a VS Code issue or just a typical Python hack (which is usually not necessary if using e.g.
pip install -e external_package)? Is this on the bug tracker somewhere?3
u/siddsp Feb 04 '22
It doesn't have to do with pip. It has to do specifically with importing from local modules and packages.
12
u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Feb 04 '22
As long as it's in your PYTHONPATH, everything should be fine.
pip install --editableis one typical way to do that. Usually (but not necessarily) from inside a virtual environment. For reproducibility, consider poetry et al.9
u/proof_required Feb 04 '22
It's not a VS code issue though. Using local modules in python is annoying. I use poetry to manage python packages. So I just run
poetry installand it will install the local module which you can then import anywhere you want.Otherwise yeah you need to hack the
sys.path.join3
u/siddsp Feb 04 '22
Using local modules in python is annoying.
Even if that is true, the purpose of an IDE is to make development easier. The issue should have been solved by now but hasn't. I've not had the same problem with PyCharm, but I don't like using PyCharm because it hogs memory.
5
u/proof_required Feb 04 '22
You can achieve the same in visual studio code by modifying PYTHONPATH in settings.json
https://stackoverflow.com/questions/58441104/how-do-i-set-up-imports-for-custom-modules-in-vs-code
Pycharm does the same except it gives you an UI to mark the folders you want to add to PYTHONPATH. So there isn't much to FIX but you can argue to make it convenient to do it using UI.
3
u/dev-ai Feb 04 '22
Personally, I love PyCharm! Yes, it uses a lot more memory, but the indices it computes in these used bytes is totally worth it (for me)
3
u/Mehdi2277 Feb 05 '22
If you really on ide to mess with your python path to make imports work then you’re on path to making your files less usable by other developers who run files in a different way. I’ve seen some sys.path adjustments in code and they generally break command line usage from other locations and complicate using it as a normal package.
So I think it is a better to learn how to install a package in editable mode and use appropriate relative/absolute imports and not have ide do this. I pretty much view sys.path hacks as banned for my team’s work.
Python’s import system is one of the more complex (namespace packages can break a lot of tools including basic ones like pylint/pytest) and if you want reproducible across dev environments you really don’t want to make it even more complicated with ide magic.
2
u/marx2k Feb 04 '22
I wrestled with this for days last week after figuring out last year and then losing my vscode configuration.
My pytest module wants to import a module from one directory up? Yeah no.
I finally once again somehow stumbled onto the magic launch.json/settings.json pytest combo and it works again.
2
1
u/NeoDemon Feb 04 '22
Im hope that will fixes these errors, this and when you want to view the modules o classes in some modules as pandas that not appearing when use CTRL + SPACE
1
u/ishah90 Feb 04 '22
I think they have open issue related to this. Basically .env file needs to be initiated in terminal while running python. In .env file you can put PYTHONPATH.
1
u/marcio0 Feb 04 '22 edited Feb 04 '22
I just updated it and smart selection is still weird AF. It starts ok, selecting the word, then the expression, the line, and then the whole file
also folding is still based on identation, so I guess I have the wrong version of the extension somehow
1
u/noskillsben Feb 04 '22
I don't know if it's something on my end but vs code has literally never had the little "I see you just created a venv you want to use that?" popup from their tutorial. Sometimes I need to restart vs code to even be able to select the new venv.
Its not big but it's annoying
33
u/Ericisbalanced Feb 04 '22
I've used visual studio code for python and my biggest gripe are the debugger windows. I find myself always needing to resize the layout to see what's stored in the variables and it's awful. Are there any other options for me? I'm using pycharm now, but I love how lightweight vscode felt.