Programming newbie here.
I have used Spyder before, but recently wanted to try my hands with VS Code, but when setting up I run into issues.
I have installed the numpy-module without issue, but when I run my code I get the error message:
"Traceback (most recent call last):
File "c:\Users(user)\Desktop\test.py", line 1, in import numpy as np
ModuleNotFoundError: No module named 'numpy'
but when I then run pip install numpy
, I get the message
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in c:\users(user)\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (1.26.4)
I can't figure out why it won't work, and other solutions on StackOverflow haven't worked as of yet.
I have tried looking at different solutions on StackOverflow which seemingly similar issues, but none have worked so far.
I hope it's just my newbie brain that can't set it up right and that's it's an easy fix.
This problem usually happens due to not setting the right Python version and/or environment in the VSCode config.
From the path you provided you have Python 3.12 by default when using the command line.
You can change the selected python version that is used to run scripts inside VSCode by clicking on a button on the bottom right of the screen:
Or by executing the command (Ctrl+Shift+P), on top of the screen: > Python: Select Interpreter
Finally, select the 3.12 version.
Another problem could be that you have set up some virtual environment. If so, open the terminal (the integrated terminal of VSCode for example) and type <env-dir>/Scripts/activate
. You know you have some virtual environment if you have run some command along the lines python -m venv <env-dir>
or virtualenv <env-dir>
. VSCode should be able to find that you are using a virtual environment so it will default to using that python version. Note it isolates the packages from the installed python.
Once activated the virtual environment, you can safely run python -m pip install numpy
to install packages in it. It is heavily recommended to run python scripts as modules by writing python -m ...
before them. See this SO question.
In this answer I assume you already have installed the Python Extension from Microsoft.