Following the documentation here https://docs.sqlalchemy.org/en/14/orm/extensions/mypy.html I got an error while running mypy on any file using sqlalchemy mypy plugin.
To Reproduce
Create a new virtualenv using Python 3.8
pip install sqlalchemy[mypy]==1.4
Create a simple mypy config (mypy.ini)
[mypy] plugins = sqlalchemy.ext.mypy.plugin
Run mypy on any file
mypy --config-file=mypy.ini
Error
Error importing plugin "sqlalchemy.ext.mypy.plugin": cannot import
name 'Optional' from 'mypy.plugin'
Versions.
OS: Linux Ubuntu 22.04
Python: 3.8
SQLAlchemy: 1.4
mypy: 0.991
Following the issue on GitHub the problem is the SQLAlchemy version.
SQLAlchemy 1.4.0 is not supported.
Using SQLAlchemy 1.4.44 solved the problem.
I want to import a dataframe into a access database but I got an error NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:access.pyodbc
from sqlalchemy import create_engine
import urllib
import pyodbc
conec = (r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
r"DBQ=C:\Users\redim\Desktop\18_marzo\Libr2.accdb"
)
con = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(conec)}"
acc_engine = create_engine(con)
df.to_sql('hola', acc_engine)
For some reason this only works in Jupyter notebooks, not in PyCharm which I was having the same problems for quite some time in both until I upgraded SQLalchemy
I use conda install with most of my libraries:
conda update sqlalchemy
But if you are using pip, then below is command:
pip install --upgrade sqlalchemy
I went from sqlalchemy 1.3.7 to 1.4.39 and the error went away, at least in Jupyter Notebooks anyways.
I don't know why my code doesn't work, I Import the library in the first line
from flask_sqlalchemy import SQLAlchemy
Knowing that I've already install flask_sqlalchemy
If it is a flask app you should run the app in virtualenv, otherwise simply running by python appName.py will give that error.
The other way is "set FLASK_APP=appName.py" then run flask run.
The first thing I'd check would be can you import the whole flask_sqlalchemy module:
import flask_sqlalchemy
If that works, it could be a case or spelling issue as that command works in my local environment.
It seems like you installed the wrong package. If you are using python3 you have to use python3 -m pip install <module>
I tried to import sqlalchemy to my pytest file but when I tried to run it shows this error, even though I have already installed sqlalchemy.
new.py:1: in <module>
import sqlalchemy
E ImportError: No module named sqlalchemy
my code :
import pytest
import sqlalchemy
the code was just when I was importing the sqlalchemy.
How do I fix it? Thanks in advance
I had the same, and fixed by running tests with:
python -m pytest .
I'm working through a flask tutorial and am trying to run a script that creates a database instead of doing it through the command line. It uses the SQLAlchemy-migrate package, but when I try to run the script, it gives an ImportError.
This is the terminal output:
Sean:app seanpatterson$ python ./db_create.py
Traceback (most recent call last):
File "./db_create.py", line 2, in <module>
from migrate.versioning import api
ImportError: No module named migrate.versioning
This is the db_create.py script:
#!flask/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
This is the config file it references:
#!/usr/bin/env python
import os
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
This application is being run with a virtual environment. This are the module that relates to it that I have installed in the environment:
sqlalchemy_migrate-0.7.2-py2.7.egg-info
Any help appreciated
pip install sqlalchemy==0.7.9
and
pip install sqlalchemy-migrate==0.7.2
and
optionally this flask-whooshalchemy==0.55a should solve the problem
ImportError: No module named migrate.versioning probably means the module is not installed. Make sure it has been installed in the correct virtual environment, it is activated (you ran the activate script in that environment) and the selected Python binary is actually making use of that environment (i.e. you are using Python2 and not Python3).
As said by #BoppreH earlier
ImportError: No module named migrate.versioning
means that the module named 'migrate' is not installed in your virtual environment or your system. First make sure that you are using the proper environment and that it is activated using the activate script.
I had the same problem and had the correct environment set up. But still the error was not solved.
What worked for me was installing the sqlalchemy-migrate package from pip. After activating my environment, I ran the following code to install it :
pip install sqlalchemy-migrate
flask/bin/pip install flask-sqlalchemy without defining the version worked fine for me.
run :
easy_install Flask-SQLAlchemy
to install Flask-SQLAlchemy
sudo pip install flask-migrate
to install flask-migrate
I think this error might pop up for several obscure reasons, I would like to add another which I experienced:
I had the same exact error while having sqlalchemy-migrate correctly installed, and guess what, it didn't work just because I had named the migration script file as migrate.py, this created some conflict with the migrate package.
In fact PyCharm warned me with this message:
"Import resolves to its containing file... This inspection detects names that should resolve but don't."
I renamed the migration script as db_migrate.py and everything started working fine.
I could understand what was the issue cause I had another project with an identical set-up but with migrate-sqlalchemy working perfectly and the only difference was indeed that file name...
Hope this might help someone one day...
I had the same problem - "No module named migrate.versioning", and everything is much easier than we are talking about, you need to perform the commands "run"
file: db_create.py or file: db_migrate.py if you using PyCharm (not from the terminal). And you will have the expected output: "New migration saved as D:...there is my path...\microblog\db_repositort/versions/001_migration.py
Current database version: 1"