Jupyter packages - python

I am trying to import certain packages as I am working with Jupyter notebook files, and most of the packages seem to be missing, even though I have installed them. For example, when I do the command: from bs4 import BeautifulSoup or import requests
I get the error saying ModuleNotFoundError: No module named 'bs4' for the first one and a similar one for importing requests as well. I have tried pip install requests and pip install bs4, but same issue persists. I have installed them on:
"(base) aminnazemzadeh#amins-MacBook-Pro ~ % " which seems to be my home directory, and I also have anaconda3 installed alongside python3. What is the issue that I cannot import these modules.
I am using visual studio if it makes any difference
Once I add :
!pip install requests
!pip install bs4
I get:
/Users/aminnazemzadeh/.zshenv:.:1: no such file or directory: /Users/aminnazemzadeh/.cargo/env
Requirement already satisfied: requests in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (2.28.1)
Requirement already satisfied: charset-normalizer<3,>=2 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (2022.9.24)
/Users/aminnazemzadeh/.zshenv:.:1: no such file or directory: /Users/aminnazemzadeh/.cargo/env
Requirement already satisfied: bs4 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from bs4) (4.11.1)
Requirement already satisfied: soupsieve>1.2 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from beautifulsoup4->bs4) (2.3.1)
followed by this warning:
ModuleNotFoundError Traceback (most recent call last)
Cell In[7], line 4
2 get_ipython().system('pip install bs4')
3 from urllib.request import urlopen
----> 4 from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
Thanks

probably you're installing the packages on an environment other than the one vs code is using. you can try installing the packages directly from your jupyter notebook by running the following code in a notebook cell. the current best practice to be running installs in the notebook is using the magic commands %pip or %conda:
%pip install requests beautifulsoup4
# or
%conda install requests beautifulsoup4
this should install the packages in the same environment that the notebook is running on.
note that you may need to restart the kernel to use the affected packages.
sources:
Jupyter Discourse Forum - Location of libraries or extensions installed in JupyterLab
Jupyter Discourse Forum - Why users can install modules from pip but not from conda?
Installing Beautiful Soup
ps: thanks #wayne for the comments regarding the current best practices for installing on the current running environment.

If you're using conda, you should install via conda whenever possible. When you install via pip, conda loses some of its ability to manage dependency versions.
Try creating a new conda environment, install the needed packages via conda, then set the kernel to your new environment in vscode. Dedicate conda environments to specific projects. It is okay to have a default/generic environment for playing around but not for any significant work as you can easily create errors in your other work if a dependency changes to an incompatible version.
Conda cheet sheet for reference if you need it: https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf
You will need the Jupyter extension in vscode if you do not already have it installed.
You will also need to install the corresponding jupyter package in your conda environment.

Related

torch virtual env not working python 3.7 , what am i doing wrong?

so i installed pytorch using conda into a virtual env while referring to this video
https://www.youtube.com/watch?v=vBfM5l9VK5c
i have activated the env
now inside jupyter notebook i run
import torch
print(torch.__version__)
and it works but when ever i run this in .py file and run it through terminal it gives me this error
import torch
ModuleNotFoundError: No module named 'torch'
if I try to pip install pytorch it says
Requirement already satisfied: torchvision in c:\users\kiit\anaconda3\envs\torch\lib\site-packages (0.10.0)
Requirement already satisfied: numpy in c:\users\kiit\anaconda3\envs\torch\lib\site-packages (from torchvision) (1.20.3)
Requirement already satisfied: torch==1.9.0 in c:\users\kiit\anaconda3\envs\torch\lib\site-packages (from torchvision) (1.9.0)
Requirement already satisfied: pillow>=5.3.0 in c:\users\kiit\anaconda3\envs\torch\lib\site-packages (from torchvision) (8.2.0)
Requirement already satisfied: typing_extensions in c:\users\kiit\anaconda3\envs\torch\lib\site-packages (from torch==1.9.0->torchvision) (3.7.4.3)
so what is going on exactly??
You might have more than one python versions installed on your system i.e One with conda and one separately.
In order to check that, you can go to control panel --> Programs and features. There you can figure out how many python installations are present on your system. Delete the one you dont need and your problem will be resolved.
I found this answer that could be related:
https://stackoverflow.com/a/57753725
Credits to trsvchn for the above answer link.
Perhaps also try opening jupyter notebook from the command line if that doesn't work.

installing doesn't resolve ModuleNotFoundError: No module named 'mpl_finance'

