ImportError: No module named sqlalchemy - python

I'm unable to find a module in python ,though easy_install says its already installed.
Any idea how to resolve this isseue?
$ python -c "from flaskext.sqlalchemy import SQLAlchemy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named sqlalchemy
$ python -V
Python 2.7
$ sudo easy_install sqlalchemy
Searching for sqlalchemy
Best match: SQLAlchemy 0.7.7
Adding SQLAlchemy 0.7.7 to easy-install.pth file
Using /usr/lib/python2.7/site-packages
Processing dependencies for sqlalchemy
Finished processing dependencies for sqlalchemy
$ sudo pip install SQLAlchemy --upgrade Requirement already
up-to-date: SQLAlchemy in /usr/lib/python2.7/site-packages Cleaning
up...
Though pip says it's installed.But I can't find them in sys.path output.
$ sudo python -c "import sys;print sys.path" ['',
'/usr/lib/python2.7/site-packages/Flask_SQLAlchemy-0.15-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask-0.8-py2.7.egg',
'/usr/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask_WTF-0.5.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/WTForms-0.6.3-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask_Mail-0.6.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/blinker-1.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/lamson-1.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/python_daemon-1.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/mock-0.8.0-py2.7.egg',
'/usr/lib/python2.7/site-packages/chardet-1.0.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/lockfile-0.9.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask_FlatPages-0.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/Markdown-2.1.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/PyYAML-3.10-py2.7-linux-i686.egg',
'/usr/lib/python2.7/site-packages/uWSGI-1.0.3-py2.7.egg',
'/usr/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg',
'/usr/lib/python27.zip', '/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

Did you install flask-sqlalchemy? It looks like you have SQLAlchemy installed but not the Flask extension. Try pip install Flask-SQLAlchemy in your project's virtualenv to install it from PyPI.

I just experienced the same problem. Apparently, there is a new distribution method, the extension code is no longer stored under flaskext.
Source: Flask CHANGELOG
This worked for me:
from flask_sqlalchemy import SQLAlchemy

Install Flask-SQLAlchemy with pip in your virtualenv:
pip install flask_sqlalchemy
Then import flask_sqlalchemy in your code:
from flask_sqlalchemy import SQLAlchemy

Okay,I have re-installed the package via pip even that didn't help. And then I rsync'ed the entire /usr/lib/python-2.7 directory from other working machine with similar configuration to
the current machine.It started working. I don't have any idea ,what was wrong with my setup. I see some difference "print sys.path" output earlier and now. but now my issue is resolved by this work around.
EDIT:Found the real solution for my setup. upgrading "sqlalchemy only doesn't solve the issue" I also need to upgrade flask-sqlalchemy that resolved the issue.

try this:
from flask.ext.sqlalchemy import SQLAlchemy

first install the library
pip install flask_sqlalchemy
after that
from flask_sqlalchemy import SQLAlchemy
put this in app.py file to get the access of database through SQLAlchemy

So here is an idea!
Since it seemed to work somewhere else.
install python-virtualenv
and optionally you can install virtualenv-wrapper (which is pretty cool to create projects and so on)
In each env, you might have different versions of eggs. In other word, you could have sqlalchemy 1 and sqlaclhemy 1.5 in two different envs and they won't conflict with each others. It seems that you have a problem with your currently installed eggs.
So here we go:
virtualenv --no-site-packages foo
source foo/bin/activate
The parameter --no-site-packages will create a virtualenv and not use the packages already installed on your computer. It's pretty much like a bare python install.
source foo/bin/activate loads the virtualenv.
It's not that really userfriendly. And that's why http://www.doughellmann.com/projects/virtualenvwrapper/ exists.
That said, you should see somthing like thant in your terminal "(foo)user#domain$:" once your virtualenv is activated. It means that you can go on!
Then you have to do.
python setup.py develop of your project. It should download and install dependencies of your project in the virtualenv located in foo. If you need to install anything else, please use pip or easy_install without using sudo. When using virtualenv, you almost never need to use sudo. Sudo will install package in your global python install while it's not required and not really desirable.
If something happens in your virtualenv, you can always delete it and create a new one. This is no big deal. No need to mess with anything. Doesn't work? start over, do pip install -U if needed, define the versions if needed and so on.
Last but not least, in the other answers, it seems that the import changed. If the new versions for flask-sqlalchemy is located somewhere else, you should update your import or install the version you used to use.

This code works perfectly:
import sqlalchemy
Maybe you installed the package in another version of the interpreter?
Also, like Shawley pointed out, you need to have the flask extension installed in order for it to be accessible.

I'm new comer, use python 3.8 and met the same problem. I installed with pip instead of pip3 because i thought the pip installer is the same for python2 and python3
so this is proper installation
pip3 install flask_sqlalchemy

Probably a stupid mistake; but, I experienced this problem and the issue turned out to be that "pip3 install sqlalchemy" installs libraries in user specific directories.
On my Linux machine, I was logged in as user1 executing a python script in user2's directory. I installed sqlalchemy as user1 and it by default placed the files in user1's directory. After installing sqlalchemy in user2's directory the problem went away.

Solution for me was to use:
from flask_sqlalchemy import SQLAlchemy
instead of
from flask.ext.sqlalchemy import SQLAlchemy

Very late to the party but hopefully this will help someone, was in the same situation for about a hour without any of the solutions mentioned above working. (On a Windows 10 machine).
In the Settings/Preferences dialog (Ctrl+Alt+S), from the side menu select Project: | Project Interpreter.
Check which packages you currently have installed (You need SQLAlchemy and Flask-SQLAlchemy). Double click on any package name, an 'Available Packages' menu will open.
Search for the missing package(s) and click install.

On Windows 10 # 2019
I faced the same problem. Turns out I forgot to install the following package:
pip install flask_sqlalchemy
After installing the package, everything worked perfectly. Hope, it helped some other noob like me.

I just experienced the same problem using the virtual environment.
For me installing the package using python from the venv worked:
.\venv\environment\Scripts\python.exe -m pip install flask-sqlalchemy

I am not sure if it is still relevant but try uninstalling sqlalchemy and then installing flask-sqlalchemy.
I guess if you have sqlalchemy, flask-sqlalchemy wont work.
Tried in Python 3.8

I also faced the same problem by a silly mistake. I have created a separate conda environment for flask from scratch. However, I tried to test the import of FlaskSQLAlchemy using IPython. Finally, I realized that it was the system-based python, not the one coming from conda env.
Always make sure that your sys.path contains the directory with flask_sqlalchemy.

I was using virtualenv, even if the flask_sqlalchemy was installed, I was still getting it, what I did was to deactivate virtual environment, delete the flask from global packages, and delete virtual environment, create fresh one, install requirements again. And it worked. Hope this helps.

Related

ImportError: cannot import name '_literal_as_text' from 'sqlalchemy.sql.expression' [duplicate]

After installing apache-superset using pip in virtual environment, I run:
superset upgrade db
I run into the following error:
ImportError: cannot import name '_ColumnEntity' from 'sqlalchemy.orm.query' (/Users/ahmedawny/supersetenv/lib/python3.7/site-packages/sqlalchemy/orm/query.py)
Any advice would be appreciated.
The core reason for this is that sqlalchemy 1.4 shipped, so it is now the default that pip installs. There were many interface changes and a new query interface added.
So upgrading to 1.4 breaks stuff that depends on the sqlalchemy 1.3 API internals. In your requirements.txt file-- pin the project version to the 1.3.x series until the rest of your stuff catches up. For example, as of this date sqlalchemy-utils is still dependent on sqlalchemy 1.3x.
requirements.txt:
sqlalchemy < 1.4.0
Then use:
pip -r requirements.txt
to "downgrade" to the pinned version.
If you don't want to modify the requirements file, as recommended in #InsertSpywareTrackingHere's answer, you can manually pip install an older version instead:
pip install sqlalchemy==1.3.24
Go to sqlalchemy_utils.functions file and change :
from sqlalchemy.orm.query import _ColumnEntity (comment this one, or delete it)
from sqlalchemy.orm.context import _ColumnEntity (include)
in consequence of this, i belive it should change some stuff along the code...
you can temporarily fix this by changing the imports in sqlalchemy_utils\functions\orm.py:
from sqlalchemy.orm.query import _ColumnEntity
to
from sqlalchemy.orm.context import _ColumnEntity
#InsertSpywareTrackingHere answer is correct, Just want to add that a new release for Flask-AppBuilder was made that pins SQLAlchemy bellow 1.4.0. So installing apache-superset using pip install apache-superset should work now.

ImportError: cannot import name '_ColumnEntity' from 'sqlalchemy.orm.query'

After installing apache-superset using pip in virtual environment, I run:
superset upgrade db
I run into the following error:
ImportError: cannot import name '_ColumnEntity' from 'sqlalchemy.orm.query' (/Users/ahmedawny/supersetenv/lib/python3.7/site-packages/sqlalchemy/orm/query.py)
Any advice would be appreciated.
The core reason for this is that sqlalchemy 1.4 shipped, so it is now the default that pip installs. There were many interface changes and a new query interface added.
So upgrading to 1.4 breaks stuff that depends on the sqlalchemy 1.3 API internals. In your requirements.txt file-- pin the project version to the 1.3.x series until the rest of your stuff catches up. For example, as of this date sqlalchemy-utils is still dependent on sqlalchemy 1.3x.
requirements.txt:
sqlalchemy < 1.4.0
Then use:
pip -r requirements.txt
to "downgrade" to the pinned version.
If you don't want to modify the requirements file, as recommended in #InsertSpywareTrackingHere's answer, you can manually pip install an older version instead:
pip install sqlalchemy==1.3.24
Go to sqlalchemy_utils.functions file and change :
from sqlalchemy.orm.query import _ColumnEntity (comment this one, or delete it)
from sqlalchemy.orm.context import _ColumnEntity (include)
in consequence of this, i belive it should change some stuff along the code...
you can temporarily fix this by changing the imports in sqlalchemy_utils\functions\orm.py:
from sqlalchemy.orm.query import _ColumnEntity
to
from sqlalchemy.orm.context import _ColumnEntity
#InsertSpywareTrackingHere answer is correct, Just want to add that a new release for Flask-AppBuilder was made that pins SQLAlchemy bellow 1.4.0. So installing apache-superset using pip install apache-superset should work now.

pymongo - "dnspython" module must be installed to use mongodb+srv:// URIs

I am trying to connect MongoDB from Atlas.
My mongo uri is: mongodb+srv://abc:123#something.something.com/admin?retryWrites=True
My pymongo version is 3.6.1
I have installed dnspython and done import dns
But i still get this error:
dnspython module must be installed to use mongodb+srv:// URI
In order to use mongo+srv protocol, you need to install pymongo-srv
Launch this command to do it with python 3:
pip3 install pymongo[srv]
or this one for other versions:
pip install pymongo[srv]
And as suggested by #lukrebs, add quotes for ZSH:
pip3 install 'pymongo[srv]'
I would like to answer my own questions here. As I mentioned in the comment, the kernel of the jupyter notebook has to be restarted in order for the pymongo to take effect of the loaded dnspython.
I solved this problem with:
$ python -m pip install pymongo[srv]
In requirements.txt, replace pymongo with pymongo[tls,srv], as mentioned here.
I got stuck with the same problem and tried
pip install dnspython==2.0.0
This is the latest version from https://pypi.org/project/dnspython/
It worked :D
you can use mongo:// instead of mongodb+srv://
May be the protocol, your URI should start with:
mongo+srv instead of mongo+src
If it still not working please put a pip list with the versions of PyMongo and dnspython (and version of python that you are using)
I had the same problem on Ubuntu 18 but since I am using Anaconda
I just tried
Conda install dns python
I had an IPython running, it did not work while the same instance was open but when I restarted that instance it worked.
On a different machine using
Conda install dns python
and it worked but I had to restart my machine altogether due to a different reason before testing it
I had the same issue and found the following line.
import dns.resolver
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=['8.8.8.8']
It worked for me.
pip install dnspython
dnspython is a DNS toolkit for Python. It supports almost all record types. It can be used for queries, zone transfers, and dynamic updates. It supports TSIG authenticated messages and EDNS0.
None of the existing answers had worked for me. I had to do the following:
sudo apt-get install python3-dnspython

Installing python bottle framework for python 3.6.1 version instead of python 2.7.1 version?

I am currently working on a mongo db assignment using pymongo, as part of it we need to install python bottle framework. I have installed bottle on the Mac using the below command:
$ pip install bottle
got the below message returned.
Requirement already satisfied: bottle in /Library/Python/2.7/site-packages
when I run a program from terminal which uses bottle, it throws the below error:
$ python hello_m101p.py
Traceback (most recent call last):
File "hello_m101p.py", line 2, in <module>
import bottle
ModuleNotFoundError: No module named 'bottle'
I noted that i also have python 3.6.4 version installed on my system. I need bottle framework to be saved or installed for this version instead of the default 2.7 version. After a quick lookup around stack overflow, I tried the below command from one the suggested answers. I get the command not found error:
sudo pip-3.6.4 install bottle
sudo: pip-3.6.4: command not found
Any help to correct this error ? I am not sure how to address this. Please let me know if the question is unclear or need more context.
The default name for pip for Python version X.Y.Z is not pip-X.Y.Z, but pipX.Y. Some linux distros use different names for their versions, but I believe on Mac, both the python.org Python installer and the Homebrew python package (and you probably have one of those two) use the default names, at least as of 2018.
If you only have one 3.Y.Z, it will probably also be available as pip3, which is convenient.
If you've got different names and are hopelessly confused, but you do know how to run your Python 3 itself, you can always use the -m flag to run pip.
Also, most Mac Python installs don't need sudo. If you don't know whether you do, try it without first. If you get a bunch of permissions errors, then you do need sudo after all, but otherwise, don't use it.
So, what you want is probably any of these:
pip3.6 install bottle
pip3 install bottle
python3 -m pip install bottle

Conda 'ImportError: No module named ruamel.yaml.comments'

Conda gives error when I run any command with it.
Traceback (most recent call last):
File "/usr/local/bin/conda", line 7, in <module>
from conda.cli.main import main
File "/usr/local/lib/python2.7/dist-packages/conda/cli/__init__.py", line 8, in <module>
from .main import main # NOQA
File "/usr/local/lib/python2.7/dist-packages/conda/cli/main.py", line 46, in <module>
from ..base.context import context
File "/usr/local/lib/python2.7/dist-packages/conda/base/context.py", line 18, in <module>
from ..common.configuration import (Configuration, MapParameter, PrimitiveParameter,
File "/usr/local/lib/python2.7/dist-packages/conda/common/configuration.py", line 40, in <module>
from ruamel.yaml.comments import CommentedSeq, CommentedMap # pragma: no cover
ImportError: No module named ruamel.yaml.comments
The module ruamel.yaml.comments will normally be loaded from site-packages/ruamel/yaml/comments.py, and not from site-packages/ruamel_yaml/comments.py
Conda seems to have problems with properly supporting namespaces (ruamel.) which I can only attribute to not (yet) being fully pip compatible. That although "namespaces are a honking good idea", and package namespaces have been around for many years.
Assuming you can extend "conda" installations with pip you could try to do a normal install of ruamel.yaml with:
pip install ruamel_yaml==0.11.14
I would not normally recommend such an old version, but that is more likely to work in combination with the version conda uses itself internally.
The alternative would be to switch to using python and pip without conda, that way you can just use the latest version of software from PyPI.
Try pip install ruamel.yaml
It works for me.
Try conda install ruamel.yaml ... pip didnt work for me
Try sudo pip install ruamel_yaml
I went into this file:
/anaconda2/lib/python2.7/site-packages/dateparser/utils/__init__.py
edited this line:
import ruamel.yaml as yaml
to read
import ruamel_yaml as yaml
Changing the dot to an underscore worked for me.... I hope it works for you.
this worked for me:
pip install --upgrade ruamel.yaml --ignore-installed ruamel.yaml
from an answer in matsci.org
https://matsci.org/t/modulenotfounderror-no-module-named-ruamel/36183
The above answer didn't work for me. I had to do a fresh install of the core conda components as described in the conda docs here. Copy and pasted below:
Issue: My conda is broken and I want to fix it without blowing away the current installation
I am getting a conda error and want to reinstall Miniconda to fix it but when I try, it gives me the error that Miniconda (or Anaconda) is already installed and will not let me continue. I want to force the installation.
Resolution: Install Miniconda using the -f (force) option
Download and install the appropriate Miniconda for your computer operating system from the Miniconda download page using the force or -f option as shown:
bash Miniconda3-latest-MacOSX-x86_64.sh -f
NOTE: Substitute the appropriate filename and version for your
operating system.
NOTE: Be sure that you install to same install location as your
existing install so it overwrites the core conda files and does not
install a duplicate in a new folder.
Go to anaconda3\lib\site-packages\rpcq_base.py
and change line #22 :
from ruamel import yaml
to
from ruamel_yaml as yaml
This might not be a popular answer, but it finally helped me after many hours of troubleshooting:
Uninstall conda (I used this stack overflow solution) and also rm -rf miniconda3 in my home directory, fwiw.
Reinstalled conda using data camp's tutorial.
No other solutions worked for me after lots of head banging.
For python3 use
pip3 install ruamel_yaml
if pip3 not installed try at first
sudo apt install python3-pip
For me this was a conda/pip error. I'd tried to install (cwltool in my case) through pip.
It completed successfully, but then running any command gave me the error like above.
ImportError: No module named ruamel.yaml.
It turned out that the pip binary wasn't part of my conda env and was installing cwltool into a completely separate location.
To resolve the issue I completed the following:
conda activate <env I want to install cwltool into>
conda install -y pip
# Run 'rehash' now if you're using zsh to ensure you're using the right pip
pip install cwltool
cwltool -h
To add to what #user612161 has said, go to the directory of parent module (dateparser in this case) requiring ruamel.yaml:
cd anaconda2/lib/python2.7/site-packages/dateparser
and change all occurrences of ruamel.yaml into ruamel_yaml by the following command (Linux):
find . -name '*.py' | xargs sed -i 's/ruamel.yaml/ruamel_yaml/g'
The quick and easy is to ignore the previously installed version in an upgrade
pip install --ignore-installed ruamel_yaml==0.17.4
I was trying to link Bloomberg to Python
pip install --index-url=https://bcms.bloomberg.com/pip/simple blpapi
pip install xbbg
so far, so good.... then I tried to import a module from the package xbbg:
from xbbg import blp
and I was faced with an error, it couldn't find "ruamel.yaml" within the "param.py" within the xbbg module
When I dug into the folder C:/Anaconda3/Lib/site-packages I could see that there was a folder there called ruamel_yaml so I went back to the param.py file and edited ruamel.yaml to be ruamel_yaml as suggested in other posts.
"from xbbg import blp" now worked and I'm able to take data directly from Bloomberg into Python now. Problem solved.
I have a feeling that this issue is being caused by downloading different versions at different times as I've found the learning curve to get setup on Python difficult with many false starts. I was tearing my hair out a bit because I just got Python up and running linked to Bloomberg on my work pc but when I tried to link Bloomberg up to Python on my laptop it kept getting stuck with the "ruamel" issue. The version of Python on my laptop is much older than the version on my work pc. What makes me think that its a version issue is that I did not have to edit ruamel.yaml to be ruamel_yaml in order for me to link Python and BB.
These are just ideas, I'm too inexperienced at this stage to offer much more than to share what happened.

Categories