ModuleNotFoundError: No module named 'keras.layers.preprocessing' - python-3.x

After writing this -
VERIFICATION_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'builders', 'model_builder_tf2_test.py')
!python {VERIFICATION_SCRIPT}
I am getting this error-
from keras.layers.preprocessing import image_preprocessing as image_ops
ModuleNotFoundError: No module named 'keras.layers.preprocessing'

This error is because there is no api named keras.layers.preprocessing. The correct name of this api is tensorflow.keras.preprocessing and you can import image from this api not image_preprocessing
Try using:
from tensorflow.keras.preprocessing import image as image_ops
in place of (incorrect way)
from keras.layers.preprocessing import image_preprocessing as image_ops
Please check this link for more details.

Related

Django unable to get models module

I am learning Django and not sure what is causing the error 'ModuleNotFoundError: No module named 'models''
even though the files are in the same folder
files are available here
File "/Users/mayanksharma/Library/Mobile Documents/com~apple~CloudDocs/for Stackoverflow/app-7-django/blogs/urls.py", line 1, in <module>
from . import views
File "/Users/mayanksharma/Library/Mobile Documents/com~apple~CloudDocs/for Stackoverflow/app-7-django/blogs/views.py", line 2, in <module>
from models import Post
ModuleNotFoundError: No module named 'models'
Do you mean by?
from .models import Post
Replace from . import views with:
import sys
sys.append('..')
import views
This was something to do with python reading path instead of from .models import Post or from . import views I gave from app.models import Post and that made it work

Error Launching Blob Trigger function in Azure Functions expected str, bytes or os.PathLike object, not PosixPath

