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

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 :)

Related

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 do I run this flask application without getting this error message?

(venv) -MBP microblog % flask run
* Serving Flask app "microblog.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "microblog", an ImportError was raised:
Traceback (most recent call last):
File "/Users//microblog/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/Users//microblog/microblog.py", line 1, in <module>
from app import app
ImportError: cannot import name 'app' from 'app' (unknown location)
I have tried export FLASK_APP=microblog.py
I have also tried to run it from multipled directories I'm not sure if that makes a difference.
Thanks.

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

serve multiple gunicorn django instances under nginx ubuntu

I need to serve up two webs apps (Django) on gunicorn under different domain names using nginx on ubuntu. I started using this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
It worked just fine for one using that. Then I tried to do the same thing for the second but it gives me a 502 gateway for the second domain. So I tried following this tutorial:
http://michal.karzynski.pl/blog/2013/10/29/serving-multiple-django-applications-with-nginx-gunicorn-supervisor/
I'm at the part where you run the gunicorn start script:
sudo bin/gunicorn_start
I get back the response:
Starting webuildblack as root
Traceback (most recent call last):
File "/home/devin/webuildblack/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
ImportError: No module named 'gunicorn.app'; 'gunicorn' is not a package
You have named your program gunicorn.py like the gunicorn-package. Rename your this file and remove any gunicorn.pyc file.

Import error with remote interpreter in pycharm

I am trying to run my code in the server using ssh remote interpreter.
The connection and the deployment work but when I want to import libraries located in the server it gives an import error
ssh://***#****.com:22/usr/bin/python -u
/home//main.py Traceback
(most recent call last): File
"/home//main.py", line 11,
in
from clplibs.clp import ContinuousLearningPlatform as clp ImportError: No module named clplibs.clp
Process finished with exit code 1
I've found a solution, the environment variables (including python path) should be defined from pycharm: Run/Debug configurations-> Environment variables. Pycharm won't use bashrc paths.

Categories