ModuleNotFoundError: No module named 'bs4' - python

I'm still super new to coding! Trying to learn from online tutorials, but I seem to be stuck on the first step! I think I installed bs4, but it's not showing up in python3, is it installed in the wrong place?
robbie$ sudo -H pip install bs4
Requirement already satisfied: bs4 in /Library/Python/2.7/site-packages
Requirement already satisfied: beautifulsoup4 in /Library/Python/2.7/site-packages (from bs4)
Robbies-MBP:~ robbie$ python3
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'bs4'
Any help would be greatly appreciated :)

Requirement already satisfied: bs4 in /Library/Python/2.7/site-packages
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
You have the module installed for Python 2.7, however you're using and trying to import it with Python 3.6.
You have to use pip3 like you use python3.

If you are using python 3 then you shoud use pip 3. In order to install it in ubuntu:
sudo apt install python3-pip
And then use the following command for installing the module bs4:
pip3 install bs4

go to the bottom left of your vs code and select python 3.9.1 64 bit (depending on the version of your python). it'll work.
it works for me

Just a quick note but a lot of times when this happens and you are running python3 and you used pip3 to install bs4 or another module but are running two different versions of python3 (eg 3.4 and 3.6). Sometimes just doing a normal update in a distro like CentOS installs a newer version of python, but not the corresponding pip.
Take for example here where a user is running python3.6 but all modules are installed only for python3.4 and there is no pip3.6 available.
http://realtechtalk.com/python_ModuleNotFoundError_No_module_named_bs4_even_though_you_have_the_module-2267-articles

I was having the same issue but managed to solve it with custom install script in the code. This tries to import and if it fails, automatically installs it
def safe_import(module):
try:
exec(f'import {module}')
globals()[module] = eval(module)
except ModuleNotFoundError as err:
import os
os.system(f'pip install {module}')
exec(f'import {module}')
globals()[module] = eval(module)

Related

How to uninstall redundant version of python3

