Error of importing BeautifulSoup in python 3 - python

I am working with Jupyter Notebook and have python 2.7 and 3.4 installed on it. I installed BeautifulSoup before importing it by this line:
pip install beautifulsoup4
The problem is that it is installed on python 2.7 while beautifulsoup4 is working on python 3.4. I also tried pip3 install beautifulsoup4 to install it on python 3 but the problem is still there and when i do this line: from bs4 import BeautifulSoup i again get the error below:
Error:
ImportError: cannot import name _htmlparser
Does anyone know how i can solve this problem since it appears beautifulsoup4 should be installed on python 3 while mine get installed in python 2??

Assuming you have added both versions to path you should rename the Python 2.7 version and Python 3.4 version to something else (ie pip27 and pip34). I also suggest you not only rename the pip but python interpreter as well (like python27 and python34.
Second since you have two Python versions you will need to call from command line (if you do not already). It might be that you have installed beautifulsoup correctly but you are using the wrong Python interpreter.
To run Python from command line use:
pythonXX file.py
I used pythonXX because I assume you have renamed each interpreter to distinguish the two versions.

Related

Python Libraries installing to Python 2.7 by default

I recently shifted to a Macbook. I installed Python 3.8 and installed Pyperclip using pip install pyperclip in the terminal, which was successful. When I tried to import it in IDLE shell, I get the error
ModuleNotFoundError: No module named 'pyperclip'.
On looking around I found that pyperclip has been installed in-
/Users/aamodpant/Library/Python/2.7/lib/python/site-packages/pyperclip
I did not install Python 2.7 but it must be required for macOS, so I don't want to delete it. How do I change the "default" version of Python for installing new libraries?
Use :
python3 -m pip install pyperclip
This will install the package for python3.
You can reinstall or upgrade pyperclip using sudo:
pip3 install -U pyperclip
Also, run your script using python3 instead of just python:
python3 script.py
The best is to use alias, as this answer describes changing default Python version in Mac can cause multiple issues.
alias python=python3
alias pip=pip3
The answer you are probably looking for is You should not change this.
Otherwise, you may like to set Python 3 as default, there are many ways to do it. But do it with caution as the guides suggest below.
The official documentation: Using Python on a Macintosh, some well described very good guides: The right and wrong way to set Python 3 as default on a Mac, Installing Python 3 on Mac OS X.

Python can't find already installed module robobrowser

I am trying to create a very simple Python 3.6 script. Using MacOS.
For this script I had to install robobrowser, which I installed with easy_install robobrowser. After that I try to import it with the following statements:
import re
from robobrowser import RoboBrowser
However, terminal prompted me with the following (infamous) error:
Traceback (most recent call last):
File "signup.py", line 2, in <module>
from robobrowser import RoboBrowser
ModuleNotFoundError: No module named 'robobrowser'
I have installed Python 3.6. However, in my /Library/Python I only have 2 folders: 2.6 and 2.7. In /Library/Python/site-packages there is a folder named robobrowser-0.5.3-py2.7.egg. Might it have something to do with this?
Sorry for asking a most likely easy question. I can however not seem to figure it out.
Thanks for reading this,
Thijmen.
Probably you are using easy_install for Python2. The best solution is to install pip for Python3 and run pip install robobrowser
This must be because you have multiple versions of Python installed, and when you installed robobrowser, it got installed to a different python version than the one you are trying to use. If you want to use it with Python 3, make your $PATH variable use the Python3's Scripts path instead of the one from Python2. Or a simpler way is to install pip3 on your computer and do sudo pip3 install robobrowser
For reference: changing python path on mac?
You can run the which python, which python3 and which easy_install commands in your terminal to find which versions of python and easy_install are being used by default.

How to install BeautifulSoup4 for Python 2.7 while also having Python 3.4 installed?

I have both Python 2.7.11 and Python 3.4.1 installed on my Windows 8.1 system. I had installed BeautifulSoup4 with pip to run a code (not mine). However, pip automatically installed bs4 to Python 3.4.1. (I checked that it was installed in C://Python34/lib/site-packages/bs4)
I have use the command prompt, change directory to C:\Python27 (where Python 2.7 is installed), and pip install bs4 from that directory, but it didn't work. I had copied the bs4 folder from Python 3.4, but it didn't work either. It only gave another Import Error: No module named html.entities.
How can I install bs4 on Python 2.7? Thanks in advance.
I guess you can run pip2 install bs4 and pip3 install bs4 to install them separately on different python versions

A pip module is available to python3.4 but getting an error when importing to python3.5

Ok, the general problem is that I can install a module say:
pip3 install xlsxwriter
and I can call it using python3.4
python3.4 >>> import xlsxwriter
python3.4 >>> success!
however when I run it in python3.5 I get an error
python3.5 >>> import xlsxwriter
python3.5 >>> ImportError: no module named 'xlsxwriter'
I'm using Ubuntu Server 15.10 as my test box.
Any help would be greatly appreciated
The problem you have is that you have two python different python versions (3.4 and 3.5) that look for modules in two separate places. What this means is that python 3.4 will look for modules in a folder in the python 3.5 directory while python 3.5 will look for modules in a folder in the python 3.4 directory. To fix the issue that you are having, you must install the module in the python 3.5 directory. To do this go your command prompt and type:
cd C:\Python35\Scripts # this is where I have my python 3.5 folder change the destination to where you have python 3.5 installed
pip install xlsxwriter
This should fix the issue. Hope it works!

Receiving 'ImportError: cannot import name etree' when using lxml in Python on Mac

I'm having difficulty properly installing lxml for Python on Mac. I have followed the instructions here, which after installation indicates that the installation is successful (however, there are some warnings. The full log of the install and warnings can be found here)
After running the install, I am trying to run Test.py in the lxml install directory to ensure that it's working correctly. I am immediately prompted with the error:
ImportError" cannot import name etree.
This error results from the line from lxml import etree.
I can't seem to figure out why it's failing here after a seemingly successful install. Please forgive my ignorance, as I don't typically program in Python and certainly never on a MAC (forced to do so at the moment).
For reference:
Python Version: 2.7.2
Mac OS X 10.8.2
Thanks in advance for all the help.
Sounds like you have another lxml in your path. Make sure you are referencing the right one, it should look something like this:
>>> import lxml
>>> lxml
<module 'lxml' from '/path/to/lib/python2.7/site-packages/lxml/__init__.pyc'>
I had the same problem as you.
My problem was that I named one of my Python files lxml.py.
You should always check if your Python file's name conflicts with a module.
Microsoft Server
Spyder IDE
Python 3.7 and Python 3.9
Documenting this as I couldn't find the solution ANYWHERE on Google/StackOverflow etc.
Regarding SharePlum, SharePoint Integration, issues with
import from lxml import etree
ImportError: cannot import name etree
I had the same issue while trying to use SharePlum with Spyder IDE.
I tried installing, uninstalling, and installing versions of the below with no success
pip install shareplum
pip install lxml
I had Python 3.9 installed and Spyder IDE (which I didn't know, also installs it's own version of Python 3.7 - as at 13 May 2021 Spyder version) .. [even though the Spyder IDE is set up to search all python package inventories (e.g. ../Python39/Lib/Site-Packages) for packages] there seems to be a Spyder issue with Python pulling packages from other paths.
Solution, I have uninstalled all Python and Spyder IDE installations.
I then ONLY installed Spyder IDE (which nests a python install within it's PATH. I then went into the PATH ".../Spyder/Python" where you can find all the familiar Python.exe and other familiar Python files. I had to use this install of Python to install Pip, and then I could "pip install -r requirements.txt" - which installs etree lxml and other goodies such as SharePlum etc.
This seemed to do the trick!
I had the same problem using PyCharm IDE v2021.2 with Python 3.8
What worked for me was in PyCharm,
go to Preferences -> Python Interpreter,
uninstall the lxml package,
close & restart PyCharm,
install the lxml package
Dunno why it worked, but it did.

Categories

Resources