AttributeError: module 'keras.api._v2.keras' has no attribute 'utils' - deep-learning

OS: MacOS Monterey (12.4)
Chip: Apple Silicon M1
Env: Conda 4.12.0
Python: 3.9.12
TensorFlow version: 2.9.0, installed through pip install tensorflow-macos
Error message:
Traceback (most recent call last):
File "/Users/okihita/PycharmProjects/TFFlowerClassification/main.py", line 20, in <module>
training_dataset = tf.keras.utils.image_dataset_from_directory(
AttributeError: module 'keras.api._v2.keras' has no attribute 'utils'
Here's my main.py code:
import pathlib
import tensorflow as tf
from PIL import Image
data_dir = pathlib.Path('aksara-batak/toba')
aksara = list(data_dir.glob('a/*'))
img_height = 30
img_width = 30
training_dataset = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width)
)
Did I import the wrong version of TensorFlow?

Related

ImportError: cannot import name '_rfc_1738_quote'

I am trying to connect Jupyter Notebook to Snowflake but getting the below issue
`
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-12-3b4f5d455df9> in <module>
1 from sqlalchemy import create_engine
----> 2 from snowflake.sqlalchemy import URL
3 import sqlalchemy as db
4 import pandas as pd
5
~\AppData\Local\Continuum\anaconda3\lib\site-packages\snowflake\sqlalchemy\__init__.py in <module>
56 VARIANT,
57 )
---> 58 from .util import _url as URL
59 from .version import VERSION
60
~\AppData\Local\Continuum\anaconda3\lib\site-packages\snowflake\sqlalchemy\util.py in <module>
11 from urllib.parse import quote_plus
12
---> 13 from sqlalchemy.engine.url import _rfc_1738_quote
14
15
ImportError: cannot import name '_rfc_1738_quote'
`
And here: https://github.com/snowflakedb/snowflake-sqlalchemy/issues/350 it was supposed to upgrade to v1.4.3 version but getting the below error when trying to upgrade.
Used command: pip install snowflake-sqlalchemy==1.4.3
Error
`
ERROR: Could not find a version that satisfies the requirement snowflake-sqlalchemy==1.4.3 (from versions: 1.0.7, 1.0.8, 1.0.9, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5, 1.1.6, 1.1.9, 1.1.10, 1.1.11, 1.1.12, 1.1.13, 1.1.14, 1.1.15, 1.1.16, 1.1.17, 1.1.18, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.3.3, 1.3.4)
ERROR: No matching distribution found for snowflake-sqlalchemy==1.4.3
`
Can someone please help with the issue?
Tried to upgrade to the latest version which is not the latest.
The issue was resolved by upgrading python to 3.9 and then upgrading sqlalchemy to 1.4.3

Django 2.0 - Python3.6 - CentOS 7 - MySQL connections

i try to install my website on a webserver which is in CentOS 7 with Django 2.0.6 and Python 3.6.
I installed : mysql-connector = 2.1.6,
mysql-connector-python = 8.0.11,
MySQL-python = 1.2.5,
mysqlclient = 1.3.12,
PyMySQL = 0.8.1
And when i run this command :
python3.6 manage.py migrate
I have this error :
File "MYPYTHONPATH/python3.6/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 1.2.5
But in the base.py file :
try:
import MySQLdb as Database
except ImportError as err:
raise ImproperlyConfigured(
'Error loading MySQLdb module.\n'
'Did you install mysqlclient?'
) from err
from MySQLdb.constants import CLIENT, FIELD_TYPE # isort:skip
from MySQLdb.converters import conversions # isort:skip
# Some of these import MySQLdb, so import them after checking if it's installed.
from .client import DatabaseClient # isort:skip
from .creation import DatabaseCreation # isort:skip
from .features import DatabaseFeatures # isort:skip
from .introspection import DatabaseIntrospection # isort:skip
from .operations import DatabaseOperations # isort:skip
from .schema import DatabaseSchemaEditor # isort:skip
from .validation import DatabaseValidation # isort:skip
version = Database.version_info
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
I dont understand what can i do for resolving this kind of problem !
I hope someone can help me on =)
Okay, i solved it with the answer of this thread.
No module named MySQLdb
pip install PyMySQL
and then add this two lines to your Project/Project/init.py
import pymysql
pymysql.install_as_MySQLdb()
Works on WIN and python 3.3+

Import globally installed module

I am trying to execute my python script which imports globally installed modules through pip. (Module like nltk)
Below is my code.
interpreter.execfile("/home/roy/eclipse-workspace/geospatial-analysis/src/main/resources/wordnet_util.py");
PyObject someFunc = interpreter.get("get_similarity");
PyObject result = someFunc.__call__(new PyString("Apple pie."), new PyString("Banana pie."));
Double realResult = (Double) result.__tojava__(Double.class);
I have added the path for python modules like,
import sys
sys.path.append("/usr/local/lib/python2.7/dist-packages")
But I get the following error,
*sys-package-mgr*: can't create package cache dir, '/usr/local/lib/python2.7/dist-packages/cachedir/packages'
Exception in thread "main" Traceback (most recent call last):
File "/home/roy/eclipse-workspace/geospatial-analysis/src/main/resources/wordnet_util.py", line 6, in <module>
from nltk import word_tokenize, pos_tag
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 18, in <module>
from __future__ import print_function, absolute_import
ImportError: No module named __future__
Update:
I added sys.path.append("/usr/lib/python2.7") to the python file. It solved the above error. Now it can't find select module.
ImportError: No module named signal

