python 3.3 and beautifulsoup4-4.3.2 - beautifulsoup

from bs4 import BeautifulSoup
import urllib
import socket
searchurl = "http://suchen.mobile.de/auto/search.html?scopeId=C&isSearchRequest=true&sortOption.sortBy=price.consumerGrossEuro"
f = urllib.request.urlopen(searchurl, None, None)
html = f.read()
soup = BeautifulSoup(html)
for link in soup.find_all("div","listEntry "):
print(link)
Traceback (most recent call last):
File "C:\Users\taha\Documents\worksapcetoon\Parser\com\test__init__.py", line 6, in
f = urllib.request.urlopen(searchurl, None, None)
AttributeError: 'module' object has no attribute 'request'

For urllib.request docs look here:
http://docs.python.org/py3k/library/urllib.request.html?highlight=urllib#urllib.request.urlopen
use import urllib.request instead of import urllib

Replace:
import urllib
with:
import urllib.request
Python by default doesn't include submodules into packages' namespace - it must be done by hand by programmer. Some packages are written to do it - for example package 'os' puts 'path' in own namespace so 'import os' is enough to use both 'os' and 'os.path' functions - but in general all module imports need to be explicit.

Related

ModuleNotFoundError for .vision and .utils , download_url, VisionDataset

I am trying to create a custom dataset in google colab, but imports give me errors.
from PIL import Image
from six.moves import zip
import os
from .vision import VisionDataset ------------------------(1)
from .utils import download_url, check_integrity --------------(2)
class datasetName(VisionDataset):
...
(1) error :
ModuleNotFoundError: No module named 'main.vision'; 'main' is
not a package
(2) error :
ModuleNotFoundError: No module named 'main.utils'; 'main' is
not a package
I have tried to add from torchvision import utils but it does not solve the error.
If I change to from torch.utils import download_url, check_integrity
then the error becomes:
ImportError: cannot import name 'download_url'
Please try the following import lines.
from torchvision.datasets.vision import VisionDataset
from torchvision.datasets.utils import download_url, check_integrity
Hope it helps!

py2exe setup.py not working

I have an small code that uses pandas and sqlalchemy and is declared in my main.py as:
import pandas as pd
from sqlalchemy import create_engine
this is my complete setup.py:
from distutils.core import setup
import py2exe
from glob import glob
data_files = [("Microsoft.VC90.CRT", glob(r'C:\Users\Flavio\Documents\Python_dll\*.*'))]
opts = {
"py2exe": {
"packages": ["pandas", "sqlalchemy"]
}
}
setup(
data_files=data_files,
options = opts,
console=['main.py']
)
And I'm using this command in terminal:
python setup.py py2exe
But when I run main.exe it's open the terminal start to execute code and suddenly close window.
when I run over terminal it's the error:
C:\Users\Flavio\Documents\python\python\untitled\dist>main.exe
Please add a valid tradefile date as yyyymmdd: 20150914
Traceback (most recent call last):
File "main.py", line 11, in <module>
File "C:\Users\Flavio\Anaconda3\lib\site-packages\sqlalchemy\engine\__init__.p
y", line 386, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Users\Flavio\Anaconda3\lib\site-packages\sqlalchemy\engine\strategies
.py", line 75, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Users\Flavio\Anaconda3\lib\site-packages\sqlalchemy\connectors\pyodbc
.py", line 51, in dbapi
return __import__('pyodbc')
ImportError: No module named 'pyodbc'
without know what your program does
I would try the following 1st
open a command window and run your .exe from there
The window will not close and any error messages (if any) will be displayed

win32com.client error

When using win32com, something puzzled my.
>>> import win32com
>>> w=win32com.client.Dispatch('Word.Application')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
w=win32com.client.Dispatch('Word.Application')
AttributeError: 'module' object has no attribute 'client'
what's wrong?
win32com.client is a module in the win32com package you need to import the actual module.
import win32com.client
w = win32com.client.Dispatch('Word.Application')

ImportError: cannot import name extensions

$ scrapy version: 0.14 $
$ file: settings.py $
EXTENSIONS = { 'myproject.extensions.MySQLManager': 500 }
$ file: pipeline.py $
# -- coding: utf-8 --
# Define your item pipelines here #
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: doc.scrapy.org/topics/item-pipeline.html
from scrapy.project import extensions
from urlparse import urlparse
import re
class MySQLStorePipeline(object):
def process_item(self, item, spider):
if self.is_allowed_domain(item['url'], spider) is True:
MySQLManager.cursor... #cannot load MySQLManager
ImportError: cannot import name extensions.
i can't find extensions class in /scrapy/project.py
I don't know what are you trying to import but as far as I understand, if you want to "import" an extension, you should add some lines to your settings.py file in your scrapy project.
Example:
EXTENSIONS = {
'scrapy.contrib.corestats.CoreStats': 500,
'scrapy.webservice.WebService': 500,
'scrapy.myextension.myextension': 500,
}
You can read more about Scrapy extensions here

Creating executable with Py2exe and matplotlib errors

I have been searching this forum and many others and cannot seem to get a good method of creating an executable. I have tried several different methods (py2exe, pyinstaller and cx_freeze) and all seem to give me some kind of error.
When i tried pyinstaller, I received the error that "no _imaging C module is installed". Everything I search says that it has to do with PIL, but my code is not using PIL.
When I tried py2exe, I keep receiving the following error:
File "Scout_Tool.py", line 18, in <module>
File "matplotlib\pyplot.pyc", line 95, in <module>
File "matplotlib\backends\__init__.pyc", line 25, in pylab_setup
ImportError: No module named backend_qt4agg
I am at a loss of what to do. My code contains the following imports:
import os
import csv
import wx
import time
import math
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.pyplot import figure,show
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from numpy.random import rand
from datetime import datetime
import wx.calendar as cal
import numpy as npy
from pylab import *
import numpy as np
import matplotlib
import adodbapi
import sqlparse
import pylab
import annote_new
import cPickle as pickle
Does anyone have any suggestions on how to do a build of the executable using py2exe? What i have tried...
from distutils.core import setup
import py2exe
import matplotlib
setup(
windows=[{'script': r'Scout_Tool.py'}],
data_files=matplotlib.get_py2exe_datafiles(),
options={
r'py2exe': {
r'includes': r'ElementConfig',
r'includes': r'ColorConv',
r'includes': r'Tkinter',
r'includes': r're',
r'includes': r'math',
r'includes': r'sys',
r'includes': r'matplotlib',
r'includes': r'mpl_toolkits',
r'includes': r'matplotlib.backends.backend_wx',
r'dll_excludes': [r'MSVCP90.dll'],
}
},
)
Thanks for any help!
I am not fully sure this will fix your problem, but you should start by correcting that faulty options dictionary entry. In python, when you define a dictionary with the same key over and over, you are only going to get the last value. A key can only exist once:
options={
r'py2exe': {
r'includes': r'ElementConfig',
...
r'includes': r'mpl_toolkits',
r'includes': r'matplotlib.backends.backend_wx',
...
}
}
print options
#{'py2exe': {'includes': 'matplotlib.backends.backend_wx'}}
I suspect the result of this usage is py2exe not really finding any of your intended includes. includes should be a list:
options={
'py2exe':{
'includes': [
'ElementConfig',
'ColorConv',
'Tkinter',
're',
'math',
'sys',
'matplotlib',
'mpl_toolkits',
'matplotlib.backends.backend_wx'
],
'dll_excludes': ['MSVCP90.dll'],
}
},
If after this, it still complains about the backend missing, you can add another explicit entry:
'includes': [
...
'matplotlib.backends.backend_qt4agg'
],