The title says it all. Upon trying to run code related to this matplotlib candlestikck tutorial, I got the error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-5aa61276079d> in <module>
2 import numpy as np
3 import yfinance
----> 4 from mpl_finance import candlestick_ohlc
5 import matplotlib.dates as mpl_dates
6 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'mpl_finance'
So I installed it and restarted the kernel but still no dice. Next, I followed someone's advice on here and installed mplfinance and restarted the kernel, again no go. I rebooted, nil. I even tried installing "mlp_finance" as another answer on here suggested hoping it was some weird name clashing thing but again it didn't work. Why cannot I import mplfinance?
Addition 202007311328: this is a jupyter notebook; I can install and import fine on the command line. Installing mpl_finance from inside jupyter with "!pip3 install mpl_finance" produces no error:
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: mpl_finance in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (0.10.1)
Requirement already satisfied: matplotlib in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from mpl_finance) (3.2.1)
Requirement already satisfied: numpy>=1.11 in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from matplotlib->mpl_finance) (1.18.4)
Requirement already satisfied: cycler>=0.10 in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from matplotlib->mpl_finance) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from matplotlib->mpl_finance) (2.4.7)
Requirement already satisfied: python-dateutil>=2.1 in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from matplotlib->mpl_finance) (2.8.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from matplotlib->mpl_finance) (1.2.0)
Requirement already satisfied: six in /home/pi/src/pyfin1/pyfin1.env2/lib/python3.7/site-packages (from cycler>=0.10->matplotlib->mpl_finance) (1.14.0)
but the same error happens when I try to import it.
edit 2020071343 here's an image of my tying to install mplfinance and then use it. anything pop out at you?
solved, see https://github.com/jupyter/notebook/issues/3311
In my experience, unless the module has been misspelled (which doesn't appear to be the case based on your screenshot above), ModuleNotFoundError usually indicates that the install location is not within the search path for importing modules.
Since this is working for you under ipython, so have a simple way to find where your system installed mplfinance:
In [1]: import mplfinance as mpf
In [2]: mpf.__file__
To find out where your system is searching for modules
import sys
print(sys.path)
If you run the above for both ipython and in your jupyter notebook, I am guessing that you will find that sys.path is different for your ipython installation than it is for your jupyter notebook (and that the notebook's search path does not include the install location). This could be for various reasons such as environment variables, where jupyter is installed, whether you are running virtual and/or conda environments, etc. You can read more about the module search path here: https://docs.python.org/3/tutorial/modules.html#the-module-search-path
Note that if you are using a virtual environment, or conda environment, you have to (1) activate the environment before installing mplfinance and (2) activate the environment before running jupyter notebook. It may also be necessary to have a separate install of jupyter notebook in the activated environment.
Let me know if the above gives you enough information to resolve the issue. If not, please provide the output from the above commands and I will do what I can to help further. All the best. --Daniel
This had happened to me once. In my case the problem was:
Usually pc had 2 versions of python installed - Python 2, Python3. If you run pip install.. . It just install that module to python2 or Python3 by own.
Solution :
If your program runs under python3
python3 -m pip install mpl_finance
If your program runs under python2
python2 -m pip install mpl_finance
Hope it solves your problem.

Cannot import name 'WordCloud'

I am using Jupyter Notebook and trying to build a wordcloud. Turns out there are some issues with the pillow package and the internet is full of talks around it. I was geetting the DLL error initially. I tried a lot of different things and not sure which one worked but right now, I am getting the 'cannot import name' error.
Some details from Anaconda Prompt-
>python -m pip --version
pip 18.0 from C:\Users\Kritika.Jalan\Anaconda3\lib\site-packages\pip (python 3.6)
>python -m pip install wordcloud
Requirement already satisfied: wordcloud in c:\users\kritika.jalan\anaconda3\lib\site-packages (1.5.0)
Requirement already satisfied: numpy>=1.6.1 in c:\users\kritika.jalan\anaconda3\lib\site-packages (from wordcloud) (1.15.0)
Requirement already satisfied: pillow in c:\users\kritika.jalan\anaconda3\lib\site-packages (from wordcloud) (4.0.0)
Requirement already satisfied: olefile in c:\users\kritika.jalan\anaconda3\lib\site-packages (from pillow->wordcloud) (0.45.1)
Details from Jupyter Notebook -
from wordcloud import WordCloud
ImportError: cannot import name 'WordCloud'
import PIL
print(PIL.PILLOW_VERSION)
5.0.0
What am I doing wrong here?
Mostly, having a can not import name error even after successful library installation, is result of conflict with your local file name and python library name.
Do you have your own python file in your local named wordcloud.py ?
If yes, that is causing conflict with wordcloud.py in python library.
Simply rename your script.
Thanks
You just need to change your file name that is same to the library name

webkit_server installed but cannot find in script

I have installed webkit-server and dryscrape - but when I run a script I get an error when using a dryscrape function. The error is as follows:
"No such file or directory: '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/webkit_server'"
However, when trying to install webkit_server, I get:
Requirement already satisfied: webkit_server in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (1.0)
To show I already installed it, see link image.
Suggestions from my side are:
There is a difference between webkit_server and webkit-server?
Webkit_server is in the wrong place and can't be found by dryscrape?
I tried to install both using gitclone - same error
Any suggestions would be useful.
List of pip3 installed packages includes webkit-server

No module named googleapiclient.discovery

I have been looking to implement the example Python scripts I have found online to allow me to interact with the YouTube API as per the GitHub link found here
The problem I am having is with the import statement at the start:
import argparse
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
The online documentation requires the following command to install the googleapiclient library:
pip install --upgrade google-api-python-client
However, once installed I am still receiving an error that googleapiclient.discovery cannot be found. I have tried reinstalling via pip, with the following command line output generated, suggesting all is well:
Requirement already up-to-date: google-api-python-client in g:\python27\lib\site-packages (1.7.4)
Requirement not upgraded as not directly required: httplib2<1dev,>=0.9.2 in g:\python27\lib\site-packages (from google-api-python-client) (0.9.2)
Requirement not upgraded as not directly required: google-auth>=1.4.1 in g:\python27\lib\site-packages (from google-api-python-client) (1.5.0)
Requirement not upgraded as not directly required: google-auth-httplib2>=0.0.3 in g:\python27\lib\site-packages (from google-api-python-client) (0.0.3)
Requirement not upgraded as not directly required: six<2dev,>=1.6.1 in g:\python27\lib\site-packages (from google-api-python-client) (1.10.0)
Requirement not upgraded as not directly required: uritemplate<4dev,>=3.0.0 in g:\python27\lib\site-packages (from google-api-python-client) (3.0.0)
Requirement not upgraded as not directly required: rsa>=3.1.4 in g:\python27\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (3.4.2)
Requirement not upgraded as not directly required: cachetools>=2.0.0 in g:\python27\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (2.1.0)
Requirement not upgraded as not directly required: pyasn1-modules>=0.2.1 in g:\python27\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (0.2.2)
Requirement not upgraded as not directly required: pyasn1>=0.1.3 in g:\python27\lib\site-packages (from rsa>=3.1.4->google-auth>=1.4.1->google-api-python-client) (0.1.9)
pyasn1-modules 0.2.2 has requirement pyasn1<0.5.0,>=0.4.1, but you'll have pyasn1 0.1.9 which is incompatible.
What am I doing wrong?
Thanks
In case you are running Python3 (python --version), perhaps you should run this instead:
pip3 install google-api-python-client
Another quick way to counter this problem could be to install the package in the same folder as your code:
pip install google-api-python-client -t ./
That's not ideal but it will definitely work.
Or if you prefer to move external libraries to a lib/ folder:
pip install google-api-python-client -t ./lib
in that last case you will also need this at the beginning of your Python code:
import os
import sys
file_path = os.path.dirname(__file__)
module_path = os.path.join(file_path, "lib")
sys.path.append(module_path)
from googleapiclient.discovery import build
This solution is only applicable to those using "Visual studio" for building flask apps.(others can try though)
The only thing you need to check is "from where am I importing all my libraries" follow the process below while creating new env.
Python environments >(Right click) > Add new env > check the "View in python environments window".
I faced similar issue while I was trying to write code involving 'YouTube API' in VS Code. On the suggestion by many folks from online coding forums I ran
pip install --upgrade google-api-python-client
but it didn't help.
Taking following steps resolved the issue for me:
In VSCode go to 'Settings' (Ctrl + , on Windows), inside 'Search settings' enter venv and under the heading for 'Python: Venv Path' enter the path for your virtual environment as seen in the following screenshot:
settings for Python: Venv Path in VS Code
And, then click on the Python interpreter in VS Code as seen below: (the selected interpreter reflects at the bottom left corner of the VS Code editor)
Python interpreter path

Categories

Resources