Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticFiles Import Results in "No module named 'aiofiles' Error #4234

Closed
9 tasks done
rbrady opened this issue Dec 1, 2021 · 3 comments
Closed
9 tasks done

StaticFiles Import Results in "No module named 'aiofiles' Error #4234

rbrady opened this issue Dec 1, 2021 · 3 comments
Labels
question Question or problem question-migrate

Comments

@rbrady
Copy link

rbrady commented Dec 1, 2021

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

app = FastAPI()

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

Description

Attempt to run the file with uvicorn:

(.venv) ➜  hmx1 uvicorn app2:app --reload --port 8002
INFO:     Uvicorn running on http://127.0.0.1:8002 (Press CTRL+C to quit)
INFO:     Started reloader process [70344] using statreload
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/subprocess.py", line 61, in subprocess_started
    target(sockets=sockets)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/server.py", line 48, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/server.py", line 55, in serve
    config.load()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/config.py", line 308, in load
    self.loaded_app = import_from_string(self.app)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/importer.py", line 23, in import_from_string
    raise exc from None
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./app2.py", line 2, in <module>
    from fastapi.staticfiles import StaticFiles
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/fastapi/staticfiles.py", line 1, in <module>
    from starlette.staticfiles import StaticFiles as StaticFiles  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/starlette/staticfiles.py", line 7, in <module>
    from aiofiles.os import stat as aio_stat
ModuleNotFoundError: No module named 'aiofiles'

Operating System

macOS

Operating System Details

(.venv) ➜  hmx1 pip freeze
aiofiles==0.8.0
anyio==3.4.0
asgiref==3.4.1
Chameleon==3.9.1
click==8.0.3
fastapi==0.70.0
fastapi-chameleon==0.1.12
h11==0.12.0
idna==3.3
Jinja2==3.0.3
MarkupSafe==2.0.1
pydantic==1.8.2
sniffio==1.2.0
starlette==0.16.0
typing-extensions==4.0.1
uvicorn==0.15.0

FastAPI Version

0.70.0

Python Version

Python 3.8.7

Additional Context

I'm just trying to follow the tutorial at https://fastapi.tiangolo.com/advanced/templates/.

I can do the import interactively from a python terminal, but it fails when using uvicorn.

Python 3.8.7 (v3.8.7:6503f05dd5, Dec 21 2020, 12:45:15)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fastapi.staticfiles import StaticFiles
>>>
@rbrady rbrady added the question Question or problem label Dec 1, 2021
@rbrady
Copy link
Author

rbrady commented Dec 2, 2021

It appears to work fine on Fedora, but duplicating in a new directory with a new venv and using pip install with --no-cache-dir doesn't improve the situation on osx.

@markusobi
Copy link

markusobi commented Mar 7, 2022

I ran into the same or similar (?) issue. As you can see from your posted error message, starlette cannot find module aiofiles.

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/starlette/staticfiles.py", line 7, in <module>
    from aiofiles.os import stat as aio_stat
ModuleNotFoundError: No module named 'aiofiles'

In my case aiofiles was missing completely. There seems to be a bug in old version of starlette: starlette fails to define package dependency on aiofiles, but uses it!
I installed aiofiles manually to verify it and the problem was gone.

You listed aiofiles==0.8.0 - are you sure that the package was installed in the python (virtual) environment that you were using to execute the fastapi application?

The bug in starlette was maybe accidentally fixed when starlette replaced aiofiles with anyio. See encode/starlette#1157

@camerondavison
Copy link

i just ran across this, is the fix still just to install aoifiles? or is there a feature of fastapi that needs to be enabled?

Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8694 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants