python 3.7.4 : FLASK_SQLALCHEMY No module named '_sqlite3' - python

I'm using Debian "Linux mint 19.1" and python 3.7.4 and system have already 2.7 & 3.6 versions. I create an env but when I start to create database file with db.create_all() I have "...
ModuleNotFoundError: No module named '_sqlite3' "
I have try by installing this packages [ quick-sqlite , pysqlite3 ]
db.create_all()
Traceback (most recent call last):
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 338, in dbapi
from pysqlite2 import dbapi2 as sqlite
ModuleNotFoundError: No module named 'pysqlite2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/flask_sqlalchemy/init.py", line 1033, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/flask_sqlalchemy/init.py", line 1025, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/flask_sqlalchemy/init.py", line 956, in get_engine
return connector.get_engine()
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/flask_sqlalchemy/init.py", line 561, in get_engine
self._engine = rv = self._sa.create_engine(sa_url, options)
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/flask_sqlalchemy/init.py", line 966, in create_engine
return sqlalchemy.create_engine(sa_url, **engine_opts)
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/sqlalchemy/engine/init.py", line 435, in create_engine
return strategy.create(*args, **kwargs)
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 343, in dbapi
raise e
File "/media/gabalawy/projetcs/training/Flask_Blog/env3.7/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 341, in dbapi
from sqlite3 import dbapi2 as sqlite # try 2.5+ stdlib name.
File "/usr/local/lib/python3.7/sqlite3/init.py", line 23, in
from sqlite3.dbapi2 import *
File "/usr/local/lib/python3.7/sqlite3/dbapi2.py", line 27, in
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

I have Installed "pysqlite3"
pip install pysqlite3
and modify
"/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 341
from sqlite3 import dbapi2 as sqlite # try 2.5+ stdlib name.
to
from pysqlite3 import dbapi2 as sqlite # try 2.5+ stdlib name.

I believe that the _sqlite3 import is an internal import and should be available from your standard python installation. The fact that it's missing suggests a problem with your python build (did you compile it yourself).
I was able to fix this on suse by first ensuring that the ncurses were installed and then installing:
zypper install python-curses
On ubunti I guess the equivalent is (although I'm not sure its absence would give the same error):
sudo apt-get install libncursesw5-dev
(my complete build pre-requisites are:
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
)

Related

ffi library can't be loaded from cryptography.hazmat.bindings._openssl

I'm trying to open a mysql database on windows 10, with anaconda (python 3.7 64 bit) and the pymysql package.
From windows command prompt, I do the following :
import pymysql
db = pymysql.connect(host='localhost', user='tester', passwd='xxxx', port=3306, db='mydb')
Username, password and port are correct; the host should be localhost (I get the same error if I pass 127.0.0.1); but I get the following error, which at the end states that it can't find the ffi package:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\programs\Anaconda3\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\programs\Anaconda3\lib\site-packages\pymysql\connections.py", line 327, in __init__
self.connect()
File "C:\programs\Anaconda3\lib\site-packages\pymysql\connections.py", line 598, in connect
self._request_authentication()
File "C:\programs\Anaconda3\lib\site-packages\pymysql\connections.py", line 873, in _request_authentication
auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
File "C:\programs\Anaconda3\lib\site-packages\pymysql\_auth.py", line 252, in caching_sha2_password_auth
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
File "C:\programs\Anaconda3\lib\site-packages\pymysql\_auth.py", line 132, in sha2_rsa_encrypt
rsa_key = serialization.load_pem_public_key(public_key, default_backend())
File "C:\programs\Anaconda3\lib\site-packages\cryptography\hazmat\backends\__init__.py", line 15, in default_backend
from cryptography.hazmat.backends.openssl.backend import backend
File "C:\programs\Anaconda3\lib\site-packages\cryptography\hazmat\backends\openssl\__init__.py", line 7, in <module>
from cryptography.hazmat.backends.openssl.backend import backend
File "C:\programs\Anaconda3\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 53, in <module>
from cryptography.hazmat.bindings.openssl import binding
File "C:\programs\Anaconda3\lib\site-packages\cryptography\hazmat\bindings\openssl\binding.py", line 13, in <module>
from cryptography.hazmat.bindings._openssl import ffi, lib
ImportError: DLL load failed: The specified module could not be found.
I've seen that conda navigator (nor pip) doesn't see an ffi package from its standard channels.
I have already tried this suggestion (adding the conda-forge channel) :
conda config --add channels conda-forge
conda install libffi
but conda takes forever in "solving environment" and doesn't give answers in reasonable time.
What am I supposed to do to install ffi and being able to connect to the db ? Am I the first in the world in calling mysql from python/Anaconda on windows ? I suppose there must be a solution.
Thanks, Marco
I ran into the same problem. Here's what solved it for me:
1. pip uninstall scrapy
2. conda uninstall scrapy
3. pip install --force --upgrade scrapy
4. scrapy startproject dingding
It works.
I got similar error:
File "/root/ncs/bootloader/mcuboot/scripts/imgtool.py", line 22, in <module>
main.imgtool()
File "/usr/lib/python3.7/site-packages/lib/python3.7/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/lib/python3.7/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/lib/python3.7/site-packages/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/lib/python3.7/site-packages/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/lib/python3.7/site-packages/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/root/ncs/bootloader/mcuboot/scripts/imgtool/main.py", line 111, in getpub
key = load_key(key)
File "/root/ncs/bootloader/mcuboot/scripts/imgtool/main.py", line 74, in load_key
key = keys.load(keyfile)
File "/root/ncs/bootloader/mcuboot/scripts/imgtool/keys/__init__.py", line 52, in load
backend=default_backend())
File "/usr/lib/python3.7/site-packages/cryptography/hazmat/backends/__init__.py", line 14, in default_backend
from cryptography.hazmat.backends.openssl.backend import backend
File "/usr/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/__init__.py", line 6, in <module>
from cryptography.hazmat.backends.openssl.backend import backend
File "/usr/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 113, in <module>
from cryptography.hazmat.bindings.openssl import binding
File "/usr/lib/python3.7/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 14, in <module>
from cryptography.hazmat.bindings._openssl import ffi, lib
ImportError: this module was compiled for Python 3.5
Solution:
Search _cffi_backend.cpython*.so in /usr/lib and /usr/local/lib
Kill these _cffi_backend.cpython*.so
Search cffi directories in /usr/lib and /usr/local/lib
Kill these directories
pip3 install --force-reinstall cryptography

