Why is beautifulsoup4 not importing to my script? - python-3.x

Before I explain my problem I want to start by saying that I have looked all over this site for an answer and tried almost all of them, to no avail.
I am on a windows machine, in a virtual environment. I have a command prompt opened in the virtual environment. I have already pip installed over a dozen modules that are being imported into my script and working perfectly. When I run the line:
from bs4 import BeautifulSoup
I get this error:
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
In my command prompt I can do 'pip list' and see beautifulsoup4 version 4.9.0. As well as in my virtual environment folder I can go into Lib\site-packages and I see all of my other installed packages that work in my script, along with a folder called 'bs4' and one called 'beautifulsoup4-4.9.0.dist-info'. I have also tried pip uninstall beautifulsoup4 and then re installed it, confirming that it did not show up in pip list between running those.
I am completely at a loss for why I cannot access the bs4 module. Anyone have any ideas for me to try? Thanks in advance.
EDIT: I am currently using VS Code and have pip installed several other modules including pymysql and google-api packages. I have successfully connected to my gmail and Amazon RDS database, so i know the other modules are working properly. For some reason its only BeautifulSoup4 I am having issues with.
EDIT 2: I opened up ipython from my virtual environment, and I entered from bs4 import beautifulsoup and i get this error.
ImportError: cannot import name 'beautifulsoup' from 'bs4' (C:\Users\Brian\Desktop\Projects\VirtualENVs\test_venv\Lib\site-packages\bs4\__init__.py)
EDIT 3: I finally just opened a new command prompt and pip installed beautifulsoup4 onto my computer outside of the virtual environment and now it works fine.

Related

Python Packages Not Working From Same Directory As Working Packages

