1

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 
Arie
  • 3,041
  • 7
  • 32
  • 63
  • How do you run your project? The venv is activated before running the project in the same circumstances? – Ali Irani Dec 11 '21 at 04:36
  • @AliIrani i run my project running app.py - see in main post i enclosed it. python3.8.exe app.py. venv seems to be activated i see it in terminal – Arie Dec 11 '21 at 04:38
  • Would you please add the below solution to your code to see what packages are installed when you run your project? Add code from importing `create_app` in your `app.py` file. https://stackoverflow.com/a/23885252/10910984 – Ali Irani Dec 11 '21 at 04:43
  • Add `import sys; print(sys.executable)` to the top of your scipt to see if you are really using the python.exe from the `.ven` folder. – Niko Föhr Dec 11 '21 at 06:33
  • @np8 it shows: C:\Users\Robert\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe How can i fix it? – Arie Dec 11 '21 at 07:31

1 Answers1

1

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.

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
  • yes, when i do like this: .\venv\Scripts\python.exe .\app.py then works (on local) as well as if i run python .\app.py – Arie Dec 11 '21 at 09:55
  • so using ur two commands worked on local but once after i deployed on azure i see errors (see main post) whats going on? – Arie Dec 11 '21 at 10:13
  • Are you running a Windows or Linux machine on Azure? Did you try googling our your importerror? Seems that OpenCV has not been installed correctly. See, for example [ImportError: libGL.so.1: cannot open shared object file: No such file or directory](https://stackoverflow.com/questions/55313610/importerror-libgl-so-1-cannot-open-shared-object-file-no-such-file-or-directo) – Niko Föhr Dec 12 '21 at 17:34
  • scode installs linux container on publish. And yes i checked this link before and even run manualy and installed it on container, that not helped. – Arie Dec 12 '21 at 18:06