ModuleNotFoundError: No module named 'MySQLdb' Amazon MySQL RDS SQLAlchemy

I am having issues with connecting Amazon AWS MySQL with SQLAlchemy. According to the instruction, I have connected.
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://<user>:<password>#<host>/<dbname>
But there is an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 74, in __call__
return self.registry()
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1001, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2950, in __call__
return self.class_(**local_kw)
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 877, in engine
return self.get_engine()
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
return connector.get_engine()
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 559, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 424, in create_engine
return strategy.create(*args, **kwargs)
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 81, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 102, in dbapi
return __import__('MySQLdb')
ModuleNotFoundError: No module named 'MySQLdb'
I am using Python3.7 version. Even according to the stackoverflow, I have installed pymysql, but still facing the problem.
Thank you for your reply.
If you use the PyMySQL client to connect to MySQL, your SQLAlchemy connection string needs to start with
mysql+pymysql://
instead of mysql://.
If you connect with mysql:// then you will need to install the MySQLdb library as shown in the Traceback.
Above issues has been solved with the following:
import pymysql
pymysql.install_as_MySQLdb()
Nothing to change anywhere.
Step 1
If you want to use MySQLDB you need to use one of the following commands. Which one depends on what OS and software you have and use.
easy_install mysql-python (mix os)
pip install mysql-python (mix os/ python 2)
pip install mysqlclient (mix os/ python 3)
apt-get install python-mysqldb (Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
yum install MySQL-python (Linux Fedora, CentOS ...)
For Windows, see this answer: Install mysql-python (Windows)
Step 2:
Create Engine
engine = create_engine('mysql+mysqldb://...', pool_recycle=3600)
use the create_engine.pool_recycle option which ensures that a connection will be discarded and replaced with a new one if it has been present in the pool for a fixed number of seconds:
Create Connection object
conn = engine.connect()
Execute SQL queries
conn.execute("SELECT * From table;")

How to install mtools by pip on Mac OS?

I followed this instruction to install mtools: https://github.com/rueckstiess/mtools/blob/develop/INSTALL.md. I tried both by pip and building from source but failed to launch it. I got below error when trying to launch mlaunch. I see this error relates to python and I am not familiar with python. Does anyone have any idea on this error?
$ mlaunch
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/bin/mlaunch", line 9, in <module>
load_entry_point('mtools==1.2.4.dev0', 'console_scripts', 'mlaunch')()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 565, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2697, in load_entry_point
return ep.load()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2370, in load
return self.resolve()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2376, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mtools-1.2.4.dev0-py2.7.egg/mtools/mlaunch/mlaunch.py", line 40, in <module>
raise ImportError("Can't import pymongo. See http://api.mongodb.org/python/current/ for instructions on how to install pymongo.")
ImportError: Can't import pymongo. See http://api.mongodb.org/python/current/ for instructions on how to install pymongo.
The error message in the trace states:
ImportError: Can't import pymongo. See http://api.mongodb.org/python/current/ for instructions on how to install pymongo.
This means that you are missing a dependency called pymongo to run mlaunch.
You can install pymongo by running the following command:
python -m pip install pymongo
For more information on how to install pymongosee the official documentation: https://api.mongodb.com/python/current/installation.html

Error when install pip

I have installed python2.7 on Linux and I want to install pip under /path/to/python2.7.9/bin. So I did this:
> cd /path/to/setuptools-16.0
> /path/to/python2.7.9/bin/python setup.py install
> cd /path/to/pip-7.0.0
> /path/to/python2.7.9/bin/python setup.py install
Then pip is installed under /path/to/python2.7.9/bin. But when I executed /path/to/python2.7.9/bin/pip, it shows:
> ~/local/python-2.7.9/bin/pip
Traceback (most recent call last):
File "/root/local/python-2.7.9/bin/pip", line 9, in <module>
load_entry_point('pip==7.0.0', 'console_scripts', 'pip')()
File "/root/local/python-2.7.9/lib/python2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 345, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/root/local/python-2.7.9/lib/python2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2381, in load_entry_point
return ep.load()
File "/root/local/python-2.7.9/lib/python2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2087, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
File "/root/local/python-2.7.9/lib/python2.7/site-packages/pip-7.0.0-py2.7.egg/pip/__init__.py", line 15, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/root/local/python-2.7.9/lib/python2.7/site-packages/pip-7.0.0-py2.7.egg/pip/vcs/subversion.py", line 9, in <module>
from pip.index import Link
File "/root/local/python-2.7.9/lib/python2.7/site-packages/pip-7.0.0-py2.7.egg/pip/index.py", line 30, in <module>
from pip.wheel import Wheel, wheel_ext
File "/root/local/python-2.7.9/lib/python2.7/site-packages/pip-7.0.0-py2.7.egg/pip/wheel.py", line 34, in <module>
from pip._vendor.distlib.scripts import ScriptMaker
File "/root/local/python-2.7.9/lib/python2.7/site-packages/pip-7.0.0-py2.7.egg/pip/_vendor/__init__.py", line 92, in load_module
raise ImportError("No module named '%s'" % (name,))
ImportError: No module named 'pip._vendor.distlib.scripts'
I don't know what is wrong and what should I do. Can you help me?
Thanks.
As suggested here, try installing SSL support.
On Debian:
apt-get -y install libssl-dev
On CentOS:
yum install openssl openssl-devel
If libssl-dev is not present, try: openssl-dev.

broken easy_install and pip after upgrading to OS X Mavericks

Upgraded to OS X 10.9 Mavericks and installed XCode, Command Line Tools, XQuartz, etc. Trying to run a pip install now, but it says that the distribution is not found:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2603, in <module>
working_set.require(__requires__)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 666, in require
needed = self.resolve(parse_requirements(requirements))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 565, in resolve
raise DistributionNotFound(req) # XXX put more info here
pkg_resources.DistributionNotFound: pip==1.4.1
So I tried to install pip with an easy_install. Turns out that's borked too:
Traceback (most recent call last):
File "/usr/local/bin/easy_install", line 5, in <module>
from pkg_resources import load_entry_point
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2607, in <module>
parse_requirements(__requires__), Environment()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 565, in resolve
raise DistributionNotFound(req) # XXX put more info here
pkg_resources.DistributionNotFound: setuptools==1.1.6
So some of the other threads say to reinstall setuptools with a sudo python ez_setup.py. It seems to work fine:
Installed /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-1.1.6-py2.7.egg
Processing dependencies for setuptools==1.1.6
Finished processing dependencies for setuptools==1.1.6
But when running the easy_install pip, the same pkg_resources.DistributionNotFound: setuptools==1.1.6 error occurs. Anyone else have this problem? Any ideas how to fix this?
Install easy_install:
Download ez_setup.py module from https://pypi.python.org/pypi/setuptools
$ cd path/to/ez_setup.py
$ python ez_setup.py
Install pip:
$ sudo easy_install pip
try sudo python -m easy_install pip
I ran into a similar problem with git-review.
$ git review -s
Traceback (most recent call last):
File "/usr/local/bin/git-review", line 11, in <module>
sys.exit(main())
File "/Library/Python/2.7/site-packages/git_review/cmd.py", line 1132, in main
(os.path.split(sys.argv[0])[-1], get_version()))
File "/Library/Python/2.7/site-packages/git_review/cmd.py", line 180, in get_version
provider = pkg_resources.get_provider(requirement)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 197, in get_provider
return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 666, in require
needed = self.resolve(parse_requirements(requirements))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 565, in resolve
raise DistributionNotFound(req) # XXX put more info here
pkg_resources.DistributionNotFound: git-review
The git-review team said it was a bug with pkg_resources that could be fixed with
sudo pip install --upgrade setuptools
This worked fine for me.

Categories

Resources