My problem is: I try to execute a fresh uploaded python function in an Azure Function App service and launch it (no matter if I use blob trigger or http trigger) I allways get the same error:
Exception while executing function: Functions.TestBlobTrigger Result: Failure
Exception: TypeError: expected str, bytes or os.PathLike object, not PosixPath
Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 284, in _handle__function_load_request
func = loader.load_function(
File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
return func(*args, **kwargs)
File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 53, in load_function
register_function_dir(dir_path.parent)
File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 26, in register_function_dir
_submodule_dirs.append(fspath(path))
Why is this happening: when the function is successfully deployed I upload a file in a blob in order to trigger the function but I get allways the same error, caused by the pathlib library (https://pypi.org/project/pathlib/). I have written a very easy function that works in my local vscode and it just prints a message.
import logging
import configparser
import azure.functions as func
from azure.storage.blob import BlockBlobService
import os
import datetime
import io
import json
import calendar
import aanalytics2 as api2
import time
import pandas as pd
import csv
from io import StringIO
def main(myblob: func.InputStream):
logging.info("BLob Trigger function Launched ");
blob_bytes = myblob.read();
blobmessage=blob_bytes.decode()
func1 = PythonAPP.callMain();
func1.main(blobmessage);
The Pythonapp class is:
class PythonAPP:
def __init__(self):
logging.info('START extractor. ');
self.parameter="product";
def main(self,message1):
var1="--";
try:
var1="---";
logging.info('END: ->paramet '+str(message1));
except Exception as inst:
logging.error("Error PythonAPP.main : " + str(inst));
return var1;
My requirements.txt file is:
azure-storage-blob== 0.37.1
azure-functions-durable
azure-functions
pandas
xlrd
pysftp
openpyxl
configparser
PyJWT==1.7.1
pathlib
dicttoxml
requests
aanalytics2
I've created this simple function in order to check if I can upload the simpliest example in Azure Functions, is there any dependencies that am I forgetting?
Checking the status of the functions I found this:
------------UPDATE1--------------------
The function is failing because the pathlib import, this is because in the requirements of the function it downloads this library and fails with AZ functions. Please see the requirements.txt file in the following link: https://github.com/pitchmuc/adobe_analytics_api_2.0/blob/master/requirements.txt
Can I exlude it somehow?
Well I can't provide an answer for that, I made a walkarround. In this case I created a copy of the library in a github repository. In this copy I erased the references to the pathlib in the requrements.txt and setup.py because this libary causes the failure in the AZ function APPS. By the way in the requirements file of the proyect make a reference to the project, so please mind the requiremnts file that I wrote above and change aanalytics2 reference to:
git+git://github.com/theURLtotherepository.git#master#egg=theproyectname
LINKS.
I've checked a lot of examples in google but none of them helped me:
Azure function failing after successfull deployment with OSError: [Errno 107]
https://github.com/Azure/azure-functions-host/issues/6835
https://learn.microsoft.com/en-us/answers/questions/39865/azure-functions-python-httptrigger-with-multiple-s.html
https://learn.microsoft.com/en-us/answers/questions/147627/azure-functions-modulenotfounderror-for-python-scr.html
Missing Dependencies on Python Azure Function --> no bundler flag or –build remote
https://github.com/OpenMined/PySyft/issues/3400
This is a bug in the azure codebase itself; specifically
within:
azure-functions-python-worker/azure_functions_worker/dispatcher.py
the problematic code within dispatcher looks to be setting up the exposed functions with the metadata parameters found within function.json
you will not encounter the issue if you're not using pathlib within your function app / web app code
if pathlib is available the issue manifests
rather than the simple os.path strings pathlib.Path objects are exposed - deeper down in the codebase there looks to be a conditional import and use of pathlib
to resolve simply remove the pathlib from your requirements.txt and redeploy
you'll need to refactor any of your own code that used pathlib - use equivalent methods in the os module
there looks to have been a ticket opened to resolve this around the time of the OP post - but it is notresolved in the current release

Import of an attribute of a python module fails

I have the following directory structure:
http://localhost:8888/notebooks/translation.ipynb
http://localhost:8888/edit/Fill_temp/prepare_test_data.py
In
prepare_test_data.py
I have a function:
def to_cap (EXP_FILE, SAMPLES_FILE: str= EXP_FILE + '.cap', cap_rate=0, by_token=False):
In the notebook
translation.ipynb
I do these imports:
%load_ext autoreload
%autoreload 2
import Fill_temp
import Fill_temp.prepare_test_data
then I run
Fill_temp.prepare_test_data.to_cap("en12.json.pres", "en12.cap.0")
and I get
AttributeError: module 'Fill_temp.prepare_test_data' has no attribute 'to_cap'
How come?
I explicitly imported both the Fill_temp package and the prepare_test_data module.
Do I need to import even the lowest level functions that are defined in the module?
EDIT:
I tried to import the low level function explicitly:
%load_ext autoreload
%autoreload 2
import Fill_temp
import Fill_temp.prepare_test_data
import Fill_temp.prepare_test_data.to_cap
but I get:
ModuleNotFoundError: No module named
'Fill_temp.prepare_test_data.to_cap';
'Fill_temp.prepare_test_data' is not a package
So what shall I do?
This is a bit bizarre. Basically, it turned out that there was a syntax error in that low level function.
But instead of saying it jupyter was saying that it doesn't see that function. Which is a really counter-intuitive error message.

Learning Python extracting data from a website

I am trying to write a script to get data from an internal website that exports to Excel, that data gets broken into smaller pieces and gets emailed to technicians. (metric data) I am trying to get into the website using robobrowser but I keep getting this:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Aging.py
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\Aging.py", line 3, in
from robobrowser import RoboBrowser
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\robobrowser-0.5.3-py3.6.egg\robobrowser__init__.py", line 3, in
from .browser import RoboBrowser
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\robobrowser-0.5.3-py3.6.egg\robobrowser\browser.py", line 7, in
from bs4 import BeautifulSoup
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4__init__.py", line 30, in
from .builder import builder_registry, ParserRejectedMarkup
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\builder__init__.py", line 308, in
from . import _htmlparser
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\builder_htmlparser.py", line 7, in
from html.parser import (
ImportError: cannot import name 'HTMLParseError'
Here is the code:
import webbrowser
import re
from robobrowser import RoboBrowser
#Set BR module
br = RoboBrowser()
#open a website
br.open("https://www.whatever.com")
form = br.get_form()
form ['username'] = "username"
form ['password'] = "password"
br.submit_form(form)
Any help would be appreciated.
You should try reinstalling RoboBrowser and BeautifulSoup. What's happening is that when you import robobrowser, RoboBrowser then tries to import BeautifulSoup (a python module) which then tries to import _htmlparser (a python module that is part of the BeautifulSoup package), but it can't find that file and the load fails.
This is most likely caused by a missing or corrupted file (or maybe an out of date version). If you reinstall BeautifulSoup (and probably robobrowser to be safe) it should fix the problem.

ImportError: cannot import name Multipoint

I'm trying to import Multipoint from shapely.geometry like so:
from shapely.geometry import Multipoint
state = Multipoint(((36.977397, -102.985840),(37.004892, -109.050293)(31.805382, -109.006348),(31.861385, -103.095703))).convex_hull
point.within(polygon)
But I get the following error:
ImportError: cannot import name Multipoint
Anyone know what's causing this error?
Try
from shapely.geometry import MultiPoint
Make sure you are using a capital 'P' in the word MultiPoint. Docs

Resources