I'm using a Jupyter notebook, Python 3.5, and a virtual environment.
Within my virtual env I did:
(venv) > pip install BeautifulSoup4
Which seemed to run fine b/c the terminal output was:
Downloading beautifulsoup4-4.6.0-py2-none-any.whl (86kB)
100% |████████████████████████████████| 92kB 297kB/s
Installing collected packages: BeautifulSoup4
Successfully installed BeautifulSoup4-4.6.0
But when I run my Jupyter notebook I get:
ImportError Traceback (most recent call last)
<ipython-input-1-5fe69888b1a1> in <module>()
5 from itertools import groupby
6 import pickle
----> 7 import bs4
8 import matplotlib.pyplot as plt
9 get_ipython().run_line_magic('matplotlib', 'inline')
ImportError: No module named 'bs4'
And I can't change the line import bs4 to from bs4 import BeautifulSoup which I've seen on other SO posts b/c its read-only and I'm not supposed to
Since you are using Python 3, I think you needed to do
pip3 install BeautifulSoup4
Just pip install would have been for the Python 2 package.
Other things to check:
First make sure you ran both the BeautifulSoup install in your virtualenv, and the python3 prompt or Jupyter notebook in your virtualenv. If you did the install in your root environment and the notebook is run in your virtualenv, or vice versa, you might have a mismatch in your site-packages directory.
Ensure that your package indeed installed by typing pip3 list at a command prompt, and noting that it appeared in the list.
Check that you Jupyter notebook is indeed running a Python 3.5 kernel (upper right hand corner of the browser window)
Check that the path where the package is installed is in your sys.path
Open a Python3 prompt or in Jupyter and run
import sys
print (sys.path)
I had the same problem, bs4 was installed in my virtual environment along with Jupyter Lab. Jupyter would throw the ModuleNotFound Error but if I opened the notebook in Visual Studio Code and chose the .venv as the interpreter, it worked.
I got it to work in Jupyter Lab by uninstalling from the virtual environment and installing with sudo apt-get install python3-bs4 instead.
Related
I installed the numpy with Conda, pip and pip3, also tried installing from the requirements.txt file but I am still getting the following issue. Has anyone encountered this before and could please kindly suggest something?
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_732/1818554654.py in
1 import os
----> 2 import numpy as np
3 import matplotlib.pyplot as plt
4 import pandas as pd
5 import random
ModuleNotFoundError: No module named 'numpy'
numpy is built-in the default:Python environment in Studio Lab. If you open a new notebook with the File -> New -> Notebook, and choose the default:Python kernel, you should be able to import numpy without having to install the package.
If you would like to have a reproducible environment with a set of packages, see this sample repo on creating custom environments.
Numpy isn't preinstalled as a module you will need to install it yourself using pip or conda. Using notebooks just include a code block with the following:
!python -m pip install numpy
Suggestion to compile a list of required modules in a 'requirements.txt' file and install using:
!python -m pip install -r requirements.txt
Just as an FYI, the base package list is as follows:
Python 3.9 bzip2 build-essential curl git libgl1-mesa-glx nano rsync
unzip wget ca-certificates pip ipykernel-6.4
Have a look at this link for more info.
SageMaker - Manage your environment
So I wanted to import matplotlib to my virtual python version of 3.10.0 (and other versions). I install it as usual:
pip install matplotlib
Everything seems to work, no errors show up with pip. But when I try to run this code snippet in vs code (to see if mpl works):
import matplotlib
print(matplotlib.__version__)
It outputs this:
File "/Users/XYZ/Desktop//pienv.py", line 1, in <module>
import matplotlib
File "/Users/XYZ/Desktop//matplotlib.py", line 1, in <module>
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
But when I do the same in the terminal it outputs the correct version.
It doesn't matter if I have python installed with dmg file or with pyenv, result is the same.
I tried to format operating system to ensure there is no os trash that might be getting in a way. Then I just installed python with pyenv.
I'm using osx 12.0.1
My vscode setup is straightforward, just python extension and python interpreter set to what pyenv has as a local python.
Is there that can be done, or that I'm doing wrong?
Found the answer.
It's just a stupid thing.
Don't name your projects with the names of your libraries when they're in the same directory.
I installed pandas via pip in my virtualenv venv.
When I run import pandas as pd, I get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-588656c123e4> in <module>
3 import tarfile #2
4 from six.moves import urllib #3
----> 5 import pandas as pd #4
ModuleNotFoundError: No module named 'pandas'
I am running Jupiter notebook in the virtual environment. To make sure it installed properly, I opened the python in the virtualenv and ran the import, and it worked fine, what could be the issue? Could it be that though I ran Jupiter Notebook in my venv its not looking at the packages in the virtual environment?
Edit 1: I installed pandas on my machine, not in the virtual machine, and that resolved my issue. However, I want the notebook to look at the packages in my virtual environment, how do I do that?
Try running this in a Jupyter cell
!pip install pandas
I created a new environment and installed some packages in Anaconda prompt:
conda create -n webscap requests scrapy lxml selenium html5lib pandas
activate webscap
conda config --env --add channels conda-forge
conda install -n webscap beautifulsoup4
I can see the selenium is installed by running !conda list -n webscap in jupyter:
selenium 3.9.0 py36_0 conda-forge
but when I import selenium I get:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-12-612ef00a7e75> in <module>()
----> 1 import selenium
ModuleNotFoundError: No module named 'selenium'
I even tried to install it again by conda install -n webscap selenium but got the same error again. Can anyone help please? Thanks!
In case anybody else has experienced the same problem, here is what worked for me:
It turned out that in the new environment I created webscap I was not able to import selenium in jupyter but I was able to do it in python prompt. This happened because the sys.path was different in jupyter from python prompt. In jupyter there was no path looking at the \\envs\\webscap\\, therefore I need to add the python sys.path to the jupyter sys.path like so:
import sys
sys.path
for element in r'C:\Users\<username>\Anaconda3\envs\webscap\python36.zip,C:\Users\<username>\Anaconda3\envs\webscap\DLLs,C:\Users\<username>\Anaconda3\envs\webscap\lib,C:\Users\<username>\Anaconda3\envs\webscap,C:\Users\<username>\Anaconda3\envs\webscap\lib\site-packages,C:\Users\<username>\Anaconda3\envs\webscap\lib\site-packages\win32,C:\Users\<username>\Anaconda3\envs\webscap\lib\site-packages\win32\lib,C:\Users\<username>\Anaconda3\envs\webscap\lib\site-packages\Pythonwin'.split(','):
sys.path.append(element)
Your code indicates that you created webscap instead of webscrap so you need to activate webscap:
conda create -n webscap requests scrapy lxml selenium html5lib pandas
activate webscap ## you created the env as webscap (not webscrap)
conda config --env --add channels conda-forge
conda install -n webscap beautifulsoup4
I am using python 2.7 in Ubuntu and recently updated tensorflow 0.12.1.
I installed jupyter today for my sample code of tf and I need to use matplotlib. It does not find module name matplotlib and ipython in tensorflow has same error.
1. How can I set path in virtualenv or ipython or jupyter?
After activate tensorflow, I need to use jupyter notebook.
This below in the script for error does not work.
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
2. other information: My environments are below.
mickyefromsd#DEKSTOP~$source ~/tensorflow/bin/activate
When I find matplotlib by python script under TF condition and before TF activation, it has below;
/usr/lib/python2.7/dist-packages/matplotlib/
When I type 'which ipython', it has below (not by /usr/bin/ipython) ;
/home/mickeyfromd/tensorflow/bin/ipython
Btw, /tensorflow/lib/python2.7/site-packages/ it has ipython and jupyter.
(not in the same path of matplotlib)
3. My ipython under TF cannot find my existing matplotlib.
(tensorflow) mickeyfromd#DK-DESKTOP:~$ ipython
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
In [1]: import matplotlib ImportError: No module named matplotlib
4. I wanted to setup virtualenv, so I just run this
I followed this site. site:http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
(tensorflow) mickeyfromd#ipython kernelspec install-self --user
.....
Installed kernelspec python2 in /home/mickeyfromd/.local/share/jupyter/kernels/python2
(tensorflow) mickeyfromd#DK-DESKTOP:~$
I cannot move the the folder (in the second step)
How can I make ipython to have path for Matplotlib?
That import error is due to change in environment of the jupyter notebook. You might have installed the packages in one environment and you are running the jupyter notebook in another environment.
I have got two environments (envs) in my Anaconda folder ( I have Anaconda3 folder to be specific ).
(windows key+cmd ) -> open the windows command prompt run as administrator.
Activate (name of the environment) -> eg.: activate tensorflow-gpu
Start installing packages using conda install
Note: For each environment you need to install all the packages you want to use, separately using the same process. This solution is for windows users, might work for linux users not sure though.
Additionally to make sure your conda environment is up to date run:
conda update conda
conda update anaconda
check this out : https://pradyumnamajumder.wordpress.com/2017/09/30/solution-to-the-python-packages-import-error-in-jupyter/