I have a developed Server application using pywps on a Flask server and now im trying to migrate it to an apache server.
Tech-stack :
OS: Windows Server
Python 3.9.6
Apache 2.4.48 (Win64)
mod-wsgi 4.8.0
I can open the configured URL but receive a status code 500. Error log says the following:
mod_wsgi (pid=7212, process='', application='127.0.0.1:8008|/wps'): Loading Python script file 'C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi'.
mod_wsgi (pid=7212): Failed to exec Python script file 'C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi'.
mod_wsgi (pid=7212): Exception occurred processing WSGI script 'C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi'.
Traceback (most recent call last):\r
File "C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi", line 9, in <module>\r
import pywps\r
ModuleNotFoundError: No module named 'pywps'\r
The wsgi file in question is :
from pathlib import Path
import pywps
from pywps import Service
from processes.sayhello import SayHello
from processes.csv_input import CSVInputs
processes = [
SayHello(),
CSVInputs()
]
application = Service(processes, [Path(r'C:\Users\Jdoe\wps_env\pywps-flask-master\pywps.cfg')])
Now comes the strange thing, i am able to execute this exakt same script from the powershell with no errors at all.
I would rule out, path or enviroment related issues, because i have installed all pip packages which i used in the virtaul enviroemnt i have to develop in the global namespace aswell, so both interpreters know the same packages. I know this is not best practice, but i'm currently working in a sandbox anyway and this more a POC then anything else.
Since the wsgi application tries to run, i also assume that my apache conf is correct.
What am I missing here?
Thank you for your help
ModuleNotFoundError means that a module is missing and that you have to install it. In your case its PyWPS.
So open a terminal and type
pip install pywps
or
pip3 install pywps
then if you rerun your code it should work.
P.S. You can find the package on PyPI
I am trying to start learning python bottle framework, I've installed python 2.7.11, and installed pip too, I've installed bottle using
pip install bottle
Collecting bottle
Using cached bottle-0.12.9.tar.gz
Installing collected packages: bottle
Running setup.py install for bottle ... done
Successfully installed bottle-0.12.9
Now I tried to run the example code from bottle website
from bottle import route, run, template
#route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
when I run this code it throws the following error
Traceback (most recent call last):
File "C:/Users/SID/Desktop/bottle.py", line 1, in <module>
from bottle import route, run, template
File "C:/Users/SID/Desktop\bottle.py", line 1, in <module>
from bottle import route, run, template
ImportError: cannot import name route
I don't know what went wrong could some one guide me, Is it the error with the code? or in bottle installation?
Note: I Tried python 3.4.3 too still facing same error while running program and I'm using windows 8.1 in virtual box
You have bottle.py file within your project folder. Problem occurred because python module C:/Users/SID/Desktop/bottle.py shadowed bottle.py module which installed by pip. Rename file which shadow real bottle.py module to fix import problem.
Location of file which shall be renamed is C:/Users/SID/Desktop/bottle.py.
When a module named bottle is imported, the interpreter first searches for a built-in module with that name. If not found, as in this case it then searches for a file named bottle.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
Make sure bottle is in PYTHONPATH. More info on PYTHONPATH here
I've been working on a Flask app for a few weeks. I finished it today and went to deploy it... and now it won't launch.
I haven't added or removed any code so assume something has changed in the deployment process?
Anyway, here is the full error displayed in the terminal:
Traceback (most recent call last):
File "C:\Users\Kev\Documents\Projects\Docket\manage.py", line 5, in <module>
from app import create_app, db
File "C:\Users\Kev\Documents\Projects\Docket\app\__init__.py", line 21, in <module>
from app.api import api, blueprint, limiter
File "C:\Users\Kev\Documents\Projects\Docket\app\api\__init__.py", line 2, in <module>
from flask_restplus import Api
File "C:\Users\Kev\.virtualenvs\Docket-LasDxOWU\lib\site-packages\flask_restplus\__init_
_.py", line 4, in <module>
from . import fields, reqparse, apidoc, inputs, cors
File "C:\Users\Kev\.virtualenvs\Docket-LasDxOWU\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\Kev\.virtualen
vs\Docket-LasDxOWU\lib\site-packages\werkzeug\__init__.py)
Also here's the code in the three files mentioned.
manage.py:
from apscheduler.schedulers.background import BackgroundScheduler
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import create_app, db
app = create_app()
app.app_context().push()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
from app.routes import *
from app.models import *
def clear_data():
with app.app_context():
db.session.query(User).delete()
db.session.query(Todo).delete()
db.session.commit()
print("Deleted table rows!")
#manager.command
def run():
scheduler = BackgroundScheduler()
scheduler.add_job(clear_data, trigger='interval', minutes=15)
scheduler.start()
app.run(debug=True)
if __name__ == '__main__':
clear_data()
manager.run()
app/__init__.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config import Config
db = SQLAlchemy()
login = LoginManager()
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
login.init_app(app)
login.login_view = 'login'
from app.api import api, blueprint, limiter
from app.api.endpoints import users, todos, register
from app.api.endpoints.todos import TodosNS
from app.api.endpoints.users import UserNS
from app.api.endpoints.register import RegisterNS
api.init_app(app)
app.register_blueprint(blueprint)
limiter.init_app(app)
api.add_namespace(TodosNS)
api.add_namespace(UserNS)
api.add_namespace(RegisterNS)
return app
api/__init__.py:
from logging import StreamHandler
from flask_restplus import Api
from flask import Blueprint
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
blueprint = Blueprint('api', __name__, url_prefix='/api')
limiter = Limiter(key_func=get_remote_address)
limiter.logger.addHandler(StreamHandler())
api = Api(blueprint, doc='/documentation', version='1.0', title='Docket API',
description='API for Docket. Create users and todo items through a REST API.\n'
'First of all, begin by registering a new user via the registration form in the web interface.\n'
'Or via a `POST` request to the `/Register/` end point', decorators=[limiter.limit("50/day", error_message="API request limit has been reached (50 per day)")])
I've tried reinstalling flask & flask_restplus but no-luck.
The proper answer for May 2020: flask-restplus is dead, move to flask-restx.
From noirbizarre/flask-restplus#778 (comment):
flask-restplus work has been discontinued due to maintainers not having pypi keys. See the drop in replacement, flask-restx. It's an official fork by the maintainer team. We have already fixed the issue there
From noirbizarre/flask-restplus#777 (comment):
No. Flask-restplus is no longer maintained. The former maintainers do not have privileges to push to pypi, and after many months of trying, we forked the project. Check out flask-restx. It's a drop in replacement and we are roadmapping, designing, and making fixes...for instance, we already patched for Werkzeug
So the real solution is to move to flask-restx rather than pinning to an old version of Werkzeug.
Downgrading to Werkzeug==0.16.1 solves this
see https://github.com/noirbizarre/flask-restplus/issues/777#issuecomment-583235327
EDIT
Wanted to add that a permanent(long term) solution would be to move to flask_restx as flask-restplus is no longer being maintained.
See how to migrate from flask-restplus
Try:
from werkzeug.utils import cached_property
https://werkzeug.palletsprojects.com/en/1.0.x/utils/
Downgrade Werkzeug to 0.16.1
pip3 install --upgrade Werkzeug==0.16.1
If you do a pip3 list you may see something like this:
Flask 1.1.2
Werkzeug 0.16.1
For fix this painful bug was to identify what happen after the installation of the new package in the file PipFile.lock was to do a diff file and find the differences:
What I found after the installation of the package was the werkzeug package change her version of "version": "==0.15.2" to "version": "==1.0.1" and when try to excute the command sudo docker-compose build give me the error.
To fix this error this is what I did:
Discard all the change and start again.
Stop and remove your previous docker
sudo docker stop $(sudo docker ps -aq)
sudo docker rm $(sudo docker ps -aq)
Execute again the docker:
sudo docker-compose build
sudo docker-compose up
Go inside of the docker , get the id of the docker : first press CRTL+z to pause the docker and after in the terminal execute
sudo docker ps
Get the number of the CONTAINER ID column
execute : sudo docker exec -it 23f77dbefd57 bash for enter in terminal of the docker
Now execute the package that you want in my case is SOAPpy, like this
`pipenv install SOAPpy`
And after this instalation, install the previous package of the werkzeug package in my case is
pipenv install Werkzeug==0.15.2
write exit and press "Enter" in the terminal, for close the terminal inside the docker.
If you compare the files Pipfile.lock have the same version, this is the real fix.
for final steps is to do : stop , build and up the docker again:
sudo docker stop $(sudo docker ps -aq)
sudo docker-compose build
sudo docker-compose up
Now is running again the docker:
A note to myself, I just want to remind you that you are capable of
everything and not giving up so easily, remember there is always one
way, it is only to connect the dots. Never give up without fighting.
This may be too weird to happen to anyone else, but... Check your actually-imported packages. Mine looked like this:
Clearly, something borked on import here... removed and readded the correct "werkzeug" package and it "worked" (turns out I still need to implement one of the other solutions offered to this question... :-( )
Ah- but you ask: "how do I remove a corrupted package like this? The GUI won't let me!!". Fear not, that happened to me too. In Pycharm, find the package file location by hovering over the package name under the settings menu, go there in file explorer, and delete the folder and anything else like it. Then reinstall the package with the GUI.
If you or anyone who are doing some flask course about swagger that require restplus, Werkzeug. Please uninstall restplus and use restx instead since restplus is dead so any solutions that require downgrading Werkzeug or restplus OR typing some weird stuff that you can't understand, is just making another error.
Read this article about restx, it fairly similar to restplus.
Flask-restx article
I have created a virtual environment specifying python3
$ virtualenv -p /usr/bin/python3 flask_tut
$ source flask_tut/bin/activate
created a file named hello.py containing:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
and, while in my working directory containing hello.py, enter:
(flask_tut) $ export FLASK_APP=hello.py
(flask_tut) $ python -m flask run
However, this yields errors that I am struggling with:
Traceback (most recent call last):
File "/home/matt/Envs/flask_tut/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/matt/Envs/flask_tut/local/lib/python3.5/site-packages/flask/cli.py", line 478, in main
...
ImportError: cannot import name 'flask'
This is weird to me, I have installed flask as pip list indicates I have Flask (0.11.1). Others have solved similar issues by using python 3 which I am using as python --version shows I am using Python 3.5.1+
If anyone can provide help that would be excellent.
I am using Linux Mint if that makes a difference.
Edit:
After trying 100 different things, I just deleted my virtual environment, and hello.py, created a new virtual environment, working directory and script, and there are no problems. I will post if I get down to the root issue as I suspect this may happen to others running through the Flask tutorial.
I'm following a Flask tutorial and am getting an import error. I have a file called run.py which contains:
from app import app
app.run(debug = True)
When I run ./run.py, I get:
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/Users/myName/Desktop/SquashScraper/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
This seems similar to this issue: http://stackoverflow.com/questions/26960235/python3-cannot-import-name-flask
So I attempted the checked solution by running:
virtualenv -p /usr/bin/python3 my_py3_env
Unfortunately, I get:
The executable /usr/bin/python3 (from --python=/usr/bin/python3) does not exist
Any ideas what may be happening here?
Thanks for the help,
bclayman
If you want your virtual environment to be Python 3 but don't know the installation directory, use which python3. Use that directory in your virtualenv -p [directory] my_py3_env command to set up the Python 3 virtual environment.
I sounds like your pip is installing to your Python 2.X directory. If you're okay with that, you'll need to run the app either with python2 run.py, python2.X run.py where x is your installed version, or change the symlink of python in /usr/bin/python to your installation of Python 2.
This question has some more information.
Regardless of the version of Python that you wish to use, you will need to install Flask to that version of Python. See this question for that.