VS Code Jupyter Notebook not importing packages - python

I am new to Python and I am trying to start with some easy machine learning projects. I am trying to import the packages sys, scipy, numpy, matplotlib, pandas, and sklearn to a visual studio jupyter notebook. I am using this test code to see if they import properly:
import sys
print('Python: {}'.format(sys.version))
# scipy
import scipy
print('scipy: {}'.format(scipy.__version__))
# numpy
import numpy
print('numpy: {}'.format(numpy.__version__))
# matplotlib
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
# pandas
import pandas
print('pandas: {}'.format(pandas.__version__))
# scikit-learn
import sklearn
print('sklearn: {}'.format(sklearn.__version__))
When I do this with a jupyter notebook on the website launched from anaconda, it gives me no problems. However, I want to use VS code, but when I run it there, it gives me this:
P5 C:\Users\matti> conda activate base
conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ conda activate base
What is going on and how can I fix this so these packages get imported into my VS Code jupyter notebook? I am very new to Python and many things with coding so this may be a really easy fix.
PS If anyone wants to help me learn a little more about using Python with machine learning(interesting in medical image segmentation) don't hesitate to pm me. Just a student trying to learn :)

To execute imported modules in Jupyter notebook in VSCode, we need to install them in the selected environment (upper right corner of Jupyter).
Install the module in the VSCode terminal (use the shortcut key Ctrl+Shift+` to open a new terminal, it will automatically enter the currently selected environment):
Implementation:
More reference: Jupyter in VSCode.

You have to activate the virtual environment that has packages installed. Initial environment in conda is named 'base'. So, if you are using windows powershell as your terminal run these commands to activate your conda environment.
conda init powershell
then
activate <YOUR_ENVIRONMENT_NAME>
In your case environment name should be 'base'.
If you are using bash in windows environment.
conda init bash
then activate the environment
source activate <YOUR_ENVIRONMENT_NAME>
That should solve your issue.
You can also select default python interpreter for your project at the bottom left in VS Code.
Check the virtual environment docs for more info on environments
It is recommended to create separate environment for each project to avoid version conflicts and keep packages for each project separate.

Related

After two VScode updates the function import (cv or numpy) does not work, even selecting others python interpreter path

I often use python in VScode. However, after two VScode updates, the import function (cv or numpy) is not working. I've tried using the shift+command+p and > python select interprete but none are working for these functions.
I tried use > pip install pylint but it didn't work.
How can I solve this problem? Has someone the same problem in VScode?
It is recommended to use a virtual environment to manage third-party packages and python versions more conveniently.
Create a virtual environment with the following command
python -m venv myenv
After the command is completed, select the virtual environment interpreter just created in the Select Interpreter panel
The new terminal can automatically activate the environment
Now there is no package installed in the new virtual environment, and an error occurs
Install the cv2 package in the new environment, and the error disappeared after installing the package
Steps I would suggest to follow:
install the package again (cv2), even if it is installed.
check vs code if it has any updates for the python extension.
update vs code application.
restart vs code.
hope this helps.

VS Code - No module named 'streamlit'

I am trying to use VS Code and I am importing some modules (for example : pandas and streamlit). The terminal of VS Code tells me "No module named pandas" or "No module named streamlit" whereas it is downloaded on my computer.
VS Code:
Terminal of my computer when trying "pip install streamlit":
Do you know how I could correct this mistake ?
You are in a different env, so before you can import, you will have to append the path of the installed packages to your working environment.
import sys
sys.path.append("/opt/anaconda3/lib/python3.9/site-packages")
import streamlit as st
Please ensure that the environment is consistent. pip installs the library into the real Python environment, and you use the virtual environment.
If you want to use it in the virtual environment, please use the
command conda install to install it.
If you want to use the real python environment, you can use shortcuts "Ctrl+shift+P" and type "Python: Select Interpreter" to change the python environment.

No module named 'sportsreference'

I pip installed the sportsreference package in my command prompt and it shows successful. Yet when I try to call it in my Jupyter Notebook it says no module named sportsreference.
Any help on what I'm doing wrong?
The interpreter that your Jupyter uses to run the cells is not the one you installed that package into. Remember you can have not only different versions of Python interpreter, but also different virtual environments associated with each Python interpreter in your machine.
So either install the package in the interpreter that Jupyter uses, or run the Jupyter from the interpreter you've installed that package into.
The simplest solution is to install it in a Jupyter cell with:
!pip install <your_package_name>
the ! allows you to execute command.
To check which interpreter currently runs your code you can check the output of:
import sys
print(sys.executable)

Sometimes Unable to Import NumPy

When I work in Jupyter Notebooks everything works fine, and I can import numpy and pandas successfully. However, when I try to download the script and then run it in an editor such as PyCharm or Atom, I get an import error: no module named numpy, and the same for pandas. How do I fix this? Is this due to the packages being installed in a different location than where I am downloading the code? Everything is installed with Anaconda, and when I try to do ```conda install numpy`` it tells me that all packages have already been installed.
This may be because Pycharm and Atom are using your default python install rather than your anaconda python environment.
You can configure Pycharm to use your conda environment via (https://www.jetbrains.com/help/pycharm/conda-support-creating-conda-virtual-environment.html).
Anaconda uses virtual conda environments to store the Python interpreter and libraries. If this isn't set up in your IDE, it won't see the libraries. This is described in this post: Use Conda environment in pycharm
Check your PyCharm interpreter options: File > Settings > Project > Project Interpreter. Make sure your desired Anaconda interpreter/environment is selected.
If your Anaconda environment isn't selected, click the Project Interpreter drop-down. if you see it there, select it. If not, click Show All... then + (Add) and browse to the Anaconda folder.
This post describes how to set up conda in Atom:
Using anaconda environment in Atom

When using Jupyter, I have to install python modules with Conda and not cmd prompt. Why?

I'm pretty new to programming so forgive my ignorance.
When installing certain python packages/modules in the cmd prompt I am able to import them fine when using Jupyter Notebook. But some modules Jupyter Notebook cannot import without installing via Conda first. Why is this?
The problem seems to be related to system and environment that you are using and not the programming :)
Since you are a beginner, let us understand the concepts first rather than solving the problem.
Python code is run on an interpreter that is installed on your machine.
Jupyter is a web application that takes code using a given language's interpreter. So Jupyter, on its own doesn't run your code. It uses the interpreter installed on a system(your local machine).
Conda is a package manager and also an environment manager. That means using conda, you can create a virtual environment on your machine and that virtual environment can have its own installation of an interpreter. This virtual environment can have its own copy of the packages/modules as well.
Now comes the best part: The jupyter notebook can be asked to use any of the interpreters including the ones installed on a virtual environment.
So most likely, you are running the jupyter notebook from an environment which doesn't have the required dependencies. So either run the jupyter notebook outside the environment or install the required packages in the environment where your jupyter notebook is running.
To know which environment is being used by your jupyter notebook, run below lines from the jupyter notebook cell:
import sys
sys.executable
If you don't get something like /usr/bin/python then the jupyter is running inside an environment. So you have to install all the packages/modules inside that environment only.

Categories

Resources