"No module named urllib2" after installed "urllib2" using pip in Visual Studio 2015 - beautifulsoup

I have installed "urllib2" using pip by interface in Visual Studio 2015, but the debugger gets the error in line "import urllib2".
I have solved by now using:
with urllib.request.urlopen(url) as response:
html_doc = response.read()
gp = BeautifulSoup(html_doc, 'html.parser')
My job is just to parse an html page. Should those two lines be fine anyway? (I'm using BeautifulSoup4 with Python 3.4.3)

As you discovered, urllib2 has been split up into urllib.request and urllib.error in Python 3.
You can change your import to:
from urllib.request import urlopen
Your code looks fine to me.

Related

How do you run BeautifulSoup

I do a course on coursera, Python for everybody. I am stuck on the 3rd course, week 4.
My problem is that when I try to run Beautifulsoup, it has a traceback. I am uploading 2 pics, one my code and one my traceback. I tried downloading it from the web, the tried from the course folder, and then tried it from the command prompt, using python -m pip install beautifulsoup. None of there are working. I really need to know how to run BeautifulSoup because i have to submit my code soon. So if you can explain how to run BeautifulSoup I would appreciate it. (Also would appreciate if you explained the traceback!)
Thanks!
traceback
my code
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
print(soup.find_all("span"))
#tags = soup('a')
#for tag in tags:
#print('TAG:', tag)
#print('URL:', tag.get('href', None))
#print('Contents:', tag.contents[0])
#print('Attrs:', tag.attrs)
You're having a circular imports issue. I think it's because you're importing bs4 but you also have a folder bs4 next to your Python file. Try removing/renaming that folder.
The problem is not with the code snippet but your installation of Beautiful soup. Try reinstalling beautiful soup with
pip3 install —ignore-installed beautifulsoup4
as suggested here

ModuleNotFoundError: No module named 'pandas.io' for json_normalize

Please read carefully. In my Python script I have the following:
import json
import pandas
from pandas.io.json import json_normalize
and it returns the following error:
from pandas.io.json import json_normalize ModuleNotFoundError: No
module named 'pandas.io'; 'pandas' is not a package
My steps:
I have uninstalled and installed Pandas
I have upgraded pip and pandas
I have installed io (pip install -U pandas.io)
I have installed data_reader and replaced the pandas.io.json part with that: from pandas_datareader import json_normalize
I have tried every solution I saw on stackoverflow and github and nothing worked. The only one I have not tried is installing Anaconda but it should work with what I tried before. Do you think it is a Windows setting things I must change?
PS: My Python version is 3.7.4
Try:
Go to ...\Lib\site-packages\pytrends on your local disk and open file request.py
Change
from pandas.io.json._normalize import nested_to_record
to
from pandas.io.json.normalize import nested_to_record
I had the same error, but it helped me.
also change
from pandas.io.json.normalize
to
from pandas.io.json._normalize
The cause of the problem was the fact that the python file had the name pandas. The filename was pandas.py. After renaming it, the code worked normally without errors.
i had same problem and i solve it b uninstalling extra python versions install on my windows.now i have only one python installed by anaconda,and everything is working perfectly

Failed to parse service&apos

Visual Studio 2017 Preview : Python workload : Python 3.6 : Anaconda3
I have:
import cx_Oracle as ax
import numpy as nm
import pandas as pd
All of these packages have been installed in Anaconda3/Lib/site-packages
When using Debug (F5) with a breakpoint on import numpy as nm - I step over that statement and all is well. However, when I step over import pandas as pd an error message pops up:
The debug adapter exited unexpectedly.
When I step into this statement the message is
Frame not in module - The current stack frame was not found in a loaded module.
When I view the disassembly, the message is No disassembly available.
The activity log error is:
Failed to parse service's registered package GUID ({BEB01DDF-9D2B-435B-A9E7-76557E2B6B52) for service {094e1961-17bf-4038-9bde-e7e11825c67b} as a GUID.
This issue also occurs on the import matplotlib statement.
I have looked on the internet for an answer to this issue but cannot find one. I am very new to Visual Studio and to Python. Any assistance would be greatly appreciated.
Thanks

pyttsx: No module named 'engine'

I'm trying to install TTS package by using this. Everything was okay until I tried to execute the following command:
import pyttsx
I got back this error:
File "/usr/local/lib/python3.4/dist-packages/pyttsx/__init__.py", line 18, in module <br>
from engine import Engine<br>
ImportError: No module named 'engine'
Any help would be appreciated. Thank you!
Guys there is an updated package compatible with Python3 :
pyttsx3
Works offline with no delay in the sound produced.
Installation:
pip install pyttsx3
Visit https://pyttsx3.readthedocs.io for the full usage docs.
Thanks!
Combining the advice from Jacob Tsui and Jokhongir Mamarasulov worked for me. To summarize:
In site_packages/pyttsx/init.py, modify "from engine import Engine" to
from .engine import Engine
Then, in site_packages/pyttsx/engine.py,
Modify import driver to
from . import driver
Modify except Exception, e to
except Exception as e
And finally, in site_packages/pyttsx/driver.py modify except Exception, e to
except Exception as e
See the responses from the aforementioned authors for the rationale behind these changes.
I just had the same problem, try using pyttsx3 instead of pyttsx
First install pyttsx3
pip install pyttsx3
and change the
import pyttsx
for
import pyttsx3
After that you have to change engine import (if you're using it in your main .py file). Use engineio instead. Install it
pip install python-engineio
then change import engine for import engineio and change your variables.
Here's an example
import pyttsx3
# import engineio #engineio module is not needed.
engineio = pyttsx3.init()
voices = engineio.getProperty('voices')
engineio.setProperty('rate', 130) # Aquí puedes seleccionar la velocidad de la voz
engineio.setProperty('voice',voices[0].id)
def speak(text):
engineio.say(text)
engineio.runAndWait()
speak("What do you want me to say?")
while(1):
phrase = input("--> ")
if (phrase == "exit"):
exit(0)
speak(phrase)
print(voices)
Hope this helps someone
For Python3, please install the latest version via pip3 install pyttsx3 and call import pyttsx3.
I found out the solution. Library was created in python2 language and there are not a lot of differences between those 2 versions, but exclusively in this case that occurs.
Move to your DP folder and change in engine.py "except Exception as e" instead of "except Exception, e", line 67. Do the same in drive.py, line 105.
Because of files are secured try to execute, e. g.
sudo nano engine.py (or drive.py)
I guess I helped everyone with that kind of problem. :)
Modify site_packages/pyttsx/init.py "from engine import Engine" to
from .engine import Engine
Modify site_packages/pyttsx/engine.py "import driver" to
from . import driver
Reason: The import statement "from engine import Engine" tells python to import Engine module from directory engine. In our case engine is not a directory, it's a python file, engine.py. So we need to tell python to import this engine module from current directory (".").
I used this code after
pip install pywin32 pypiwin32 pyttsx3
and it worked perfectly for me
import os
import sys
import pyttsx3
engine = pyttsx3.init()
engine.say('hello world ')
engine.runAndWait()
I had the same issue.
First Try this command:
pip install pyttsx3
and then don't use
import pyttsx
use this
import pyttsx3
It will work.
pyttsx: No module named 'engine'
File "/usr/local/lib/python3.4/dist-packages/pyttsx/__init__.py", line 18, in module <br>
from engine import Engine<br>
ImportError: No module named 'engine'
If the above one is your error then try install pyttsx3 instead of
pyttsx.
Before installing check your python version, then download the version which is compatible to your python version.
Refer this link to get the previous versions of pyttsx3
REASON:
The reason we get the above error is because of the pyttsx3 version
which is not supported by your python version. Even if you get the error then
FOR pyttsx
Modify the init.py file located in
C:\Users\YOUR USER\AppData\Local\Programs\Python\Python38-32\Lib\site_packages\pyttsx\init.py
Change
from engine import Engine
to
from .engine import Engine
pyttsx
Modify the engine.py file located at C:\Users\YOUR USER\AppData\Local\Programs\Python\Python38-32\Lib\site_packages\pyttsx\engine.py
Change
import driver
to
from . import driver
These are the two main solutions for the above error

Py2exe doesn't find bs4

In my original code, I have the line:
from bs4 import BeautifulSoup
When I use py2exe, it builds fine but further up in the output it says:
The following modules appear to be missing
['_scproxy', 'bs4']
I specifically put bs4 in the py2exe options:
"includes": ["bs4.BeautifulSoup"]
Is that how I should be referencing BeautifulSoup in the includes statement?
The fella over here didn't know how to do it either: 3rd Party Libraries and Py2exe
Do I need to use packages instead of includes or something? All regular libraries and some other like mechanize import fine, but I can't get BeautifulSoup to work fine. Any advice is appreciated.
EDIT: I solved part of this by uninstall BeautifulSoup and re-installing with --always-unzip option:
easy_install --always-unzip beautifulsoup4
However, it added 9 new missing modules. One being '_scproxy'.
First thing to check is that you have your setup.py in the same directory as your module and your are running it from that directory.
If that doesn't work your should add your module to your path in setup.py:
module_path = r'path\to\your\BeautifulSoup\module'
if module_path not in sys.path:
sys.path.append(modules_path)