简体   繁体   中英

ImportError: cannot import name 'NinjaAPI' from 'ninja'

I am working on the Django ninja rest framework. the project is working fine on my machine, but when I try to run it in a virtual environment (venv) and install all requirements.txt including django-ninja it raise an error (ImportError: cannot import name 'NinjaAPI' from 'ninja').

The line that causes the error

from ninja import NinjaAPI

When I install the ninja, it gives me feedback the package is already installed

Requirement already satisfied: django-ninja 

I use this command to create a virtual env.

python3 -m venv venv 

These are my requirements.txt

asgiref==3.4.1
cffi==1.14.6
cryptography==3.4.7
dj-database-url==0.5.0
Django==3.2.6
django-heroku==0.3.1
django-ninja==0.13.2
gunicorn==20.1.0
jwt==1.2.0
ninja==1.10.2
psycopg2==2.9.1
psycopg2-binary==2.9.1
pycparser==2.20
pydantic==1.8.2
PyJWT==2.1.0
pytz==2021.1
sqlparse==0.4.1
typing-extensions==3.10.0.0
whitenoise==5.3.0

Python version

Python 3.9.0

pip version:

pip 21.2.3 

you need to install only django-ninja (not ninja)

run

pip uninstall ninja

on your environment, and you should be good to go

This might be specific to Replit, but here's what causes this error for me:

  1. I install django-ninja
  2. I start setting up my api stuff (everything works)
  3. Replit inexplicably installs ninja for some reason
  4. I get the error above

To solve it, you'd think that

pip uninstall ninja

would work. Sometimes it does, sometimes it doesn't. If it doesn't, then you'll need to run

pip uninstall django-ninja
pip install django-ninja

Why do we need to reinstall django-ninja? I have no idea. Strangely, sometimes after doing all this, I have to uninstall and reinstall django-ninja later in the replit!

To replicate this issue, moving your api.py file from your app to your project will often trigger Replit to install ninja.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM