ModuleNotFoundError: No module named 'apscheduler' in Flask - python

I want to set up a flask server with a scheduler using APScheduler.
Unfortunately flask doesn't want to run with APScheduler - it crashes at the imports.
I tried APScheduler==2.1.2 and use:
from flask import Flask
from apscheduler.scheduler import Scheduler
app = Flask(__name__)
I tried also the newest APScheduler==3.6.3 and use:
from flask import Flask
from apscheduler.schedulers.background import BackgroundScheduler
app = Flask(__name__)
In both cases after running flask run I got:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "C:\(...)\Continuum\anaconda3\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "C:\(...)\app\__init__.py", line 2, in <module>
from apscheduler.schedulers.background import BackgroundScheduler
ModuleNotFoundError: No module named 'apscheduler'
I tried installing Apscheduler with pip, pip3 and conda - same results. I tried Flask-APScheduler - same reuslts.
PyCharm recognizes and hints the APScheduler (as well as IPython), but flask doesn't.

Solved. The issue was that I have 2 flasks installed on my computer:
first one is global
second one is for my venv only
The flask run was executing the global one (because in Path environment variable there was only path to this one), but APScheduler is installed within my venv. I deleted the global flask and changed the Path variable to my venv. I don't know, if this is a proper way to solve it, but now it works.

Related

Issues with flask 1,1.2 and 2.2.2