I am using Python38.
I am trying to create a script to create/modify google sheets. So I came across several articles detailing how to accomplish this: article1, article2, article3.
Essentially it boils down to using two python packages: GSPREAD and OAUTH2CLIENT.
The problem is trying to import either of these returns the Module Not Found Error.
Running this:
import sys
print(sys.path)
I see the site-packages directory is returned in the list.
I have many other packages installed in the same directory that do work (e.g. bs4, Django, Flask, matplotlib, etc, etc), so why are these two packages not being found? Does the PATH variable need to be updated?
Thanks in advance!!
So I failed to mention in the question that I was trying to do the pip install from the command line (cmd on windows). Which successfully installed the packages, but did not allow visibility to the rest of the python environments.
The way to get this to work was to run the pip install command from the Anaconda Prompt (Anaconda's command line).

ModuleNotFoundError: No module named 'flask_wtf' in python

I'm new to python so please be patient if i ask a silly question.
I am trying to use flask wtf class as seen in a tutorial.
My first line of the program is
from flask_wtf import FlaskForm
It gives me the error ModuleNotFoundError: No module named 'flask_wtf'
I have followed the following steps to make sure i have activated the virtual environment and flask wtf was installed in the virtual environment and not global.
Step 1: Went to the virtual environment and activated the environment C:\Users\Shraddha\PycharmProjects\FlaskProject\venv\scripts activate
Step 2: Installed flask wtf
C:\Users\Shraddha\PycharmProjects\FlaskProject\venv\scripts pip install flask-wtf
I see that flask-wtf is installed correctly in the folder C:\Users\Shraddha\PycharmProjects\FlaskProject\venv\Lib\site-packages
However when i try to run it using command prompt it does not give me anything and when i try to run it using sublime text it gives me ModuleNotFoundError: No module named 'flask_wtf' error.
I know this is a flask_wtf import error because other programs are executing as expected. Any help ?
If you start your virtual environment in the same dir as the file you are running doesn't that give access to the libraries in the install (ie everything shown in 'pip freeze' for the env)?
I faced same issue and tried all the approaches given on stackoverflow ImportError: No module named flask_wtf but didn't work for me.
I am using ubuntu 20.04 LTS operating system.
Note: To follow the given step you must activate your virtual environment.
I used:
pip3 freeze
which listed following directories
libries in my virtual environment
Then i tried the below command in the same directory
git clone git://github.com/lepture/flask-wtf.git
cd flask-wtf/
python3 setup.py install
Through the above commands i successfully installed flask_wtf as show below
installed flask_wtf
but then i faced the below error as i used email_validator in my forms.py file:
Exception: Install 'email_validator' for email validation support
Then I installed email_validator using following command
pip3 install email_validator
Then i successfully run my flask application. I am sure it will work for you too.

Jupyter notebook doesn't find an import when ipython does [duplicate]

I've gotten myself into some kind of horrible virtualenv mess. Help?!
I manage environments with conda. Until recently, I only had a python2 jupyter notebook kernel, but I decided to drag myself kicking and screaming into the 21st century and installed a python3 kernel; I forget how I did it.
My main (anaconda) python defaults to 2.7.
So here I am, merrily trying to use beautiful soup from inside my shiny new python3 kernel, and I don't seem to be able to do anything to get at whatever environment it's finding packages in. Viz (all from notebook):
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
Ok, fine, I'll install it using shell magic. Right? Right?
! pip install bs4
--> Collecting bs4
Downloading bs4-0.0.1.tar.gz
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /Users/[MY-USER]/anaconda/lib/python2.7/site-packages (from bs4)
[...]
Successfully built bs4
Installing collected packages: bs4
Successfully installed bs4-0.0.1
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
Oh no. Does it think I'm in a 2.7 env even though I'm running a python3 kernel? That won't do.
! conda info --envs
--> # conda environments:
#
flaskenv /Users/[MY-USER]/anaconda/envs/flaskenv
mesa /Users/[MY-USER]/anaconda/envs/mesa
py35 /Users/[MY-USER]/anaconda/envs/py35
root * /Users/[MY-USER]/anaconda
Ok, I can fix that. One of those is a 3.5 env.
! source activate py35
--> prepending /Users/[MY-USER]/anaconda/envs/py35/bin to PATH
! conda install beautifulsoup4
--> Fetching package metadata .......
Solving package specifications: ..........
# All requested packages already installed.
# packages in environment at /Users/[MY-USER]/anaconda:
#
beautifulsoup4 4.4.1 py27_0
concerning...
! pip install bs4
--> Requirement already satisfied (use --upgrade to upgrade): bs4 in /Users/[MY-USER]/anaconda/lib/python2.7/site-packages
more concerning...
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
ARRGH!!! headdesk Am I going to have to kill the kernel in order to fix this (and re-run a bit of work)? Is killing the kernel even going to work? How do I get my jupyter kernel to know what environment it's supposed to be running under?
thanks!
This is a tricky part of ipython / Jupyter. The set of kernels available are independent of what your virtualenv is when you start jupyter Notebook. The trick is setting up the the ipykernel package in the environment you want to identify itself uniquely to jupyter. From docs on multiple ipykernels,
source activate ENVNAME
pip install ipykernel
python -m ipykernel install --user --name ENVNAME --display-name "Python (whatever you want to call it)"
If you only want to have a single Python 3 kernel, from the conda environment, just use python -m ipykernel install --user and it will reset the default python to the one in the virtualenv.
And yes, you will need to restart the kernel and re-run the prior steps.
See Also Using both Python 2.x and Python 3.x in IPython Notebook
#tschundler's solution works perfectly if your environment has already been created.
If you want to change the default kernel at the creation of your virtual environment and avoid any manual configuration, you just need to add jupyter at the end of the conda command:
conda create --name ENVNAME python=PYTHONVERSION jupyter
The correct kernel will then be used when you use ipython or jupyter notebook.
In my case somehow jupyter wasn't able to 'pick' the virtual environment's python. So I had to edit ~/.local/share/jupyter/kernels/{my_env_name}/kernel.json
and add path to the interpreter
in the argv key.
There is also an easy way here
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install tornado==4.5.3
pip install ipykernel==4.8.2
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able to switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
This worked for me. source
pip install --user ipykernel
python -m ipykernel install --user --name=myenv
Output
Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv
and go to above directory
open kernel.json
{
"argv": [
"/home/user/anaconda3/envs/myenv/bin/python", # path to your virtual environment python
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "myenv",
"language": "python"
}
I know this is an old question, but nobody has mentioned nb_conda_kernels, which was made to solve this problem. You must have it installed in the environment from which you launched the running notebook, and any other environment which has kernel/s installed (ipykernel, r-irkernel, etc ) will be available as an option (Start Preferred Kernel):
Sharing my experience here, in case someone made the same mistake:
TL;DR
In VSCode I created the virtual environment from an Ubuntu terminal but forgot to activate the WSL extension too. Once I activated the WSL extension, I was able to change the Jupyterkernel to my virtual environment.
Full explanation
Using VSCode I work with the WSL extension (see here). I created my virtual environment from an Ubuntu terminal [Created a new terminal and selected 'Ubuntu (WSL)'] and then went to my Jupyter notebook to change my kernel, but it did not appear in the list.
I tried the solution proposed above of adding the path to kernel.json in my virtual environment, adapting to my case (I don't use conda) but I was surprised to see that the folder bin in my environment did not have a file named python.
The solution was to activate the WSL extension: lower right corner > click on the arrows (Open a remote connection) > reopen folder in WSL.
After this, I could see in the explorer two new entries in the bin folder for Python and Python3 (the arrow on the right side seem to indicate it's a symbolic link).
Virtual environment now shows the python symbolic links
Then I went to my Jupyter notebook and changed the kernel to my virtual environment.
In the end, I did not have to change the path in kernel.json.
P.S: The above is my understanding of my problem and the solution given my non-existing knowledge about Linux, so any comments to complement/improve this answer are very welcome!

Anaconda3 libhdf5.so.9: cannot open shared object file [works fine on py2.7 but not on py3.4]

I just tried to use pd.HDFStore in IPython Notebook with a Python 3 kernel (Anaconda 2&3 on Ubuntu 14.04)
import pandas as pd
store = pd.HDFStore('/home/Jian/Downloads/test.h5')
but it throws the following error
ImportError: HDFStore requires PyTables, "libhdf5.so.9: cannot open shared object file: No such file or directory" problem importing
I initially thought it's because pytables is somehow missing, but when I check $source activate py34 and $conda list, pytables 3.2.0 is already installed under anaconda python3 environment.
Also, if I switch to Python 2, for example, $source activate py27 and start ipython notebook, it works properly and no import error is thrown.
I guess that I must miss something for configuring pytables under anaconda python 3 env, but I cannot figure it out. Any help is highly appreciated.
Update:
I just tried on a fresh install of Anaconda3-2.3.0-Linux-x86_64 from official website and it ends up with the same error. When I try $locate libhdf5.so.9 in command line, nothing shows up.
This is a known issue that we are working on. When it is fixed, conda update --all will update the libraries and fix the issue.

Pip in python34 does not work properly (OS Windows)

I'm writing a data extraction from xls python script using xlrd. I'm having trouble however with pip, I just managed to get a positive reply from the cmd when i requested (I had some trouble with it at first, but a few posts helped out)
py -m install requests
The response I now get is: "Requirement already satisfied." (with some mention of update since my pip version is 6.0.8 and there's a 7.0.1 available)
However, when I run my code part of which can be seen below I get a syntax error.
import xlrd
from xlrd import open_workbook
import xlwt
workbook = xlrd.open_workbook('editable.xls')
The error:
File "C:\Python34\lib\site-packages\xlrd__init__.py", line 1187
print "EXTERNSHEET(b7-):"
^ SyntaxError: invalid syntax
I run your program on Ubuntu/Linux and it runs fine.
I will recommend to follow my instructions for installing the xlrd on your Windows machine.
From here download the latest version of the xlrd.
Then extract the file, and using the command prompt in the new xlrd directory type:
python3 setup.py install
Unfortunately I don't own a Windows machine so I can't guaranteed to you that this is the right way to install it.
The trick was that you should run the setup.py using the python3 and not the python
(note that on my machine, I run the above command using sudo, which means in Linux administration permissions, so if you have any problems try to run your command prompt with administration permissions.)
I test this code on my Ubuntu machine:
import xlrd
from xlrd import open_workbook
workbook = xlrd.open_workbook('Untitled.xls')
I remove the import xlwt as I don't have this module, please try and let me know.
Edit: Also you will need the setuptools installed on your computer.

Resources