VS Code - No module named 'streamlit' - python

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.

Related

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)

Can't figure out "unable to import [module]" in VSCode

I've recently swapped drives on my notebook and decided on a completely fresh install. When setting up VSCode for a new python project, I installed the latest version from python's website, added the appropriate environment variable to the path, removed the length cap for the pathname too, and all that.
python environment variable added to Path
But still, every time I import NumPy or any other module for that matter, it throws an error saying "unable to import [module]".
vscode error
I don't know what else to do, VSCode even recognizes the interpreter (see image 3).
python interpreter selected in vscode
Tried setting up a venv aswell, end up getting the same error.
This is how I normally set up a project and it always works.
(open directory in VSCode)
Within the VSCode Terminal
python3 -m venv .venv
source .venv/bin/activate
At this time vscode should automatically detect that there is a new virtualenv and ask you to use it.
If it doesn't you can use the CMD+Shift+P (CTRL+Shift+P) search for "Select Python Interpreter" and specify the path.
Did you install Numpy in the virtual environment (if you are using one)?
Try to list your installed packages in vscode terminal
python -m pip list
If numpy does not appear, install numpy with:
python -m pip install numpy
Does it work when you run the code? Or is this just a pylint problem?

VS Code Jupyter Notebook not importing packages

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.

Can't import numpy module into python file, but it works with terminal

On Ubuntu I have python 3.6.9
I created new project using PyCharm, into project folder there is venv folder.
When I open venv folder in terminal and do:
python3
import numpy
It works, no any error.
But when I create some file in that venv folder using PyCharm, and try in this file:
import numpy
then I get ModuleNotFoundError: No module named 'numpy' error.
Why can't import module into file? module obviously installed because I can import that using terminal (folder is same for both cases).
What is my mistake ?
Pycharm uses virtual environments for serving modules, so you will have to download the package from either the GUI or by activating the virtual environment in the terminal and then doing the pip install.
if you want to do it from the GUI you can check the official tutorial. (Working and tested)
To do it manually, first to activate the environment in linux go wherever the venv folder is and type source venv/scripts/activate (to activate) and then you'll be able to do pip install numpy. (Not tested but it should work)
Could you share your Pycharm configuration?
Some extra steps are needed in Pycharm to configure venv, otherwise it will just take your normal interpreter site-packages.
Take a look at the Pycharm help page: https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html#existing-environment
looks like you need to install numpy through pip3.
Also, check which version of python is running in pycharm.

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

Categories

Resources