nltk successfully installed, but now I can't import it

I just installed nltk and now it's not working, and I need assistance figuring out what's wrong.
I followed the install guide here [https://pythonprogramming.net/tokenizing-words-sentences-nltk-tutorial/][1]
Installed via pip
sudo pip install nltk
Then I launched python and downloaded all components
# python3
>>> import nltk
>>> nltk.download()
This worked, and it downloaded all the components(I chose all). After which I figured I was good to go. However, then I went to write my first script and it errored out on the 1st line which imported nltk.
Now when I open the python interpreter I get
# python3
>>> import nltk
"2016-03-02 04:34:03,630","regex.py","ERROR","There is a problem with your SQL query: SELECT regex, regex_id FROM regex WHERE type = 'date'"
Traceback (most recent call last):
File "/home/nick/booze-dev/regex.py", line 10, in <module>
cursor.execute(SQL) # Execute SQL
NameError: name 'cursor' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/nltk/__init__.py", line 128, in <module>
from nltk.chunk import *
File "/usr/local/lib/python3.4/dist-packages/nltk/chunk/__init__.py", line 157, in <module>
from nltk.chunk.api import ChunkParserI
File "/usr/local/lib/python3.4/dist-packages/nltk/chunk/api.py", line 15, in <module>
from nltk.chunk.util import ChunkScore
File "/usr/local/lib/python3.4/dist-packages/nltk/chunk/util.py", line 13, in <module>
from nltk.tag.mapping import map_tag
File "/usr/local/lib/python3.4/dist-packages/nltk/tag/__init__.py", line 63, in <module>
from nltk.tag.sequential import (SequentialBackoffTagger, ContextTagger,
File "/usr/local/lib/python3.4/dist-packages/nltk/tag/sequential.py", line 25, in <module>
from nltk.classify import NaiveBayesClassifier
File "/usr/local/lib/python3.4/dist-packages/nltk/classify/__init__.py", line 98, in <module>
from nltk.classify.textcat import TextCat
File "/usr/local/lib/python3.4/dist-packages/nltk/classify/textcat.py", line 47, in <module>
import regex as re
File "/home/nick/booze-dev/regex.py", line 14, in <module>
sys.exit("Aborting script execution")
NameError: name 'sys' is not defined
I didn't get that error the 1st time I ran the import and executed nltk.download(), so I'm not understanding why I'm getting it now.
I've tried executing python as root and my normal user, and get the same error.
If I check my sys.path
>>> import sys
>>> sys.path
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
I can find nltk under /usr/local/lib/python3.4/dist-packages:
# ls /usr/local/lib/python3.4/dist-packages | grep nltk
nltk
nltk-3.1.egg-info
So it should be available. So why am I getting an error when importing now?

Unable to import Flask-Restless

So I'm trying to use Flask-Restless. Unfortunately I cannot import flask.ext.restless.
>>> from flask.ext.restless import APIManager
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module
__import__(realname)
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/__init__.py", line 22, in <module>
from .manager import APIManager
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/manager.py", line 17, in <module>
from .views import API
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/views.py", line 114
except ProcessingException, exception:
>>> import flask.ext.restless
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module
__import__(realname)
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/__init__.py", line 22, in <module>
from .manager import APIManager
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/manager.py", line 17, in <module>
from .views import API
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/views.py", line 114
except ProcessingException, exception:
^
SyntaxError: invalid syntax
These are the packages in my current virtualenv.
$ pip freeze
Flask==0.10.1
Flask-Admin==1.0.7
Flask-Restless==0.12.1
Flask-SQLAlchemy==1.0
Jinja2==2.7.2
MarkupSafe==0.18
SQLAlchemy==0.9.2
WTForms==1.0.5
Werkzeug==0.9.4
itsdangerous==0.23
mysql-connector-python==1.1.5
python-dateutil==2.2
six==1.5.2
I've been trying to do the tutorial (slightly modified though):
author = 'mrkaiser'
import flask
from sqlalchemy import Column, Integer, String, Sequence
from sqlalchemy import ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship
from sqlalchemy.orm import scoped_session, sessionmaker
from flask.ext.restless import APIManager
app = Flask(__name__)
engine = create_engine('mysql+mysqlconnector://flask:dummypass#localhost/test', convert_unicode=True)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
mysession = scoped_session(Session)
Base = declarative_base()
Base.metadata.bind = engine
class Person(Base):
__tablename__ = 'people'
id = Column(Integer, Sequence('person_id_seq'), primary_key=True)
first_name = Column(String(50))
last_name = Column(String(50))
url = Column(String(150))
about = Column(String(400))
def __repr__(self):
return "Person<first_name=%s,last_name=%s,url=%s" % (self. first_name, self.last_name, self.url)
# Create the database tables.
Base.metadata.create_all()
# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, session=mysession)
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Person, methods=['GET'])
# start the flask loop
app.run()
I get the same error when I run it via the command line. Not sure what to do. Thanks!
For those having the same issue with flask restless in python 3, upgrade to the current dev version. Issue was fixed here.
pip install git+https://github.com/jfinkels/flask-restless/