The situation I face is that I seem to have two python3.6s on my MacBook Pro, one with the .dmg installer from the official python website, and one installed via Homebrew ( inferred by others, I don't remember whether it is right or not) :
TreaserdeMacBook-Pro:~ treasersmac$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
'['',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-
dynload', '/Users/treasersmac/Library/Python/3.6/lib/python/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages']
And this brings a result that even some packages have been successfully installed through pip3, they cannot be imported to python3 successfully, like:
TreaserdeMacBook-Pro:~ treasersmac$ pip3 install beautifulsoup4
Collecting beautifulsoup4
Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB)
100% |████████████████████████████████| 92kB 4.3kB/s
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.6.0
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'bs4'
the same for the module Pillow, Installed Pillow but still get : ModuleNotFoundError: No module named 'PIL'
It is #hoefling that points out that I may have too many versions of python3.6.
Therefore, I think it reasonable to remove one of them.
Actually, I have no idea why there are two of them, and the time I began to learn and install python was about 2 years ago, so I cannot recall the details( that's why I use 'seem' at the beginning ), but one thing for sure is that I don't need two of them.
I use pycharm to code python. My operating system is macOS Sierra 10.12.6.
So the problems are:
which one of these python3.6s should I remove and how(which cmd to use)?
(Actually I don't quite understand how those packages work, like Pillow, how they link to python, can they serve for both version of python3.6, or can be directed to serve for a specific one, but how?.. And if I uninstall both of two versions and reinstall python3, can the packages I installed before work? )
other better solutions.
It's a problem that bugged me for a long time, I will appreciate it very much if someone can help me out.

Python: "ImportError: no module named xlrd" after installing xlrd using pip

After installing xlrd using pip, I am still unable to get it to work in my script. I'm not sure if I am missing something completely obvious - please help!
Currently, entering pip list on command line gives the following:
pip (9.0.1)
setuptools (32.1.0)
wheel (0.29.0)
xlrd (1.0.0)
And entering pip show xlrd gives:
Name: xlrd
Version: 1.0.0
Summary: Library for developers to extract data from Microsoft Excel (tm) spreadsheet files
Home-page: http://www.python-excel.org/
Author: John Machin
Author-email: sjmachin#lexicon.net
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires:
Yet, if I go into the python environment (via typing python on command line) and try to import, here's what I get:
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlrd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xlrd
Anyone has encountered this problem before? I also have python 3 installed, and I downloaded xlrd via pip3. Yet import xlrd doesn't work on either versions (2 or 3).
you can try this:
import pip
package_name='xlrd'
pip.main(['install', package_name])
First, you can check if such package actually exists on given directory (simply ls /usr/local/lib/python2.7/site-packages to see if there is __init__.py there).
If package is on place, you may check environmental variable $PYTHONPATH or just python -c 'import sys; print(sys.path)', which prints all directories where Python takes libraries from.
If everything is in place and sys.path contains site-packages (if that's not your first package installed via PIP I assume that everything's correct there), it might be problem with permissions. Please check some other stackoverflow question:
Cannot import a python module that is definitely installed (mechanize)
If package was installed with root's rw permission only - that would be the case.
Uninstall and install xlrd. May be at installing time you would have got some error. This error shows only if xlrd is not installed.
Use sudo pip install xlrd to install in python 2.7
I solved the issue with upgrading pip and following Bibek Ghimire's answer. It seems that I was using pip version 9 whereas version 18 has been already realized.
pip list
pip install xlrd
pip install xlrd == 1.2.0
now go to your IDE and start typing
import xlrd
its done now!

How to solve importing python packages " No module named "?

I am coming across a strange problem, I already installed python packages but it doesn't recognize them. please note that some packages are recognizable.
An example of importing a package
ActivePython 2.7.5.6 (ActiveState Software Inc.) based on
Python 2.7.5 (default, Sep 16 2013, 23:07:15)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
Whenever I try to install it using Pip like
sudo pip install requests
I got this message whenever I try to install the same package.
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/site-packages
What should I do to solve this problem ?
UPDATE_1
pip --version
pip 7.1.2 from /usr/local/lib/python2.7/site-packages
Find where pip installs your package and add the path to
export PYTHONPATH=<path>
#my path was /Library/Python/2.7/site-packages
in your .bashrc or .profile file.

psycopg2 module cannot be found by Python2.7

I installed psycopg2 via pip, but my programs are having trouble finding it.
So, I tried to install psycopg2 via pip again:
user#ubuntu:~/Desktop/progFolder$ sudo pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages
Cleaning up...
Then I tried to use a program that imports it:
user#ubuntu:~/Desktop/progFolder$ python myProg.py
Traceback (most recent call last):
File "myProg.py", line 6, in <module>
import psycopg2
ImportError: No module named psycopg2
And I have tried just importing directly in python:
user#ubuntu:~/Desktop/progFolder$ python
Python 2.7.5 (default, Nov 9 2014, 14:14:12)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named psycopg2
So I printed my python path.
>>> import sys
>>> print sys.path
['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']
And noticed that the path does contain the path to psycopg2.
psycopg2 in /usr/local/lib/python2.7/dist-packages
So, I have no idea on why this is happening. Any help would be appreciated.
UPDATE:
I have done
>>>help()
>>>modules
And psycopg2 was not listed among the other modules. (this does not help me but may help you help me)
Your pip looks ok (that is, it's the system/default one). Your Python executable, however, is something that didn't come by default with 14.04 LTS (e.g., on my 14.04 system, it's /usr/bin/python). Did you install that Python yourself? Then you need to install (and use) the corresponding pip as well. (Normally, Python would have come with a pip installation, but apparently in this case, it didn't.)
pip can be fairly simple installed from its installation instructions.
Though first, verify that
you did install /usr/local/bin/python yourself. That is, it didn't come with some other piece of software that you installed and that, along the way, decided to install Python there.
you want to use /usr/local/bin/python (I guess it is a more recent version of Python 2.7; the default 14.04 LTS one appears to be 2.6.7 as of 2015-08-03).
From your python path print, it looks like it doesn't have /usr/local/lib/python2.7/dist-packages included in it. You can add it in one way by:
sys.path.insert(0, "/usr/local/lib/python2.7/dist-packages")

Can't install module with python-pip properly

I would like to install a module but pip is not installing it in the right directory which I assume should be /usr/local/lib/python2.7/site-packages/. After all, I just installed Python 2.7.2 today. Originally I had 2.6.5 and had installed modules successfully there. So I think something is wrong with my Python path.
How to have all my module installations go to the proper python2.7 directory?
s3z#s3z-laptop:~$ pip install requests
Requirement already satisfied: requests in /usr/local/lib/python2.6/dist-packages/requests-0.6.1-py2.6.egg
Installing collected packages: requests
Successfully installed requests
s3z#s3z-laptop:~$ python
Python 2.7.2 (default, Oct 1 2011, 14:26:08)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
>>> ^Z
[3]+ Stopped python
Also here is what my Python directories look like now http://pastie.org/2623543
After you installed Python 2.7, did you install the Python 2.7 version of easy_install and PIP? The existing installations are configured to use Python 2.6 by default which may be causing your issue.
You are probably using pip linked to python2.6, instead of 2.7. If you have installed pip properly with python2.7, you can do:
pip-2.7 install requests
If not, try installing this way:
curl -O http://python-distribute.org/distribute_setup.py
[sudo] python2.7 distribute_setup.py
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
[sudo] python2.7 get-pip.py

Categories

Resources