python 3 import not working - python-3.x

I am new to Python 3 and am rewriting a Python 2 program. I have the following file system:
|-00_programs / test.py
|-01_classes / class_scrapper.py
I want to import the class scrapper from the file class_scrapper:
Here is class_scrapper.py:
# -*- coding: utf-8 -*-
from urllib.request import urlopen
from bs4 import BeautifulSoup
class scrapper:
def get_html(self, url):
html = False
headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' }
try:
html = urlopen(url, '', headers).read()
except Exception as e:
print ("Error getting html :" + str(e))
return html
Here is test.py:
# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, "./../01_classes/class_scrapper.py")
from class_scrapper import scrapper
o_scrapper = scrapper()
While executing I got:
Traceback (most recent call last):
File "/src/00_programs/tets.py", line 6, in <module>
from class_scrapper import scrapper
ImportError: No module named 'class_scrapper'
What should be changed on the import command to make that work?
Thanks,
Romain.

If the interpreter is saying module doesn't exist, it means you must have spelt it wrong when importing, or the module is either not in your program's directory or the python directory which has all of the other main modules.

Related

Is there any modification to execute curl command in python 3.x?

I have a runnable code in python 2.x version for following shell command :
curl -XPOST localhost:5055/parse -d '{"q":"tell me about more info on covid", "projects": "ChatBot"}'
script for python 2.x is as below :
import shutil
import sys
import urllib2
import subprocess
import json
import subprocess, sys
import os, time
import string
import os.path
import os, glob
from datetime import datetime
inputArg="tell me about more info on covid"
data = '{"q":"' + inputArg + '", "projects":"ChatBot"}'
url = 'http://localhost:5055/parse'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
But when same script is executed in Python 3.x version:
it gives error as follows :
Traceback (most recent call last):
File "main.py", line 3, in <module>
import urllib2
ModuleNotFoundError : No module named 'urllib2'
Please let me know what changes shall be done in code so that it will execute in Python 3.x version.
What are the pre-requisites needed in python 3.x version ?
I found the solution to this issue without using urllib.request:
$ cat test6.py
import requests
import json
inputArg="tell me about more info on covid"
data = {"q":inputArg , "projects":"ChatBot"}
params = json.dumps(data)
url = 'http://localhost:5055/parse'
r = requests.post(url, data=params)
print(r.text)
print(r.status_code, r.reason)

python mechanize import failing with python 2.x

Mechanize is not working with python 2.x while using "Browser" in mechanize failing.
import re
from mechanize import Browser
br = mechanize.Browser()
br.open("https://en.wikipedia.org/wiki/Whey_protein")
response1 = br.follow_link(text_regex=r"protein\s*shop", nr=1)
assert br.viewing_html()
print(br.title())
print(response1.geturl())
print(response1.info())
print(response1.read()) )
Traceback (most recent call last):
File "C:\Users\test\Mechnize_bt.py", line 2, in
from mechanize import Browser
ImportError: cannot import name Browser

looping over images in a directory

I have images in the same directory with a python file, i am trying to loop over the images and convert them into base64 but am getting this error.
Am using Ubuntu 14.0.4
Traceback (most recent call last):
File "convert_to_base64.py", line 33, in <module>
print(main())
File "convert_to_base64.py", line 26, in main
convert_to_base64()
File "convert_to_base64.py", line 19, in convert_to_base64
with open("*.jpg", "rb") as f:
IOError: [Errno 2] No such file or directory: '*.jpg'
Here is my python code
# -*- coding: utf-8 -*-
import os
import sys
import xlrd
import base64
import urllib
from datetime import datetime
reload(sys) # to re-enable sys.setdefaultencoding()
sys.setdefaultencoding('utf-8')
def convert_to_base64():
"""
Read all jpg images in a folder,
and print them in base64
"""
with open("*.jpg", "rb") as f:
data = base64.b64decode(f.read())
print data
def main():
start_datetime = datetime.now()
convert_to_base64()
end_datetime = datetime.now()
print '------------------------------------------------------'
print 'Script started : {}'.format(start_datetime)
print 'Script finished: {}'.format(end_datetime)
if __name__ == '__main__':
print(main())
print('Done')
someone help me figure out what am doing wrong.
Thanks
This is how I looped for images in a directory:
import os
pictures = []
for file in os.listdir("pictures"):
if file[-3:].lower() in ["png"]:
pictures.append(file)
Please refer to Python documentation https://docs.python.org/2/tutorial/inputoutput.html for more info on open() function:
open() returns a file object, and is most commonly used with two arguments: open(filename, mode).

AttributeError: module 'bs4' has no attribute 'BeautifulSoup'

Traceback (most recent call last):
File "bs4.py", line 1, in <module>
import bs4
File "/home/mhadi/Desktop/bs4test/bs4.py", line 5, in <module>
soup = bs4.BeautifulSoup(site,'lxml')
AttributeError: module 'bs4' has no attribute 'BeautifulSoup'
The code:
import bs4
import urllib.request
site = urllib.request.urlopen('http://127.0.0.1:8000').read()
soup = bs4.BeautifulSoup(site,'lxml')
#for i in site:
# print(site[i])
print(soup)
The problem is that your filename is bs4.py. Now if you write an import statement, Python will first look for local files with that name. So it assumes that your import bs4 refers to your own file. Your file will thus aim to import itself, but it obviously does not contain the desired module.
A quick fix is renaming the file. For instance into bs4tests.py. Then you can use import bs4.
Alternatively, you can for instance try to remove the local path, like:
import sys # import sys package
old_path = sys.path[:] # make a copy of the old paths
sys.path.pop(0) # remove the first one (usually the local)
import bs4 # import the package
sys.path = old_path # restore the import path

ImportError: No module named url

when i run the the given code in prepare_dataset.py whose code is below as
from urllib.request
import urlopen
import json as simplejson
def main():
# Download and prepare datasets
list_generator = video_list_generator.Generator("playlists.json", youtube_api_client.Client("AIzaSyD_UC-FpXbJeWzPfscLz9RhqSjKwj33q6A"))
video_list = list_generator.get_video_list("piano")
downloader = youtube_video_downloader.Downloader()
downloader.download_from_list(video_list)
if __name__ == '__main__':
main()
as python prepare_dataset.py in command-line then i get these errors
Traceback (most recent call last):
File "prepare_dataset.py", line 1, in <module>
from urllib.request import urlopen
ImportError: No module named request
How can i get to run the above file any idea guys?

Resources