skip to Main Content

First of all, I’m using Visual Studio Code as my IDE. The full error message is this:

  File "c:UsersAlexDocumentsCodingPythonezpz.py", line 1, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'

However, when I do "pip install numpy", it says, "Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in c: blah blah"
Also, when I use python in the terminal and type "import numpy" no error occurs and I can use the module normally. Is there a problem with VS Code?

As a mentioned above, I’ve tried "import numpy" in the terminal and it worked.

3

Answers


  1. First of all, please include the full error. But anyways here is your problem as far as I can tell: You have multiple conflicting versions of Python. I would recommend you to delete the python installation after "numpy in c: " Then uninstall and reinstall python. Just incase run the command in command prompt as ADMINISTRATOR. If that doesn’t work, I really dont know how to help you.

    Login or Signup to reply.
  2. At a guess, I’d say that you probably have multiple python interpreters installed. One is being used by default in the terminal, and VSCode is configured to use another.
    In the lower right of your VSCode window, you should see a python version. Click on that and it will show you the path to the current python interpreter, as well as any other available options.
    Then, you can check which version is being used in the terminal. If it’s a bash terminal, type which python or which python3 (try both, it depends on your OS and configuration). If it’s Powershell, type Get-Command python | fl *. These commands will show you which python interpreter is being used at the terminal.
    If you see that VSCode is indeed using a different interpreter, you have a few options for how to proceed.

    1. (Recommended) Learn how to use virtual environments. This takes some work upfront because there’s a little bit of a learning curve, especially if you’re new to Python or programming in general, but it saves you a lot of headache down the road. Virtual environments are a tool that lets you assign a Python interpreter and a set of packages and modules to a specific project. Set up a virtual environment for your project and activate it when you install packages (such as numpy). Here’s a good tutorial if you’re just getting started.
    2. Change the VSCode interpreter. Click on the python version number in the lower right of your window, and then select the path that you saw when you ran the terminal commands from earlier. Now when you run your program, you shouldn’t get errors because this interpreter does have numpy installed.
    3. (PROCEED WITH CAUTION) Change the system default python. If you’re on Windows, you can pretty safely change the default python. This might be the right approach if, for example, you have an old version of Python that you installed by mistake or never plan to use again. In this case, simply uninstall the old version and install a new one if needed. Alternatively, you can update your Path variable to include the new Python install as detailed here.
      Hopefully this goes without saying, but you need to make sure you don’t have programs that depend on the python version that’s getting demoted!
      If you’re on Linux, you need to be much more careful. Some distros (Ubuntu, for example) come with a pre-installed python version that must remain installed and must remain the default. You can break your system messing with this.
      If you’re on Mac, then I don’t have a lot of personal experience to help you so I’d say proceed with caution or choose one of the other options.
    Login or Signup to reply.
  3. If you run your program with python3 like python3 program.py, you need to use pip3 instead of pip to install libraries, so maybe you should try :

    pip3 install numpy
    

    Also, maybe VScode is not running the good version of python, so you can try to change it

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search