Packages not visible after venv added - python

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.

Related

ModuleNotFoundError: No module named 'apscheduler' in Flask

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.

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/")

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

Import error on first-party library with dev_appserver.py

On Ubuntu 16.04, am suddenly getting import errors from the local GAE development server.
The local dev server starts up, including the admin interface, but app no longer loads.
Native python imports of the same library on same machine (in this case "from google.cloud import datastore") work fine.
The GAE standard app does run when deployed, but development just got a little challenging.
google-cloud is version 0.27.0
gcloud components is 172.0.1
python is Anaconda 2.7.13
GAE is standard
I have confirmed to the best of my middling abilities that $PATH is correct for all named libraries.
I have removed and re-added all the named libraries to no effect.
cachetools(2.0.1) it should probably be noted, is installed as a dependency of the google cloud libraries, so I don't think this is addressable through requirements.txt or "libraries" in app.yaml.
I did recently go through a cycle of removing and adding libraries to fix a problem with apache_beam 2.0.1, so I may have jacked up something else, but am not sure where to look.
Suggestions deeply appreciated. Full traceback (from admin, same as from app):
Traceback (most recent call last):
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/request_handler.py", line 232, in handle_interactive_request
exec(compiled_code, self._command_globals)
File "<string>", line 1, in <module>
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/cloud/datastore/__init__.py", line 61, in <module>
from google.cloud.datastore.client import Client
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/cloud/datastore/client.py", line 23, in <module>
from google.cloud.client import ClientWithProject
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/cloud/client.py", line 27, in <module>
from google.oauth2 import service_account
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/oauth2/service_account.py", line 79, in <module>
from google.auth import jwt
File "/home/brian/anaconda3/lib/python2.7/site-packages/google/auth/jwt.py", line 49, in <module>
import cachetools
ImportError: No module named cachetools
The stacktrace indicates you're running the libraries from the local system installation (the site-packages dir), not from your app.
For standard env GAE apps you need to install the dependencies inside your app and they will be uploaded to GAE together with your app code.
More specifically you need to use the -t <your_app_lib_dir> option for the pip installation. From Installing a third-party library:
Use pip (version 6 or later) with the -t <directory> flag to copy the libraries into the folder you created in the previous
step. For example:
pip install -t lib/ <library_name>
I addressed my problem through the requirements.txt file in app root directory.
I had: google-cloud==0.22.0
and changed it to: google-cloud==0.27.0
which fixed it.

Import error when using uwsgi with django (Symbol not found: __PyInt_AsInt)

I've tried looking for a fix, but wasn't able to. Sorry if this question exists elsewhere.
I'm following http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html, and am currently trying to make sure django works with uWSGI. I'm trying to run uWSGI --http-socket :8000 --module mysite.wsgi, but am getting:
Traceback (most recent call last):
File "./mysite/wsgi.py", line 13, in <module>
from django.core.wsgi import get_wsgi_application
File "/anaconda/lib/python2.7/site-packages/django/core/wsgi.py", line 2, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/anaconda/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 3, in <module>
import cgi
File "/anaconda/lib/python2.7/cgi.py", line 50, in <module>
import mimetools
File "/anaconda/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/anaconda/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/anaconda/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/anaconda/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyInt_AsInt
Referenced from: /anaconda/lib/python2.7/lib-dynload/_io.so
Expected in: dynamic lookup
I already had issues with uWSGI not using the right installation.uwsgi would try to use /usr/local/bin/uwsgi despite
which uwsgi
//anaconda/bin/uwsgi
, resulting in No such file or directory. Currently, I set uWSGI="//anaconda/bin/uwsgi" to get around the problem at and get uwsgi to work, but I suspect a similar problem may be occurring. I think it might be trying to use /usr/bin/python instead of /anaconda/bin/python, and it can't import from anaconda, resulting in the error. However, I'm not sure how to fix it/confirm that this is the problem, and any advice would be appreciated.
Thanks!
EDIT: I have also tried import _io using all of the current versions of python installed, and there was no problem. brew unlink python helped deal with the conflicting anaconda version for the uwsgi problem (initially fixed with alias, as above), but this issue still exists.
EDIT: Removing anaconda from $PATH, and then reinstalling/unlinking/linking django, openssl, and libxml2 "works". I'll find out if this causes problems in the later steps shortly, but I still have no idea what was happening with the anaconda install. I'd really like to figure out what's going on since I use scipy/numpy/etc. very frequently, so this is definitely a temporary fix. Any ideas?
I just encountered this issue trying to run uwsgi on my mac with my django project
uwsgi --http :9090 --module uwsgi_django_project.wsgi
(in this case my django project was named uwsgi_django_project)
The stack-trace from uwsgi was
Traceback (most recent call last):
File "./uwsgi_django_project/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
File "/opt/anaconda3/envs/django_uwsgi_39/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
File "/opt/anaconda3/envs/django_uwsgi_39/lib/python3.8/site-packages/django/utils/version.py", line 1, in <module>
import datetime
File "/opt/anaconda3/envs/django_uwsgi_39/lib/python3.8/datetime.py", line 8, in <module>
import math as _math
ImportError: dlopen(/opt/anaconda3/envs/django_uwsgi_39/lib/python3.8/lib-dynload/math.cpython-38-darwin.so, 2): Symbol not found: _PyExc_MemoryError
Referenced from: /opt/anaconda3/envs/django_uwsgi_39/lib/python3.8/lib-dynload/math.cpython-38-darwin.so
Expected in: flat namespace
in /opt/anaconda3/envs/django_uwsgi_39/lib/python3.8/lib-dynload/math.cpython-38-darwin.so
unable to load app 0 (mountpoint='') (callable not found or import error)
Very similar to the error OP was getting except Symbol not found: _PyExc_MemoryError instead.
I had installed uwsgi via pip inside my conda environment and apparently there is an incompatability between the package installed via pip and the anaconda environment's python.
The solution was to remove uwsgi from my environment
pip uninstall uwsgi
and install it using anaconda
conda install -c conda-forge uwsgi
after doing this and running uwsgi with my django app it worked fine.
Credit to this blog where I found the solution to my issue which probably would solve a lot of similar issues with uwsgi and django

Categories