why is my flask API project can't run, am trying to run it through my window command line but am receiving erros - flask

Usage: python -m flask run [OPTIONS]
Try python -m flask run --help for help.
Error: While importing 'app', an ImportError was raised:
Traceback (most recent call last):
File "C:\Users\Dreams Reality\AppData\Roaming\Python\Python38\site-packages\flask\cli.py", line 218, in locate_app
__import__(module_name)
File "C:\Users\Dreams Reality\Desktop\api\app.py", line 2, in <module>
from flask_restplus import Api, Resource, fields
File "C:\Users\Dreams Reality\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_restplus\__init__.py", line 4, in <module>
from . import fields, reqparse, apidoc, inputs, cors
File "C:\Users\Dreams Reality\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_restplus\fields.py", line 17, in <module>
from werkzeug import cached_property
ImportError: cannot import name 'cached_property' from 'werkzeug' (C:\Users\Dreams Reality\AppData\Local\Programs\Python\Python38\lib\site-packages\werkzeug\__init__.py)
I tried running my flask project using python -m flask run
I expected to take a good look at my project on localhost,
And am getting this:
Try python -m flask run --help for help.
Error: While importing 'app', an ImportError was raised:
Traceback (most recent call last):
File "C:\Users\Dreams Reality\AppData\Roaming\Python\Python38\site-packages\flask\cli.py", line 218, in locate_app
__import__(module_name)
File "C:\Users\Dreams Reality\Desktop\api\app.py", line 2, in <module>
from flask_restplus import Api, Resource, fields
File "C:\Users\Dreams Reality\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_restplus\__init__.py", line 4, in <module>
from . import fields, reqparse, apidoc, inputs, cors
File "C:\Users\Dreams Reality\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_restplus\fields.py", line 17, in <module>
from werkzeug import cached_property
ImportError: cannot import name 'cached_property' from 'werkzeug' (C:\Users\Dreams Reality\AppData\Local\Programs\Python\Python38\lib\site-packages\werkzeug\__init__.py)

it seems like some of your packages aren't compatible with each other.
try checking your dependencies

flask-restplus does not appear to have been updated for several years[1]. Since then, Werkzeug moved cached_property[2].
In your position, I'd drop flask-restplus and either choose an actively maintained rest package for flask, or roll your own.
[1] https://github.com/noirbizarre/flask-restplus
[2] https://werkzeug.palletsprojects.com/en/1.0.x/changes/

Related

error on execute import django in python3 shell and script

when i want do an import django i get an error
ubuntu 18.04 with a reverse proxy (nginx) and uwsgi (mode emperor actived) in virtual env with python 3.6.3 and latest django 2.2.5
test.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
import django
print("test")'
when i run python3 test.py i get :
(venv) :~/testproject/testproject/testproject$ python3.6 test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
from django.http import HttpResponse
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/utils/version.py", line 4, in <module>
import subprocess
File "/usr/lib/python3.6/subprocess.py", line 140, in <module>
import threading
File "/usr/lib/python3.6/threading.py", line 7, in <module>
from traceback import format_exc as _format_exc
File "/usr/lib/python3.6/traceback.py", line 5, in <module>
import linecache
File "/home/lukas/testproject/venv/lib/python3.6/linecache.py", line 11, in <module>
import tokenize
File "/home/lukas/testproject/venv/lib/python3.6/tokenize.py", line 35, in <module>
from token import *
File "/home/lukas/testproject/testproject/testproject/token.py", line 1, in <module>
from django.contrib.auth.tokens import PasswordResetTokenGenerator
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 4, in <module>
from django.apps import apps as django_apps
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/apps/__init__.py", line 2, in <module>
from .registry import apps
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/apps/registry.py", line 426, in <module>
apps = Apps(installed_apps=None)
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/apps/registry.py", line 46, in __init__
self.ready_event = threading.Event()
AttributeError: module 'threading' has no attribute 'Event'
i have the same error on python3 shell when i do import django whereas django have been installed with pip3 install and production mode is ok and developement mode with runserver is ok too.
can you help me, i m lost...
Do you have, by any chance, a token module in you django project?
As the python path is modified to prefer local modules rather than other pre-defined modules, the from token import * instruction will import from your module instead of the python lib one.
Try renaming your local modules to avoid collision with builtin python modules.

