Skip to content
Advertisement

Modules at VS code notebook [closed]

I am trying to import numpy and matplotlib modules at VS notebook and it shows up the usual error when the module has no installed “No module named ‘numpy'”.

The problem is I already installed this packages by pip install in my console, I’m using WSL in my Windows 10 and I’ve had not problems until now.

Also i try to import numpy from the console in python and it wasn’t succes either, i don’t know why the modules hasn’t been recognize if they’ve been installed.

Advertisement

Answer

if VS notebook means Jupyter Notebooks in VS Code? then the issue is with how you’ve configured your python interpreter. You can look that up on the internet

From your problem description, it looks like you’ve configured the default python interpreter. so you can use which python on Linux or which.exe python on windows to find which actual python on your system you have installed numpy.

and to know which interpreter you’re actually using in a notebook, create a separate code cell and execute

import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)

you will get to know the actual path of your python executable.

Note: One on your terminal should match the one you got in a notebook, only then your terminal installed packages from terminal python interpreter that would be accessible to notebook else it will keep on complaining!

The direct answer to your question: create a separate code cell in your notebook and install whatever packages you need from there.

pip install numpy matplotlib

Note: you may need to restart the kernel.


Question: Now I’m wondering how I know in which python I’m installing the modules ?

Answer: A quick answer I’ve already written, adding another more cleaner solution that could be easy to follow.

I recommend using a virtual environment with your project directory to avoid the conflicts you’re having.

  • creating a virtual environment

Note: Please note that default venv which comes with python3 does not permit creating virtual environments with other versions of Python.

You can do these steps on the terminal! since on any OS, we have a default Python 2.7.18 installed you can check by which python tells /usr/bin/python and /usr/bin/python --version, we can simply install virtualenv

pip install virtualenv

incase you have python3, you can use

python3 -m pip install --user virtualenv

then create a virtual environment of your choice!

virtualenv --python=3.8 my_env38

source it :

source my_env38/bin/activate once you source it, you can install whichever python packages in your my_env38 virtual environment. Now, you can configure the virtual environment in vs code for your project, follow the instructions here

or

simply use these commands assuming you created your virtual environment and activated it as explained above!,

pip install ipykernel

ipython kernel install --user --name=my_env38_for_ipynbs

now you this custom virtual environment will show up in the kernel selection for your Jupyter notebook. Don’t forget to restart the vscode. read it

this way you won’t get confused about where you’re installing the packages since you’re created and configured it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement