"How to fix' ImportError: cannot import name 'SQLAlchemy' - python-3.x

import os
from flask import Flask
from flask_wtf import FlaskForm
import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
bsdir = os.path.abspath(os.path.dirname(__file__))
# print(bsdir)
app = Flask(__name__, template_folder = 'template')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(bsdir,'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATION'] = False
db = SQLAlchemy(app)
class Puppy(db.Model):
pass
Traceback (most recent call last):
File "D:\PYTHON\GIT_EXC\FLASK_\flask_sqlalchemyex.py", line 5, in
from flask_sqlalchemy import SQLAlchemy File "D:\PYTHON\GIT_EXC\FLASK_\flask_sqlalchemy.py", line 4, in
from flask_sqlalchemy import SQLAlchemy ImportError: cannot import name 'SQLAlch

Try using this command
pip install flask-sqlalchemy --user
It worked for me
And If you are using PyCharm go to file
Select Invalidate caches and restart

Try to install it with these commands , (it worked for me):
pip install flask-sqlalchemy
pip3 install flask-sqlalchemy
Refer this site for Example
or
Refer the official guide site for installation
if it doesnt worked then try above commands with --user at the end of both commands
another solution maybe to install an IDE (if you are not using one) like PyCharm ; rather than a some simple text editors
First command installs package to python v2.x
Second one installs package to python 3.x
If you want to use 3.x to run your app ;then go to configuration and change it to python 3.x
Refer this for Getting Help / Development / Bug reporting

Try changing the name of your .py file. It may be causing a conflict with the flask-sqlalchemy package.
"D:\PYTHON\GIT_EXC\FLASK_\flask_sqlalchemy.py"

I faced same issue while instantiating airflow db by command
$airflow db init
error: ImportError: cannot import name 'SQLAlchemyAutoSchema'
Fixes:
Uninstall:
$pip uninstall marshmallow-sqlalchemy
Then upgrade it to version 0.24.0
$pip3 install marshmallow-sqlalchemy==0.24.0
My problem resolved and able to initialize airflow db.

Related

ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.10/collections/__init__.py)

I'm running this simple flask app in vs code. Dockerized it and loaded to ECS. The task is exiting with the following error:
ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.10/collections/init.py)
The base image I'm using is python3.10.
Following is the dockerfile code:
FROM python:3.10
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
Why am I getting this error?
Trying the following in python 3.9:
>>> from collections import MutableMapping
<stdin>:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
So I'm guessing your problem is you're using python 3.10 where you need to do:
>>> from collections.abc import MutableMapping
if you update the flask package to >=2.0 it will import it correctly also in python 3.10
Looks like you're doing the same docker tutorial that I am.
#geza.denes is right, we need flask 2.0 or greater.
you can update this in flask-app/requirements.txt
Rebuild your docker image and run it again, should work!
For Python 3.10.6
ImportError: cannot import name 'MutableMapping' from 'collections'
(/usr/local/lib/python3.10/collections/init.py)
For me Modules Mapping, MutableMapping and Sequence were missing in the /usr/local/lib/python3.10/collections/init.py adding them solved the problem.
from _collections_abc import Mapping
from _collections_abc import MutableMapping
from _collections_abc import Sequence

Pandas-profiling error AttributeError: 'DataFrame' object has no attribute 'profile_report'

I wanted to use pandas-profiling to do some eda on a dataset but I'm getting an error : AttributeError: 'DataFrame' object has no attribute 'profile_report'
I have created a python script on spyder with the following code :
import pandas as pd
import pandas_profiling
data_abc = pd.read_csv('abc.csv')
profile = data_abc.profile_report(title='Pandas Profiling Report')
profile.to_file(output_file="abc_pandas_profiling.html")
AttributeError: 'DataFrame' object has no attribute 'profile_report'
The df.profile_report() entry point is available from v2.0.0. soln from here
Did you install pandas-profiling via pip or conda?
use : pip install -U pandas-profiling to solve this and restart your kernel
The issue is that the team hasn't updated the pip or conda installations yet (described here). If you installed using one of these, try this for the time being.
profile = pandas_profiling.ProfileReport(df)
print(profile)
This should work for those who want to use the latest version:
Run pip uninstall pandas_profiling from anaconda prompt (given you're using Spyder, I'd guess this would be your case) / or command prompt
Run pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip
If you're using something like a Jupyter Notebook/Jupyter Lab, be sure to restart your kernel and re-import your packages.
I hope this helps.
For the those using google colabs, the profiling library is outdated, hence use the command below and restart the runtime
! pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip
The only workaround I found was that the python script I made is getting executed from the command prompt and giving the correct output but the code is still giving an error in Spyder.
Some of the version of the pandas-profiling does not work for me and I installed 2.8.0 version and it work for me.
!pip install pandas-profiling==2.8.0
import numpy as np
import pandas as pd
import pandas_profiling as pp
df = pd.read_csv('/content/sample_data/california_housing_train.csv')
profile = df.profile_report(title = "Data Profiling Report")
profile.to_file("ProfileReportTest.html")
If none of the above worked, can you check by setting the encoding to unicode_escape in read_csv? It may be due to one of your columns
encoding = 'unicode_escape'
My solution
For me installation via pip was giving errors, therefore I installed it via conda from here.
Code Example
And here is the code example to use profile report:
import pandas as pd
from pandas_profiling import ProfileReport
data_abc = pd.read_csv('abc.csv')
profile = ProfileReport(data_abc, minimal=True)
profile.to_file("abc_pandas_profiling.html")
To read the html file I used the following code
df = pd.read_html("abc_pandas_profiling.html")
print(df[0])
Try in conda environment
!pip install --user pandas-profiling
import pandas_profiling
data.profile_report()

