Python: ImportError: cannot import name 'BeautifulSoup' - python

I have always used R and now trying to switch to Python.
I'm using Pycharm and found this error when running the following code:
import pandas as pd
from bs4 import BeautifulSoup
example1 = BeautifulSoup(train["review"][0],"lxml")
print (example1.get_text())
When I run it I have:
ImportError: cannot import name 'BeautifulSoup'
But I don't have any problem using the console. The rest of the code works fine both with the Run command/terminal and console.
Thank you for your help

Oh,I think you should check your file name or folder name.If there is a name that is already used in bs4 module,you will got ImportError.
Hope this helps.

Related

H2OModelSelectionEstimator deprecated?

Is the H2OModelSelectionEstimator deprecated? When I run the code
from h2o.estimators import H2OModelSelectionEstimator
I get the message: ImportError: cannot import name 'H2OModelSelectionEstimator' from 'h2o.estimators'
Try this instead:
from h2o.estimators.model_selection import H2OModelSelectionEstimator
If you can't import it, then you probably don't have the latest version of H2O, so you should download it. ModelSelection was just released in 3.36.0.1.

Python caldav Attribute Error

I just installed caldav 0.5.0 using pip on Windows. I tried to use this code from the documentation:
from datetime import datetime
import caldav
from caldav.elements import dav, cdav
# Caldav url
url = "https://user:pass#hostname/caldav.php/"
client = caldav.DAVClient(url)
But I get this error:
AttributeError: module 'caldav' has no attribute 'DAVClient'
Does someone know what causes this issue?
It is because your file is named calendar.py, which causes some kind of collision somewhere. Renaming your file to something else will do the trick (it did for me).

Python : no JSON object could be decoded

I am trying to run this app:
https://github.com/bmjr/guhTrends
I have python 2.7.x running the following script at command line. I reckon it was written using python3.x. What is deprecated in the code below?
import urllib
import json
import matplotlib.pyplot as plt
dates = urllib.request.urlopen('http://charts.spotify.com/api/tracks/most_streamed/global/weekly/')
dataDates = json.loads(dates.read().decode())
the error:
Traceback (most recent call last):
File "DataMining.py", line 6, in <module>
dates = urllib.request.urlopen('http://charts.spotify.com/api/tracks/most_streamed/global/weekly/')
AttributeError: 'module' object has no attribute 'request'
That script won't work under python2 as urllib of python2 has no request module.
Use urllib2.urlopen instead of urllib.request if you want start running that script under python2 .
To get python script work on bith (python2 and python3) use six module which is Python 2 and 3 Compatibility Library.
from six.moves import urllib
import json
import matplotlib.pyplot as plt
dates = urllib.request.urlopen('http://charts.spotify.com/api/tracks/most_streamed/global/weekly/')
dataDates = json.loads(dates.read().decode())
You're requesting a resource that is not currently available (I'm seeing a 504). Since this could potentially happen any time you request a remote service, always check the status code on the response; it's not that your code is necessarily wrong, in this case it's that you're assuming the response is valid JSON without checking whether the request was successful.
Check the urllib documentation to see how to do this (or, preferably, follow the recommendation at the top of that page and use the requests package instead).

Error in importing a python file

I've written a python file and am trying to import it but it's not recognized.
The file was saved as gentleboost_c_class.c in C:\User\apps\My documents.
I tried to import it like this:
import gentleboost_c_class as gbc
But I get this error:
NameError: name 'gentleboost_c_class' is not defined
gentleboost_c_class.py begins like this:
from sklearn.externals.six.moves import zip
import numpy as np
import statsmodels.api as sm
class GentleBoostC:
.....
It compiles fine.
Both files are in the same folder.
What am I doing wrong?
You're getting a NameError, not an ImportError.
So it seems to me that you import your module as gbc, but later try to refer to it as gentleboost_c_class.
If you import the module with
import gentleboost_c_class as gbc
that means it will be available under the global name gbc, but not as gentleboost_c_class.

Python script for Blender failed

In Blender 2.62 I was using this script to display a point:
import bpy
from bpy.props import FloatVectorProperty, IntProperty, FloatProperty
from add_utils import AddObjectHelper, add_object_data
data0=[]
data0.append((float(69.3456), float(36.4562), float(26.8232)))
me0 = bpy.data.meshes.new( name = "point cloud0")
me0.from_pydata( data0, [], [] )
me0.update()
add_object_data(bpy.context, me0, [])
After having updated to Blender 2.67a the execution returns a failure and the following error is reported in the console window:
ImportError: No module named 'add_utils'
Do you have any clue why this should not work anymore?
Thank you :)
Add the missing bpy_extras import at the start of the script
import bpy
import bpy_extras
from bpy.props import FloatVectorProperty, IntProperty, FloatProperty
from bpy_extras import object_utils.object_data_add
from bpy_extras import AddObjectHelper
The API for add_object_data appears to have changed to object_data_add so you'll need to change that in the script too.
object_data_add(bpy.context, me0, [])

Categories

Resources