ImportError: cannot import name _remove_dead_weakref in django virtualenv

After upgrading my Ubuntu desktop to 18.04 from 16.04, the django's virtualenv refuses to start:
(.djangoenv) mw#desktop:~/theapp$ python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 10, in <module>
from django.apps import apps
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/__init__.py", line 1, in <module>
from .config import AppConfig # NOQA
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/config.py", line 4, in <module>
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/exceptions.py", line 5, in <module>
from django.utils.encoding import force_text
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/encoding.py", line 10, in <module>
from django.utils.functional import Promise
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/functional.py", line 1, in <module>
import copy
File "/usr/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
On the djangoenv and outside it
$ python --version :
`Python 2.7.12`
Hoping to resolve the issue, I also install Python 2.7.16 on the desktop, so I get:
$ python2.7 --version
Python 2.7.16
Also when I try to install new virtualenv, I get the same error:
$ virtualenv .blaenv
Running virtualenv with interpreter /home/mw/.djangoenv/bin/python2
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/virtualenv.py", line 21, in <module>
import logging
File "/usr/lib/python2.7/logging/__init__.py", line 26, in <module>
import sys, os, time, cStringIO, traceback, warnings, weakref, collections
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Answers to the same error in other contexts did not help and my search led to no more clues.
So really appreciate your hints.
As per https://askubuntu.com/questions/981663/python2-7-broken-by-weakref-import-error-please-help you probably need to recreate your virtualenv. I suspect your problem here is that you need to deactivate your current env before you try to make the new one, in order to ensure that your system python2 interpreter is the one running the virtualenv command.

Import error when deploying Django app using Zappa?