So I've been following along a course which deals with web services and I've encountered an issue with flask dependencies.
my requirements.txt looks like this:
PyMySQL==1.0.2
cryptography==37.0.4
Flask==2.2.2
flask_sqlalchemy==2.5.1
sqlalchemy < 1.4.0
flask_migrate==2.7.0
flask_script==2.0.6
sqlalchemy_utils==0.38.3
And my manage.py which I run with python manage.py db init
looks like this
from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from configuration import Configuration
from models import database
from sqlalchemy_utils import database_exists, create_database
application = Flask(__name__)
application.config.from_object(Configuration)
migrate = Migrate(application, database)
manager = Manager(application)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
if(not database_exists(Configuration.SQLALCHEMY_DATABASE_URI)):
create_database(Configuration.SQLALCHEMY_DATABASE_URI)
manager.run()
When I use flask 2.2.2 I get this error:
Traceback (most recent call last):
File "/home/remax/PycharmProjects/V3/manage.py", line 3, in <module>
from flask_script import Manager
File "/home/remax/PycharmProjects/V3/venv/lib/python3.10/site-packages/flask_script/__init__.py", line 15, in <module>
from flask._compat import text_type
ModuleNotFoundError: No module named 'flask._compat'
But when I go back to flask 1.1.2 I get this
ImportError: cannot import name 'escape' from 'jinja2'
I honestly don't know how to deal with this, I've been trying to find a solution all morning
flask-script has been obsolete since Flask 0.11 added command support. It hasn't been updated to work with Flask 2, so if you want to use Flask 2, you'll need to get rid of it (and use Flask's native command support instead).
If you have upgraded Flask to 2.x and decide to downgrade to 1.x, you will need to install compatible versions of supporting packages (werkzeug, itsdangerous, jinja2). It might be easiest to just uninstall all packages from your virtualenv (you're using one, right?) and reinstall the correct versions, but you can also do it by hand.

Packages not visible after venv added

I've installed .venv into my python project. I've activated it and then installed from requirments:
python -m venv .ven
.\.ven\Scripts\activate
pip3 install -r requirements.txt
when i run pip3 list i see all packages installed. Nevertheless when trying to run my application i see:
Traceback (most recent call last):
File "d:/Projekty/_Python/therobertseye/app.py", line 1, in <module>
from website import create_app
File "d:\Projekty\_Python\therobertseye\website\__init__.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'
in my _init.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
but its not only Flask - when i did remove for testing purpose :
from flask import Flask
then when ran again i see:
Traceback (most recent call last):
File "d:/Projekty/_Python/therobertseye/app.py", line 1, in <module>
from website import create_app
File "d:\Projekty\_Python\therobertseye\website\__init__.py", line 1, in <module>
from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'
Means none of my packages are visible. Looks like libraries in venv are not visible? How can i sovle it?
I run my project from app.py by python3.8.exe app.py
app.py:
from website import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
EDIT:
as stated by #np8 both commands works on local:
python app.py
.\.ven\Scripts\python.exe app.py
Nevertheless when deployed on Azure i got errors in log:
021-12-11T10:11:56.228062960Z from website.Barcode import Barcode
2021-12-11T10:11:56.228068661Z File "/tmp/8d9bc8c8ae34714/website/Barcode.py", line 2, in <module>
2021-12-11T10:11:56.228074361Z import cv2
2021-12-11T10:11:56.228079661Z File "/tmp/8d9bc8c8ae34714/antenv/lib/python3.8/site-packages/cv2/__init__.py", line 8, in <module>
2021-12-11T10:11:56.228085161Z from .cv2 import *
2021-12-11T10:11:56.228090161Z ImportError: libGL.so.1: cannot open shared object file: No such file or directory
2021-12-11T10:11:56.228096061Z [2021-12-11 10:11:56 +0000] [41] [INFO] Worker exiting (pid: 41)
2021-12-11T10:11:56.497660146Z [2021-12-11 10:11:56 +0000] [39] [INFO] Shutting down: Master
2021-12-11T10:11:56.497793247Z [2021-12-11 10:11:56 +0000] [39] [INFO] Reason: Worker failed to boot.
/home/LogFiles/2021_12_11_10-30-0-5_docker.log
You are using different python executable than the one in the virtual environment. When you type python3.8.exe in cmd or powershell, Windows will try to look for an executable called python3.8.exe(*). It goes through a list of folders and these folders are in your Windows PATH variable.
How virtual environments work (partly) is that there will be a python.exe in the venv/Scripts folder. When you run the virtual environment activate script, the Scripts folder is added to the PATH of the current process (cmd or powershell). It is added to the top of the PATH so the python.exe in the venv will be the first one to be found.
Now, apparently there is no python3.8.exe in the virtual environment folder but a python.exe. Most probably just running
python app.py
will work (when virtual environment is activated in the vurrent process). If not, you can use
.\.ven\Scripts\python.exe app.py
which will always work even without activating the virtual environment.
(*)As a side note: the python3.8.exe thing is a Windows-specific thing. It is kind of a global shortcut called python3.8.exe which will point to a python.exe with correct version in a special place, as you saw yourself.

How can I permanently add a module to my flask app?

yes I know my question sounds like a duplicate but I have tried everything I have found.
I am trying to add a module to my python flask app. This module is located at ../../clients/api relative to my flask app. I haven't had any problems adding a module before, but for some reason this just doesn't want to work.
So far, I have tried:
sys.path.append("mypath") (worked temporarily but as soon as the shell was closed it disappeared)
adding a .pth file in lib/site-packages
adding it directly to my environment variables
adding in in my IDE (Wing 101)
Aside from 1., none of them had any effect whatsoever.
$ flask run
* Serving Flask app "main.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "main", an ImportError was raised:
Traceback (most recent call last):
File "c:\python37-32\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "my_flask_app_path", line 20, in <module>
from clients.api import Client as client
ModuleNotFoundError: No module named 'api'
Running this on python v-3.7.1, Windows 10, both git bash and cmd. Please try not to be blunt :)

Running Flask dev server in Python 3.6 raises ImportError for SocketServer and ForkingMixIn

I am trying to run a basic Flask app using Python 3.6. However, I get an ImportError: cannot import name 'ForkingMixIn'. I don't get this error when running with Python 2.7 or 3.5. How can I run Flask with Python 3.6?
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, World!"
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 65, in <module>
from SocketServer import ThreadingMixIn, ForkingMixIn
ImportError: No module named 'SocketServer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\fsk.py", line 9, in <module>
app.run()
File "C:\Python36\lib\site-packages\flask\app.py", line 828, in run
from werkzeug.serving import run_simple
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 68, in <module>
from socketserver import ThreadingMixIn, ForkingMixIn
ImportError: cannot import name 'ForkingMixIn'
This is fixed as of Werkzeug 0.11.15. Make sure you have installed the latest version of Werkzeug. pip install -U werkzeug.
This is a known issue that was reported to Werkzeug in anticipation of Python 3.6. Until that or another patch is merged and released, Werkzeug's dev server will not run on Python 3.6.
Check if OS can fork before importing ForkingMixIn since Python 3.6 will no longer define that when it is not available on the operating system (python/cpython#aadff9b) and ImportError: cannot import name 'ForkingMixIn' will occur.
In the mean time, you can run your app with an external WSGI server such as Gunicorn.
pip install gunicorn
gunicorn my_app:app
You can wrap your app in the debug middleware if you need the in-page debugger (as long as you only run Gunicorn with one worker).

Is the routes Dispatcher broken in CherryPy for Mac?

Is CherryPy broken? I just set it up and tried to use the routes dispatcher but it has an import error, my code is as follows:
import cherrypy
mapper = cherrypy.dispatch.RoutesDispatcher()
The error is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jwesonga/environments/cherrypy/lib/python2.6/site-packages/CherryPy-3.2.2-py2.6.egg/cherrypy/_cpdispatch.py", line 463, in __init__
import routes
ImportError: No module named routes
I'm on a Mac and I tried both 3.2.2 and 3.0 using virtualenv for the latter.
I have successfully used CherryPy with the routes dispatcher under OS X.
The error you've shown is:
ImportError: No module named routes
This is pretty clear -- Python can't find the routes modules. Have you installed it? This is not part of CherryPy, it's a separate module that you will need to install. If you're using MacPorts, you should be able to:
port install py-routes
(Or py25-routes or py26-routes depending on which Python you're using). If you're using virtualenv, you can simply run:
easy_install routes

Categories