install flask-pymongo with pip- python 3.6

hellow everyone,
i tried to install PyMongo package with pip( i hope that is the way to do it). [its a part of a login system]
my command line was:
C:/Python36/Scripts/pip install PyMongo
But, when i put it in the command line i got
the system cannot find the file specificied.
And the import line in the code was:
from flask_pymongo import PyMongo
Do someone knows what should i do?
Thanks!!
Flask-PyMongo is a different package than PyMongo. It looks like you've only installed PyMongo, but are trying to use Flask-PyMongo in your application. You should run C:/Python36/Scripts/pip install Flask-PyMongo to install Flask-PyMongo.
You installed PyMongo and tried to import PyMongo from flask_pymongo.
PyMongo is a package to work with MongoDB. To make this work more efficient, Flask just made an extension named "Flask-PyMongo". So, we can directly import "Flask-PyMongo" and start working with MongoDB in a very efficient way.
You can think "Flask-PyMongo" as a wrapper of "PyMongo".

SSHTunnel not installed correctly?

I'm using anaconda python 3.5.2 in PyCharm using windows 10.
I'm pretty new to python and a complete noob to PyCharm!
I just want to use SSHtunnel.
I have intalled the package and it shown in the project interpreter page:
However when I try to run the code which I cut and pasted from example 1 here https://github.com/pahaz/sshtunnel:
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
'pahaz.urfuclub.ru',
ssh_username="pahaz",
ssh_password="secret",
remote_bind_address=('127.0.0.1', 8080)
)
server.start()
print(server.local_bind_port) # show assigned local port
# work with `SECRET SERVICE` through `server.local_bind_port`.
server.stop()
I get the following error:
C:\Users\HP\Anaconda3\python.exe C:/Users/HP/PycharmProjects/SSH_Downloader/SSH_Downloader
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/SSH_Downloader/SSH_Downloader", line 1, in <module>
from sshtunnel import SSHTunnelForwarder
ImportError: cannot import name 'SSHTunnelForwarder'
Process finished with exit code 1
How can I sort this out?
Just run the command below:
pip install sshtunnel
sshtunnel for python and sshtunnel Anaconda-python are different modules.
To install sshtunnel for Anaconda; you need to use the Anaconda repository. use below pip command; Hope this helps:
pip install -i https://pypi.anaconda.org/pypi/simple sshtunnel
More Description Here
I think your name of file is sshtunnel.py
i face this problem on window 10 and fix it buy this line
easy_install sshtunnel

Python 3: No module named zlib?

I am trying to run my Flask application with an Apache server using mod_wsgi, and it has been a bumpy road, to say the least.
It has been suggested that I should try to run my app's .wsgi file using Python to make sure it is working.
This is the contents of the file:
#!/usr/bin/python
activate_this = '/var/www/Giveaway/Giveaway/venv/bin/activate_this.py'
with open(activate_this) as f:
code = compile(f.read(), "somefile.py", 'exec')
exec(code)
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/Giveaways/")
from Giveaways import application
application.secret_key = 'Add your secret key'
However, when I run it, I get this error:
ImportError: No module named 'zlib'
And no, I am not using some homebrewed version of Python - I installed Python 3 via apt-get.
Thanks for any help.
does the contents of somefile.py include the gzip package? in which case you may have to install gzip package via pip or similar

Resources