I get the following error when instancing my Django app as a lambda function using Zappa.
[1549659279288] Instancing..
[1549659279532] cannot import name 'models': ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 580, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 245, in lambda_handler
handler = cls()
File "/var/task/handler.py", line 151, in __init__
wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
File "/var/task/zappa/ext/django_zappa.py", line 9, in get_django_wsgi
from django.core.wsgi import get_wsgi_application
File "/var/task/django/core/wsgi.py", line 2, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/var/task/django/core/handlers/wsgi.py", line 8, in <module>
from django.core.handlers import base
File "/var/task/django/core/handlers/base.py", line 7, in <module>
from django.urls import get_resolver, set_urlconf
File "/var/task/django/urls/__init__.py", line 1, in <module>
from .base import (
File "/var/task/django/urls/base.py", line 8, in <module>
from .exceptions import NoReverseMatch, Resolver404
File "/var/task/django/urls/exceptions.py", line 1, in <module>
from django.http import Http404
File "/var/task/django/http/__init__.py", line 5, in <module>
from django.http.response import (
File "/var/task/django/http/response.py", line 13, in <module>
from django.core.serializers.json import DjangoJSONEncoder
File "/var/task/django/core/serializers/__init__.py", line 23, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/var/task/django/core/serializers/base.py", line 6, in <module>
from django.db import models
ImportError: cannot import name 'models'
I have tried running the app locally using,
python manage.py runserver
and it works fine locally.
However, I get the above error when the app is deployed to a lambda function.
I'm using Django version 2.0.10, Python 3.6 along with an AWS Aurora RDS.
I expect the lambda function instance without errors in the same way as it does locally.
Invoke zappa to keep packaged zip after deploy and check folder structure or try to run the project from that folder.
Did you add Django to requirements file? As i recall Zappa rebuilds lambda package from a fresh virtual environment, installs requirements.txt and applies your folder.
Or check carefully cloudwatch logs for import errors. There are import errors that show up only when the container is refreshed after a deploy, that will not show up for every lambda invocations.

Standalone Django application with pyinstaller on Windows

I need to package a (rather simple) django application into a standalone executable on windows. After some failed attempts with py2exe (I really don't understand why everybody recommends it for django, it seems extremely complicated to set up) I tried pyinstaller. It does package the application without complaining, but if I try to run it using myapp.exe runserver localhost:8000, I am getting the following error:
Traceback (most recent call last):
File "<string>", line 5, in <module>
File "c:\python27\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_
module
exec(bytecode, module.__dict__)
File "...\build\ck\out00-PYZ.pyz\django.test", line 5, in <module>
File "c:\python27\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_
module
exec(bytecode, module.__dict__)
File "...\build\ck\out00-PYZ.pyz\django.test.client", line 21, in <module>
File "c:\python27\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_
module
exec(bytecode, module.__dict__)
File "...\build\ck\out00-PYZ.pyz\django.db", line 11, in <module>
File "...\build\ck\out00-PYZ.pyz\django.conf", line 52, in __getattr__
File "...\build\ck\out00-PYZ.pyz\django.conf", line 45, in _setup
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must eith
er define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
My manage.py script looks like this:
#!/usr/bin/env python
import os
import sys
import Cookie
import django.test
import HTMLParser
from django.conf import settings
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Why does it not recognize the environment variable and how can I fix that?
I solved it by importing django.test after the line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

Having Issues with Django on Windows Vista

I installed subversion, which seemed to install Python25, which my computer won't let me erase, or even recognize as a program on the control panel. I copied my Python27 Python.py file into the Python 25 folder to replace Python.py (the Python 25 version), and got my command prompt to run Python 27 (even though I had specified the Python 27 folder, it insisted on running Python 25). I copied the Django trunk into the site-packages of Django 27 and Django 25. Import Django works in Python, but now I'm getting this error:
C:\Windows\system32>cd C:\Users\Susan\Documents\practice
C:\Users\Susan\Documents\practice>django-admin.py startproject mysite
Traceback (most recent call last):
File "C:\csvn\Python25\django-admin.py", line 2, in <module>
from django.core import management
File "C:\csvn\Python25\lib\site-packages\django\core\management\__init__.py",
line 7, in <module>
from django.core.management.base import BaseCommand, CommandError, handle_de
fault_options
File "C:\csvn\Python25\lib\site-packages\django\core\management\base.py", line
14, in <module>
from django.utils.encoding import smart_str
File "C:\csvn\Python25\lib\site-packages\django\utils\encoding.py", line 2, in
<module>
import urllib
File "C:\csvn\Python25\lib\urllib.py", line 26, in <module>
import socket
File "C:\csvn\Python25\lib\socket.py", line 45, in <module>
import _socket
ImportError: Module use of python25.dll conflicts with this version of Python.
Thanks in advance.
Oh, and this is my environment path variable: C:\Program Files\SSH Communications Security\SSH Secure Shell;C:\Python27;C\Python27\scripts;C:\Python27\Lib\site-packages\django\bin\
And this is my python variable: C:\Python27;C\Python27\scripts;C:\Python27\Lib\site-packages\django\bin\
EDIT: realized that USER path wasn't the same as System path. Made them both the same, now receiving this message:
C:\Users\Susan\Documents\practice>django-admin.py startproject mysite
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\django\django\bin\django-admin.py", line 2
, in <module>
from django.core import management
File "C:\csvn\Python25\lib\site-packages\django\core\management\__init__.py",
line 7, in <module>
from django.core.management.base import BaseCommand, CommandError, handle_de
fault_options
File "C:\csvn\Python25\lib\site-packages\django\core\management\base.py", line
14, in <module>
from django.utils.encoding import smart_str
File "C:\csvn\Python25\lib\site-packages\django\utils\encoding.py", line 2, in
<module>
import urllib
File "C:\csvn\Python25\lib\urllib.py", line 26, in <module>
import socket
File "C:\csvn\Python25\lib\socket.py", line 45, in <module>
import _socket
ImportError: DLL load failed: The specified module could not be found.
Why does it keep trying to use Python25?
I would un-install Python 2.7 and 2.5 from your machine and re-inistall Python 2.7. You'll have to have a working Python install before you can think about installing Django.
After that's working, I would recommend installing virtualenv and Justin Driscoll's virtualenv helper for Windows.
Virtualenv allows you to create different Python runtimes so you don't have to install modules directly into the system Python Path. Installing virtualenv gets you pip, which you can use to install modules like Django, into your virtualenv's site-packages folder.