question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MacOS: Problem with run aiosqlite for any DB

See original GitHub issue

Problem with run aiosqlite for any DB at MacOS

MacOS (macOS Big Sur, version 11.1 (20C69):

ModuleNotFoundError: No module named ‘aiosqlite’

Python 3.9 ===== FastAPI ===== SQLite ===== Databases ===== aiosqlite

Description

Test project on FastAPI - including from the example: https://fastapi.tiangolo.com/advanced/async-sql-databases/

Example My Code:

from typing import List

import databases
import sqlalchemy
from fastapi import FastAPI
from pydantic import BaseModel


DATABASE_URL = "sqlite:///./test.db"

database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()


notes = sqlalchemy.Table(
    "notes",
    metadata,
    sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
    sqlalchemy.Column("text", sqlalchemy.String),
    sqlalchemy.Column("completed", sqlalchemy.Boolean),
)


# engine = sqlalchemy.create_engine(
#     DATABASE_URL, connect_args={"check_same_thread": False}
# )
# metadata.create_all(engine)

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

Start:

Activate virtual environment:

$ pip3 -m venv venv
$ source ./venv/bin/activate

Set dependencies:

$ pip3 install aiosqlite, databases, fastapi

Look the installed dependencies:

$ pip3 list
Package           Version
----------------- --------
aiosqlite         0.17.0
databases         0.4.3
fastapi           0.63.0
pip               21.1.1
pydantic          1.8.1
setuptools        56.0.0
SQLAlchemy        1.3.24
starlette         0.13.6
typing-extensions 3.10.0.0

Start server: $ uvicorn main:app --reload

The server won’t start On Mac OS (any editor: vim, VS Code, “Pycharm”, etc.)

ERROR shell:

(env) ➜  testDB2 uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [2967] using statreload
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/subprocess.py", line 62, in subprocess_started
    target(sockets=sockets)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/main.py", line 390, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/main.py", line 397, in serve
    config.load()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/config.py", line 278, in load
    self.loaded_app = import_from_string(self.app)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 23, in import_from_string
    raise exc from None
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/alexneo/Documents/GIT/FastAPI/testDB2/./main.py", line 11, in <module>
    database = databases.Database(DATABASE_URL)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/databases/core.py", line 66, in __init__
    backend_cls = import_from_string(backend_str)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/databases/importer.py", line 21, in import_from_string
    raise exc from None
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/databases/importer.py", line 18, in import_from_string
    module = importlib.import_module(module_str)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/databases/backends/sqlite.py", line 5, in <module>
    import aiosqlite
ModuleNotFoundError: No module named 'aiosqlite'

All of the dependencies are exist and I’ve can click them and moved to these dependencies file!!! And ‘aiosqlite’ too!

on Linux, everything works OK

but on a MacOS I can not run any of the databases (example SQLITE)

The error is as follows: does not see “aiosqlite”! All of the requirements have been seated correct (Python 3.9.5, venv, pip, aiosqlite, sqlalchemy, databases etc. - all of this have been updated)

This problem at StackOverflow:

https://stackoverflow.com/questions/67406165/macos-problem-with-run-aiosqlite-for-any-db

This solution hasn’t worked to my fail:

https://question-it.com/questions/467425/kak-ja-mogu-zapustit-server-fast-api-ispolzuja-pycharm

Screenshots:

  • Error https://i.stack.imgur.com/r50bg.jpg
  • Dependencies https://i.stack.imgur.com/OeBD9.jpg
  • Pip List https://i.stack.imgur.com/KbFRB.jpg

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
akxcommented, May 16, 2021

Dear @NeoSPU:

databases isn’t part of the standard library, yet you don’t install it, and yet you are able to import it and you can see in the tracebacks that its code is being run.) That means that it is somehow available as part of your system site-packages, as I mentioned before. (The same stands for sqlalchemy - your code seems to use it, yet you don’t install it.)

Please run python3 -m pip list outside any virtualenv and let us know the output. On a “venv-clean” system it should have little more than pip, wheel, setuptools.

0reactions
NeoSPUcommented, May 16, 2021

@akx Aarni Koskela

Yes!

You are right!

I’ve fixed my solution!

The solution is next:

  • cleaning dependencies, that have been installed outside the virtual environment:
$ ~ pip freeze > to_delete.txt
$ ~ pip uninstall -y -r to_delete.txt
$ ~ /usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip
$ ~ pip list
Package    Version
---------- -------
pip        21.1.1
setuptools 54.2.0
wheel      0.36.2
  • create a virtual environment:
$ ~ python3 -m venv venv
$ ~ source ./venv/bin/activate
- install dependencies from the requirements.txt file into your virtual environment of the project.

**requirements.txt:

aiosqlite==0.17.0
click==7.1.2
databases==0.4.3
fastapi==0.65.1
h11==0.12.0
pydantic==1.8.2
SQLAlchemy==1.3.24
starlette==0.14.2
typing-extensions==3.10.0.0
uvicorn==0.13.4
$ ~ pip3 install -r requirements.txt
$ ~ uvicorn main:app --reload

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MacOS: Problem with run aiosqlite for any DB - Stack Overflow
Running this command solved the issue for me: pip install databases[sqlite].
Read more >
Problem with run aiosqlite for any DB at MacOS - Bountysource
Problem with run aiosqlite for any DB at MacOS. MacOS (macOS Big Sur, version 11.1 (20C69):. ModuleNotFoundError: No module named 'aiosqlite ...
Read more >
Install py39-aiosqlite on macOS with MacPorts
aiosqlite provides a friendly, async interface to sqlite databases. It replicates the standard sqlite3 module, but with async versions of …
Read more >
__aiter__() and fetchone() are 15x slower than sqlite3 · Issue ...
In aiosqlite, fetchmany() and fetchall() offer virtually the same performance as in sqlite3, but __aiter__() and fetchone() are 15 times slower ...
Read more >
aiosqlite: Sqlite for AsyncIO — aiosqlite documentation
aiosqlite provides a friendly, async interface to sqlite databases. It replicates the standard sqlite3 module, but with async versions of all the standard ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found